Documentation
¶
Overview ¶
Package sesame is an object to object mapper generator.
Index ¶
- Constants
- Variables
- func CanCast(sourceType, destType types.Type) bool
- func GetField(st *types.Struct, name string, ignoreCase bool) (*types.Var, bool)
- func GetMethod(nm *types.Named, name string, ignoreCase bool) (*types.Func, bool)
- func GetNamedType(typ types.Type) (*types.Named, bool)
- func GetNillableTypeSource(typ types.Type, mctx *MappingContext) string
- func GetParamsCount(f *types.Func) int
- func GetPreferableTypeSource(typ types.Type, mctx *MappingContext) string
- func GetQualifiedTypeName(typ types.Type) string
- func GetSource(typ types.Type, mctx *MappingContext) string
- func GetStructPointerTypeSource(typ types.Type, mctx *MappingContext) string
- func GetStructType(typ types.Type) (*types.Struct, bool)
- func GetValueTypeSource(typ types.Type, mctx *MappingContext) string
- func IsNillableType(typ types.Type) bool
- func IsPointerPreferableType(typ types.Type) bool
- func LoadConfig(target any, path string) error
- func LoadConfigFS(target any, path string, fs fs.FS) error
- func ParseFile(pkgPath string, mctx *MappingContext) (*types.Package, error)
- func ParseStruct(path string, name string, mctx *MappingContext) (types.Object, error)
- func StdLog(level LogLevel, format string, args ...any)
- type ConverterFuncField
- type FieldMapping
- type FieldMappings
- type FuncID
- type Generation
- type Generator
- type Ignores
- type Log
- type LogLevel
- type MapperFuncField
- type Mappers
- type Mapping
- type MappingContext
- func (c *MappingContext) AbsolutePackagePath() string
- func (c *MappingContext) AddConverterFuncField(sourceType types.Type, destType types.Type, fid FuncID)
- func (c *MappingContext) AddImport(path string)
- func (c *MappingContext) AddMapperFuncField(sourceType types.Type, destType types.Type, fid FuncID)
- func (c *MappingContext) ConverterFuncFields() []*ConverterFuncField
- func (c *MappingContext) GetConverterFuncFieldName(sourceType types.Type, destType types.Type, fid FuncID) *ConverterFuncField
- func (c *MappingContext) GetImportAlias(path string) string
- func (c *MappingContext) GetImportPath(alias string) string
- func (c *MappingContext) GetMapperFuncFieldName(sourceType types.Type, destType types.Type, fid FuncID) *MapperFuncField
- func (c *MappingContext) Imports() map[string]string
- func (c *MappingContext) MapperFuncFields() []*MapperFuncField
- func (c *MappingContext) NextVarCount() int
- type MappingOperand
- type MappingValue
- type NilCollection
- type ObjectMapping
- type OperandType
- type Printer
Constants ¶
const ( // NilCollectionUnknown is a default value of NilCollection. NilCollectionUnknown = iota // NilCollectionAsNil maps nil collections as a nil. NilCollectionAsNil // NilCollectionAsEmpty map nil maps as empty collections. NilCollectionAsEmpty // NilCollectionMax is a maximum value of NilCollection. NilCollectionMax )
Variables ¶
var LogEnabledFor = LogLevelInfo
LogEnabledFor is a threshold for the logging.
Functions ¶
func GetMethod ¶
GetMethod finds a *types.Func by name. If a method not found, GetField returns false.
func GetNamedType ¶
GetNamedType returns a named type if an underlying type is a struct type.
func GetNillableTypeSource ¶
func GetNillableTypeSource(typ types.Type, mctx *MappingContext) string
GetNillableTypeSource returns a source code of a nillable type.
- If type is an interface, a type without pointer - If type is a slice or a map, a type without pointer - Otherwise, a type with pointer.
func GetParamsCount ¶
GetParamsCount returns a number of arguments of a function. TODO: f.Signature() was added in Go1.23. Use it instead of this function.
func GetPreferableTypeSource ¶
func GetPreferableTypeSource(typ types.Type, mctx *MappingContext) string
GetPreferableTypeSource returns
- If type is defined in the universe, a type without pointer
- If type is an interface, a type without pointer
- If type is a slice or a map, a type without pointer
- Otherwise, a type with pointer
func GetQualifiedTypeName ¶
GetQualifiedTypeName returns a qualified name of given type. Qualified name is a string joinning package and name with #.
func GetSource ¶
func GetSource(typ types.Type, mctx *MappingContext) string
GetSource returns a string representation with an alias package name.
func GetStructPointerTypeSource ¶
func GetStructPointerTypeSource(typ types.Type, mctx *MappingContext) string
GetStructPointerTypeSource returns a source code of a type with a pointer.
func GetStructType ¶
GetStructType returns a struct type if an underlying type is a struct type.
func GetValueTypeSource ¶
func GetValueTypeSource(typ types.Type, mctx *MappingContext) string
GetValueTypeSource returns a source code of a type without a pointer.
func IsNillableType ¶
IsNillableType returns true if given type can be nil.
func IsPointerPreferableType ¶
IsPointerPreferableType returns true if given type seems to be better for using as a pointer.
- If a typ is a defined in the universe, interfaces, slices or maps, it returns false. - Otherwise(e.g. interface, structs), it returns true.
func LoadConfig ¶
LoadConfig read a config file from `path` relative to the current directory.
func LoadConfigFS ¶
LoadConfigFS read a config file from `path` in `fs`.
func ParseFile ¶
func ParseFile(pkgPath string, mctx *MappingContext) (*types.Package, error)
ParseFile parses a given Go source code file. Package will be imported as we in working directory. You may need to os.Chdir() before this method.
func ParseStruct ¶
ParseStruct parses a given Go source code file to find a struct.
Types ¶
type ConverterFuncField ¶
type ConverterFuncField struct {
// FieldName is a name of the field.
FieldName string
// Source is a source type of the function.
Source types.Type
// Dest is a source type of the function.
Dest types.Type
// ObjectID is an object id of the function.
// If this function is a global function, ObjectID is an empty string.
ObjectID string
// contains filtered or unexported fields
}
ConverterFuncField is a mapper function field.
func (*ConverterFuncField) Signature ¶
func (c *ConverterFuncField) Signature(mctx *MappingContext) string
Signature returns a function signature.
type FieldMapping ¶
type FieldMapping struct {
// A is a name of the field defined in [Mapping].A.
A string
// B is a name of the field defined in [Mapping].B.
B string
// Uses uses a given mapper/converter to map this field.
Uses string
// UsesForElements uses a given mapper/converter to map elements of this field.
UsesForElements string `mapstructure:"uses-for-elements"`
// SourceFile is a source file path that contains this configuration.
SourceFile string
}
FieldMapping is definitions of how fields will be mapped.
func (*FieldMapping) UsesFuncID ¶
func (m *FieldMapping) UsesFuncID(typ types.Type) FuncID
UsesFuncID returns a function name that will be used to map this field.
func (*FieldMapping) Value ¶
func (m *FieldMapping) Value(typ OperandType) string
Value returns a value by OperandType .
type FieldMappings ¶
type FieldMappings []*FieldMapping
FieldMappings is a collection of FieldMapping s.
func (FieldMappings) ConfigLoaded ¶
func (f FieldMappings) ConfigLoaded(path string) []error
ConfigLoaded is an event handler will be executed when config is loaded.
func (FieldMappings) Find ¶
func (f FieldMappings) Find(typ OperandType, value string) *FieldMapping
Find returns a FieldMapping that has a value.
func (FieldMappings) Pair ¶
func (f FieldMappings) Pair(typ OperandType, value string) (string, bool)
Pair returns a paired value.
type FuncID ¶
type FuncID string
FuncID is an ID of a function.
func (FuncID) SourceType ¶
SourceType returns a source type of this function.
type Generation ¶
type Generation struct {
// Mappers are a definition of the collection of mappers.
Mappers *Mappers
// Mappings is definitions of the mappings.
Mappings []*Mapping
// SourceFile is a source file path that contains this configuration.
SourceFile string
}
Generation is a definition of the mappings.
func (*Generation) ConfigLoaded ¶
func (g *Generation) ConfigLoaded(_ string) []error
ConfigLoaded is an event handler will be executed when config is loaded.
type Generator ¶
type Generator interface {
Generate() error
}
Generator is an interface that generates mappers.
func NewGenerator ¶
func NewGenerator(config *Generation) Generator
NewGenerator creates a new Generator .
type Ignores ¶
type Ignores []*FieldMapping
Ignores is a collection of fields should be ignored.
func (Ignores) ConfigLoaded ¶
ConfigLoaded is an event handler will be executed when config is loaded.
type MapperFuncField ¶
type MapperFuncField struct {
// FieldName is a name of the field.
FieldName string
// Source is a source type of the function.
Source types.Type
// Dest is a source type of the function.
Dest types.Type
// ObjectID is an object id of the function.
// If this function is a global function, ObjectID is an empty string.
ObjectID string
// contains filtered or unexported fields
}
MapperFuncField is a mapper function field.
func (*MapperFuncField) Signature ¶
func (m *MapperFuncField) Signature(mctx *MappingContext) string
Signature returns a function signature.
type Mappers ¶
type Mappers struct {
// Package is a package of a mappers.
Package string
// Destination is a file path that this mappers will be written.
Destination string
// NilMap defines how are nil maps are mapped.
NilMap NilCollection `mapstructure:"nil-map"`
// NilSlice defines how are nil maps are mapped.
NilSlice NilCollection `mapstructure:"nil-slice"`
// SourceFile is a source file path that contains this configuration.
SourceFile string
}
Mappers is a definition of the mappers.
func (*Mappers) ConfigLoaded ¶
ConfigLoaded is an event handler will be executed when config is loaded.
type Mapping ¶
type Mapping struct {
// ID is an ID of a mapper.
// If this is empty, Name will be used as an ID.
ID string
// Name is a name of a mapper.
Name string
// Package is a package of a mapper.
Package string
// Destination is a file path that this mapper will be written.
Destination string
// AtoB is a name of a function.
AtoB string `mapstructure:"a-to-b"`
// AtoB is a name of a function.
BtoA string `mapstructure:"b-to-a"`
// Bidirectional means this mapping is a bi-directional mapping.
Bidirectional bool
// A is a mapping operand.
A *MappingOperand
// B is a mapping operand.
B *MappingOperand
// SourceFile is a source file path that contains this configuration.
SourceFile string
// ObjectMapping is a mapping definition for objects.
ObjectMapping `mapstructure:",squash"`
}
Mapping is a definition of the mapping.
func (*Mapping) ConfigLoaded ¶
ConfigLoaded is an event handler will be executed when config is loaded.
func (*Mapping) MethodName ¶
func (m *Mapping) MethodName(typ OperandType) string
MethodName returns a name of a function that maps objects.
func (*Mapping) PrivateName ¶
PrivateName return a private-d name.
type MappingContext ¶
type MappingContext struct {
// contains filtered or unexported fields
}
MappingContext is an interface that contains contextual data for the generation.
func NewMappingContext ¶
func NewMappingContext(absPkgPath string) *MappingContext
NewMappingContext returns new MappingContext .
func (*MappingContext) AbsolutePackagePath ¶
func (c *MappingContext) AbsolutePackagePath() string
AbsolutePackagePath returns na absolute package path of a file will be generated this mapping.
func (*MappingContext) AddConverterFuncField ¶
func (c *MappingContext) AddConverterFuncField(sourceType types.Type, destType types.Type, fid FuncID)
AddConverterFuncField adds a converter function and generates a field name for it.
func (*MappingContext) AddImport ¶
func (c *MappingContext) AddImport(path string)
AddImport adds import path and generate new alias name for it.
func (*MappingContext) AddMapperFuncField ¶
AddMapperFuncField adds a mapper function and generates a field name for it.
func (*MappingContext) ConverterFuncFields ¶
func (c *MappingContext) ConverterFuncFields() []*ConverterFuncField
ConverterFuncFields returns a list of ConverterFuncField .
func (*MappingContext) GetConverterFuncFieldName ¶
func (c *MappingContext) GetConverterFuncFieldName( sourceType types.Type, destType types.Type, fid FuncID) *ConverterFuncField
GetConverterFuncFieldName returns a converter function field name.
func (*MappingContext) GetImportAlias ¶
func (c *MappingContext) GetImportAlias(path string) string
GetImportAlias returns an alias for the given import path.
func (*MappingContext) GetImportPath ¶
func (c *MappingContext) GetImportPath(alias string) string
GetImportPath returns a fully qualified path for the given import alias. If alias is not found, GetImportPath returns given alias.
func (*MappingContext) GetMapperFuncFieldName ¶
func (c *MappingContext) GetMapperFuncFieldName( sourceType types.Type, destType types.Type, fid FuncID) *MapperFuncField
GetMapperFuncFieldName returns a mapper function field name.
func (*MappingContext) Imports ¶
func (c *MappingContext) Imports() map[string]string
Imports returns a map of the all imports. Result map key is an import path and value is an alias.
func (*MappingContext) MapperFuncFields ¶
func (c *MappingContext) MapperFuncFields() []*MapperFuncField
MapperFuncFields returns a list of MapperFuncField .
func (*MappingContext) NextVarCount ¶
func (c *MappingContext) NextVarCount() int
NextVarCount returns a var count and increments it.
type MappingOperand ¶
type MappingOperand struct {
// Package is a package path
Package string
// Name is a type name of the target.
// This type must be defined in the File.
Name string
// SourceFile is a source file path that contains this configuration.
SourceFile string
}
MappingOperand is a mapping target.
func (*MappingOperand) ConfigLoaded ¶
func (m *MappingOperand) ConfigLoaded(path string) []error
ConfigLoaded is an event handler will be executed when config is loaded.
type MappingValue ¶
type MappingValue interface {
// GetGetterSource returns a source code of the getter.
GetGetterSource() string
// CanGet returns true if this value is readable.
CanGet() bool
// CanAddr returns true if this value with getter is addressable.
CanAddr() bool
// GetSetterSource returns a source code of the setter.
GetSetterSource(valueSource string) string
// DisplayName returns a name for humans.
DisplayName() string
// CanSet returns true if this value is writable.
CanSet() bool
// Type is a type of the value
Type() types.Type
}
MappingValue is a value that will be a source of the mapping or a destination of the mapping.
func NewLocalMappingValue ¶
func NewLocalMappingValue(name string, typ types.Type) MappingValue
NewLocalMappingValue is a MappingValue that related to local variables.
func NewObjectPropertyMappingValue ¶
func NewObjectPropertyMappingValue(base string, named *types.Named, name string, ignoreCase bool) (MappingValue, bool)
NewObjectPropertyMappingValue creates a new MappingValue related to the given object.
type NilCollection ¶
type NilCollection int
NilCollection is an enum that defines how are nil maps and nil slices mapped.
type ObjectMapping ¶
type ObjectMapping struct {
// ExplicitOnly indicates that implicit mappings should not be
// performed.
ExplicitOnly bool `mapstructure:"explicit-only"`
// IgnoreCase means this mapping ignores field name casing.
IgnoreCase bool `mapstructure:"ignore-case"`
// AllowUnmapped is set true, sesame does not fail if unmapped
// field exists.
AllowUnmapped bool `mapstructure:"allow-unmapped"`
// Fields is definitions of how fields will be mapped.
Fields FieldMappings
// Ignores is definitions of the fileds should be ignored.
Ignores Ignores
// NilMap defines how are nil maps are mapped.
NilMap NilCollection `mapstructure:"nil-map"`
// NilSlice defines how are nil maps are mapped.
NilSlice NilCollection `mapstructure:"nil-slice"`
}
ObjectMapping is a mapping definition for objects.
func NewObjectMapping ¶
func NewObjectMapping() *ObjectMapping
NewObjectMapping creates new ObjectMapping .
func (*ObjectMapping) AddField ¶
func (m *ObjectMapping) AddField(typ OperandType, v1, v2 string)
AddField adds new FieldMapping to this definition.
type OperandType ¶
type OperandType int
OperandType indicates a target for functions.
const ( // OperandA means that an operand is 'A'. OperandA OperandType = 0 // OperandB means that an operand is 'B'. OperandB OperandType = 1 )
func (OperandType) Inverted ¶
func (v OperandType) Inverted() OperandType
Inverted returns an inverted OperandType .
type Printer ¶
type Printer interface {
io.Closer
// P writes formatted-string and a newline.
P(string, ...any)
// WriteDoNotEdit writes a "DO NOT EDIT" header.
WriteDoNotEdit()
// AddVar adds a template variable name.
AddVar(string)
// ResolveVar resolves a variable value.
ResolveVar(string, string)
}
Printer writes generated source codes. If dest already exists, Printer appends a new data to the end of it.