Documentation
¶
Index ¶
- func DisableStrictNoMissingStructKeys(disable bool) newReflectNamerOption
- func SnakeCase(s string) string
- func WithExtraPatterns(patterns []string) newReflectNamerOption
- type ErrInvalidField
- type ErrInvalidFieldType
- type ErrInvalidFieldValue
- type ErrInvalidName
- type ErrInvalidParent
- type ErrInvalidPatternIndex
- type ErrNoPatternFound
- type FieldIndexPath
- type FormatReflectNamerOption
- type ReflectNamer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisableStrictNoMissingStructKeys ¶
func DisableStrictNoMissingStructKeys(disable bool) newReflectNamerOption
DisableStrictNoMissingStructKeys sets whether strict checking for missing struct keys is disabled. If set to true, the ReflectNamer will not require all pattern keys to be present in the struct.
func SnakeCase ¶
SnakeCase converts a string to snake_case, used for mapping struct field names to pattern keys.
func WithExtraPatterns ¶
func WithExtraPatterns(patterns []string) newReflectNamerOption
WithExtraPatterns adds extra resource name patterns to the ReflectNamer configuration.
Types ¶
type ErrInvalidField ¶
type ErrInvalidField struct {
// contains filtered or unexported fields
}
ErrInvalidField is returned when a required struct field is missing or does not match the expected pattern key.
func (ErrInvalidField) Error ¶
func (e ErrInvalidField) Error() string
Error implements the error interface for ErrInvalidField.
type ErrInvalidFieldType ¶
type ErrInvalidFieldType struct {
// contains filtered or unexported fields
}
ErrInvalidFieldType is returned when a struct field is of an unsupported type for resource name formatting/parsing.
func (ErrInvalidFieldType) Error ¶
func (e ErrInvalidFieldType) Error() string
Error implements the error interface for ErrInvalidFieldType.
type ErrInvalidFieldValue ¶
type ErrInvalidFieldValue struct {
// contains filtered or unexported fields
}
ErrInvalidFieldValue is returned when a struct field value is invalid (e.g., zero, empty, or nil) for resource name formatting/parsing.
func (ErrInvalidFieldValue) Error ¶
func (e ErrInvalidFieldValue) Error() string
Error implements the error interface for ErrInvalidFieldValue.
type ErrInvalidName ¶
type ErrInvalidName struct {
// contains filtered or unexported fields
}
ErrInvalidName is returned when a resource name is invalid or does not match any known pattern.
func (ErrInvalidName) Error ¶
func (e ErrInvalidName) Error() string
Error implements the error interface for ErrInvalidName.
type ErrInvalidParent ¶
type ErrInvalidParent struct {
// contains filtered or unexported fields
}
ErrInvalidParent is returned when a parent resource name is invalid or does not match any known pattern.
func (ErrInvalidParent) Error ¶
func (e ErrInvalidParent) Error() string
Error implements the error interface for ErrInvalidParent.
type ErrInvalidPatternIndex ¶
type ErrInvalidPatternIndex struct {
// contains filtered or unexported fields
}
ErrInvalidPatternIndex is returned when a pattern index is out of range or invalid.
func (ErrInvalidPatternIndex) Error ¶
func (e ErrInvalidPatternIndex) Error() string
Error implements the error interface for ErrInvalidPatternIndex.
type ErrNoPatternFound ¶
type ErrNoPatternFound struct {
// contains filtered or unexported fields
}
ErrNoPatternFound is returned when no suitable resource name pattern can be found for a given input.
func (ErrNoPatternFound) Error ¶
func (e ErrNoPatternFound) Error() string
Error implements the error interface for ErrNoPatternFound.
type FieldIndexPath ¶
type FieldIndexPath []int
FieldIndexPath represents the path to a struct field, supporting nested fields.
type FormatReflectNamerOption ¶
type FormatReflectNamerOption func(config formatReflectNamerConfig) formatReflectNamerConfig
FormatReflectNamerOption defines a function that modifies formatReflectNamerConfig.
func AsPatternIndex ¶
func AsPatternIndex(patternIndex int) FormatReflectNamerOption
AsPatternIndex sets the pattern index to use when formatting a resource name.
type ReflectNamer ¶
type ReflectNamer interface {
// Format returns the formatted resource name string using the specified AIP pattern.
// The pattern is selected by the provided options (e.g., AsPatternIndex).
// If no pattern index is provided, index 0 is used. If -1 is provided, all patterns
// are tried and the first that matches is used.
//
// Returns an error if the resource cannot be formatted due to missing/invalid fields
// or if the pattern index is invalid.
Format(in interface{}, options ...FormatReflectNamerOption) (string, error)
// MustFormat is like Format but panics if an error occurs.
// Use when you expect formatting to always succeed.
MustFormat(in interface{}, options ...FormatReflectNamerOption) string
// FormatParent returns the formatted parent resource name string using the specified AIP pattern.
// The pattern is selected by the provided options (e.g., AsPatternIndex).
// If no pattern index is provided, index 0 is used. If -1 is provided, all patterns
// are tried and the first that matches is used.
//
// Returns an error if the resource cannot be formatted due to missing/invalid fields
// or if the pattern index is invalid.
FormatParent(in interface{}, options ...FormatReflectNamerOption) (string, error)
// MustFormatParent is like FormatParent but panics if an error occurs.
// Use when you expect formatting to always succeed.
MustFormatParent(in interface{}, options ...FormatReflectNamerOption) string
// Parse parses a resource name string into the struct pointed to by 'in'.
// Returns the index of the pattern used for parsing, or an error if parsing fails.
//
// Returns an error if the resource cannot be parsed due to missing/invalid fields or
// if the resource name passed in is not a valid resource name for the resource.
Parse(name string, in interface{}) (int, error)
// MustParse is like Parse but panics if an error occurs.
// Use when you expect parsing to always succeed.
MustParse(name string, in interface{}) int
// ParseParent parses a parent resource name string into the struct pointed to by 'in'.
// Returns the index of the pattern used for parsing, or an error if parsing fails.
//
// Returns an error if the parent cannot be parsed due to missing/invalid fields or
// if the parent passed in is not a valid parent for the resource.
ParseParent(parent string, in interface{}) (int, error)
// MustParseParent is like ParseParent but panics if an error occurs.
// Use when you expect parsing to always succeed.
MustParseParent(parent string, in interface{}) int
}
ReflectNamer defines an interface for formatting and parsing resource names according to AIP (Google API Improvement Proposals) patterns using reflection. It supports resources with parent and ID fields, and can handle multiple patterns.
func NewReflectNamer ¶
func NewReflectNamer[ProtoType proto.Message]( options ...newReflectNamerOption, ) (ReflectNamer, error)
NewReflectNamer creates a ReflectNamer for the given proto message type ProtoType. It uses reflection to map struct fields to AIP resource name pattern variables.
If multiple fields are named or tagged with the same key, the last one is used. If struct tags are used, they must be of the form `aip_pattern:"key=pattern_key"`. Extra patterns can be added (starting at index 100) via options. By default, all pattern keys must be present in the struct; this can be disabled with DisableStrictNoMissingStructKeys.