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 GetPreferableTypeSource(typ types.Type, mctx *MappingContext) string
- func GetQualifiedTypeName(typ types.Type) string
- func GetSource(typ types.Type, mctx *MappingContext) string
- func GetStructType(typ types.Type) (*types.Struct, bool)
- func IsBuiltinType(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 SortedKeys[K cmp.Ordered, V any](in map[K]V) []K
- func StdLog(level LogLevel, format string, args ...any)
- type FieldMapping
- type FieldMappings
- 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) AddImport(importpath string)
- func (c *MappingContext) AddMapperFuncField(sourceType types.Type, destType types.Type)
- func (c *MappingContext) GetImportAlias(path string) string
- func (c *MappingContext) GetImportPath(alias string) string
- func (c *MappingContext) GetMapperFuncFieldName(sourceType types.Type, destType types.Type) *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 GetPreferableTypeSource ¶
func GetPreferableTypeSource(typ types.Type, mctx *MappingContext) string
GetPreferableTypeSource returns a string representation with an alias package name. GetPreferableTypeSource returns
- If type is defined in the universe, 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 GetStructType ¶
GetStructType returns a struct type if an underlying type is a struct type.
func IsBuiltinType ¶
IsBuiltinType returns true if given type is defined in the universe.
func IsPointerPreferableType ¶
IsPointerPreferableType returns true if given type seems to be better for using as a pointer.
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.
func ParseStruct ¶
ParseStruct parses a given Go source code file to find a struct.
func SortedKeys ¶
SortedKeys returns the keys of the map m.
Types ¶
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 // SourceFile is a source file path that contains this configuration. SourceFile string }
FieldMapping is definitions of how fields will be mapped.
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) Pair ¶
func (f FieldMappings) Pair(typ OperandType, value string) (string, bool)
Pair returns a paired value.
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 // MapperFuncName is a name of the mapper function. MapperFuncName string // Source is a source type of the function. Source types.Type // Dest is a source type of the function. Dest types.Type }
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 { // 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) AddImport ¶
func (c *MappingContext) AddImport(importpath string)
AddImport adds import path and generate new alias name for it.
func (*MappingContext) AddMapperFuncField ¶
func (c *MappingContext) AddMapperFuncField(sourceType types.Type, destType types.Type)
AddMapperFuncField adds a mapper function and generates a field name for it.
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) *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 GetGetterMethod() string // CanGet returns true if this value is readable. CanGet() 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.