Documentation
¶
Index ¶
- Variables
- func DetectedField(structTypeName string, fieldName string, fieldType reflect.Type, tag string, ...)
- type Generator
- type JSONGenerator
- type Mapper
- func (m *Mapper) AddFieldNestedMapper(fieldName string, nestedMapper *Mapper)
- func (m *Mapper) AddFieldTagName(fieldName, fieldTagName string)
- func (m *Mapper) GetFieldNestedMapper(fieldName string) *Mapper
- func (m *Mapper) GetFieldTagName(fieldName string) (string, bool)
- func (m *Mapper) GetFieldsTagName() map[string]string
- func (m *Mapper) GetNestedMappers() map[string]*Mapper
- func (m *Mapper) GetRequiredFields() map[string]bool
- func (m *Mapper) GetStructInstance() any
- func (m *Mapper) GetUniqueTypeReference() string
- func (m *Mapper) HasFieldsValidations() bool
- func (m *Mapper) IsFieldRequired(fieldName string) (isFieldRequired, fileExists bool)
- func (m *Mapper) SetFieldIsRequired(fieldName string, required bool)
- func (m *Mapper) Type() reflect.Type
- type ProtobufGenerator
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilGenerator = errors.New("generator cannot be nil") ErrNilMapper = errors.New("mapper cannot be nil") ErrNilStructInstance = errors.New("struct instance cannot be nil") ErrStructInstanceNotStruct = errors.New("struct instance must be a struct") ErrInvalidStructInstance = errors.New("invalid struct instance") )
Functions ¶
func DetectedField ¶
func DetectedField( structTypeName string, fieldName string, fieldType reflect.Type, tag string, required bool, logger *slog.Logger, )
DetectedField prints a detected field
Parameters:
- structTypeName: the name of the struct type - fieldName: the name of the field - fieldType: the type of the field - tag: the tag of the field - required: whether the field is required or not - logger: the logger to use
Types ¶
type Generator ¶
type Generator interface {
NewMapper(structInstance any) (*Mapper, error)
NewMapperWithNoError(structInstance any) *Mapper
}
Generator is an interface for creating a mapper
type JSONGenerator ¶
type JSONGenerator struct {
// contains filtered or unexported fields
}
JSONGenerator is a generator for JSON mappers
func NewJSONGenerator ¶
func NewJSONGenerator(logger *slog.Logger) *JSONGenerator
NewJSONGenerator creates a new JSON generator
Parameters:
- logger: optional logger to use for logging detected fields
Returns:
- *JSONGenerator: instance of the JSON generator
func (JSONGenerator) NewMapper ¶
func (j JSONGenerator) NewMapper(structInstance any) ( *Mapper, error, )
NewMapper creates the fields to validate from a JSON struct
Parameters:
- structInstance: instance of the JSON struct
Returns:
- *Mapper: instance of the mapper
- error: error if any
func (JSONGenerator) NewMapperWithNoError ¶
func (j JSONGenerator) NewMapperWithNoError(structInstance any) *Mapper
NewMapperWithNoError creates the fields to validate from a JSON struct
Parameters:
- structInstance: instance of the JSON struct
Returns:
- *Mapper: instance of the mapper
type Mapper ¶
type Mapper struct {
// contains filtered or unexported fields
}
Mapper is a map of fields to validate from a struct
func NewMapper ¶
NewMapper creates a new mapper
Parameters:
- structInstance: instance of the struct to create the mapper from
Returns:
- *Mapper: instance of the mapper
- error: error if the struct instance is nil
func (*Mapper) AddFieldNestedMapper ¶
AddFieldNestedMapper adds a nested mapper to the mapper
Parameters:
- fieldName: name of the field
- nestedMapper: nested mapper to add
func (*Mapper) AddFieldTagName ¶
AddFieldTagName adds a field tag name to the mapper
Parameters:
- fieldName: name of the field
- fieldTagName: tag name of the field
func (*Mapper) GetFieldNestedMapper ¶
GetFieldNestedMapper returns the nested mapper of a field
Parameters:
- fieldName: name of the field
Returns:
- *Mapper: nested mapper of the field, or nil if the field does not exist or has no nested mapper
func (*Mapper) GetFieldTagName ¶
GetFieldTagName returns the tag name of a field
Parameters:
- fieldName: name of the field
Returns:
- string: tag name of the field
- bool: true if the field exists, false otherwise
func (*Mapper) GetFieldsTagName ¶
GetFieldsTagName returns the fields of the mapper
Returns:
- map[string]string: map of fields where key is the field name and value is the tag name
func (*Mapper) GetNestedMappers ¶
GetNestedMappers returns the nested mappers of the mapper
Returns:
- map[string]*Mapper: map of nested mappers where key is the field name of the nested struct and value is the nested mapper
func (*Mapper) GetRequiredFields ¶
GetRequiredFields returns the required fields of the mapper
Returns:
- map[string]bool: map of required fields where key is the field name and value is a boolean to determine if the field is required
func (*Mapper) GetStructInstance ¶
GetStructInstance returns the instance of the struct
Returns:
- any: instance of the struct
func (*Mapper) GetUniqueTypeReference ¶ added in v0.7.1
GetUniqueTypeReference returns the unique type reference of the struct
Returns:
- string: unique type reference of the struct
func (*Mapper) HasFieldsValidations ¶
HasFieldsValidations returns if the mapper has fields
Returns:
- bool: true if the mapper has fields, false otherwise
func (*Mapper) IsFieldRequired ¶
IsFieldRequired returns if a field is required
Parameters:
- fieldName: name of the field
Returns:
- bool: true if the field is required, false otherwise
- bool: true if the field exists, false otherwise
func (*Mapper) SetFieldIsRequired ¶
SetFieldIsRequired sets if a field is required
Parameters:
- fieldName: name of the field
- required: true if the field is required, false otherwise
type ProtobufGenerator ¶
type ProtobufGenerator struct {
// contains filtered or unexported fields
}
ProtobufGenerator is a generator for Protobuf mappers
func NewProtobufGenerator ¶
func NewProtobufGenerator(logger *slog.Logger) *ProtobufGenerator
NewProtobufGenerator creates a new Protobuf generator
Parameters:
- logger: optional logger to use for logging detected fields
Returns:
- *ProtobufGenerator: instance of the Protobuf generator
func (ProtobufGenerator) NewMapper ¶
func (p ProtobufGenerator) NewMapper(structInstance any) ( *Mapper, error, )
NewMapper creates the fields to validate from a Protobuf compiled struct
Parameters:
- structInstance: instance of the Protobuf compiled struct
Returns:
- *Mapper: instance of the mapper
- error: error if any
func (ProtobufGenerator) NewMapperWithNoError ¶
func (p ProtobufGenerator) NewMapperWithNoError(structInstance any) *Mapper
NewMapperWithNoError creates the fields to validate from a Protobuf compiled struct
Parameters:
- structInstance: instance of the Protobuf compiled struct
Returns:
- *Mapper: instance of the mapper