mapper

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	State              = "state"
	SizeCache          = "sizeCache"
	UnknownFields      = "unknownFields"
	ProtobufTag        = "protobuf"
	ProtobufOneOf      = "oneof"
	ProtobufNamePrefix = "name="
	JSONTag            = "json"
	JSONOmitempty      = "omitempty"
)

Protobuf fields generated by the protoc compiler

Variables

View Source
var (
	ErrNilGenerator            = errors.New("generator cannot be nil")
	ErrNilMapper               = errors.New("mapper cannot be nil")
	ErrProtobufTagNotFound     = "missing protobuf tag: %s"
	ErrProtobufTagNameNotFound = "missing protobuf tag name: %s"
	ErrEmptyJSONTag            = "empty json tag: %s"
)

Functions

func DetectedField added in v0.6.0

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 interface{}) (*Mapper, error)
	NewMapperWithNoError(structInstance interface{}) *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 interface{}) (
	*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 added in v0.5.21

func (j JSONGenerator) NewMapperWithNoError(structInstance interface{}) *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

func NewMapper(structInstance interface{}) *Mapper

NewMapper creates a new mapper

Parameters:

  • structInstance: instance of the struct to create the mapper from

Returns:

  • *Mapper: instance of the mapper

func (*Mapper) AddFieldNestedMapper

func (m *Mapper) AddFieldNestedMapper(fieldName string, nestedMapper *Mapper)

AddFieldNestedMapper adds a nested mapper to the mapper

Parameters:

  • fieldName: name of the field
  • nestedMapper: nested mapper to add

func (*Mapper) AddFieldTagName

func (m *Mapper) AddFieldTagName(fieldName, fieldTagName string)

AddFieldTagName adds a field tag name to the mapper

Parameters:

  • fieldName: name of the field
  • fieldTagName: tag name of the field

func (*Mapper) GetFieldNestedMapper

func (m *Mapper) GetFieldNestedMapper(fieldName string) *Mapper

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

func (m *Mapper) GetFieldTagName(fieldName string) (
	string,
	bool,
)

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

func (m *Mapper) GetFieldsTagName() map[string]string

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

func (m *Mapper) GetNestedMappers() map[string]*Mapper

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

func (m *Mapper) GetRequiredFields() map[string]bool

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

func (m *Mapper) GetStructInstance() interface{}

GetStructInstance returns the instance of the struct

Returns:

  • interface{}: instance of the struct

func (*Mapper) HasFieldsValidations

func (m *Mapper) HasFieldsValidations() bool

HasFieldsValidations returns if the mapper has fields

Returns:

  • bool: true if the mapper has fields, false otherwise

func (*Mapper) IsFieldRequired

func (m *Mapper) IsFieldRequired(fieldName string) (bool, bool)

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

func (m *Mapper) SetFieldIsRequired(fieldName string, required bool)

SetFieldIsRequired sets if a field is required

Parameters:

  • fieldName: name of the field
  • required: true if the field is required, false otherwise

func (*Mapper) Type

func (m *Mapper) Type() reflect.Type

Type returns the type of the struct instance

Returns:

  • reflect.Type: type of the struct instance

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 interface{}) (
	*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 added in v0.5.21

func (p ProtobufGenerator) NewMapperWithNoError(structInstance interface{}) *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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL