internal

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package sesame is an object to object mapper generator.

Index

Constants

View Source
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

View Source
var LogEnabledFor = LogLevelInfo

LogEnabledFor is a threshold for the logging.

Functions

func CanCast

func CanCast(sourceType, destType types.Type) bool

CanCast returns true if sourceType can be (safely) casted into destType.

func GetField

func GetField(st *types.Struct, name string, ignoreCase bool) (*types.Var, bool)

GetField finds a *types.Var by name. If a field not found, GetField returns false.

func GetMethod

func GetMethod(nm *types.Named, name string, ignoreCase bool) (*types.Func, bool)

GetMethod finds a *types.Func by name. If a method not found, GetField returns false.

func GetNamedType

func GetNamedType(typ types.Type) (*types.Named, bool)

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

func GetParamsCount(f *types.Func) int

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

func GetQualifiedTypeName(typ types.Type) string

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

func GetStructType(typ types.Type) (*types.Struct, bool)

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

func IsNillableType(typ types.Type) bool

IsNillableType returns true if given type can be nil.

func IsPointerPreferableType

func IsPointerPreferableType(typ types.Type) bool

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

func LoadConfig(target any, path string) error

LoadConfig read a config file from `path` relative to the current directory.

func LoadConfigFS

func LoadConfigFS(target any, path string, fs fs.FS) error

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

func ParseStruct(path string, name string, mctx *MappingContext) (types.Object, error)

ParseStruct parses a given Go source code file to find a struct.

func StdLog

func StdLog(level LogLevel, format string, args ...any)

StdLog is a Log that writes to stdout and stderr.

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 NewFuncID

func NewFuncID(fid string, sourceType string) FuncID

NewFuncID creates a new FuncID .

func (FuncID) ObjectID

func (f FuncID) ObjectID() string

ObjectID returns an object ID of this function.

func (FuncID) SourceType

func (f FuncID) SourceType() string

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

func (f Ignores) ConfigLoaded(path string) []error

ConfigLoaded is an event handler will be executed when config is loaded.

func (Ignores) Contains

func (f Ignores) Contains(typ OperandType, value string) bool

Contains returns true if this collection contains a value.

type Log

type Log func(level LogLevel, format string, args ...any)

Log is a function for the logging.

var LogFunc Log = StdLog

LogFunc is a Log used in this package.

type LogLevel

type LogLevel int

LogLevel is a level of the logging.

const (
	// LogLevelDebug is a debug level log.
	LogLevelDebug LogLevel = -4

	// LogLevelInfo is an info level log.
	LogLevelInfo LogLevel = 0

	// LogLevelWarn is a warning level log.
	LogLevelWarn LogLevel = 4

	// LogLevelError is an error level log.
	LogLevelError LogLevel = 8
)

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

func (m *Mappers) ConfigLoaded(path string) []error

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

func (m *Mapping) ConfigLoaded(path string) []error

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

func (m *Mapping) PrivateName() string

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

func (c *MappingContext) AddMapperFuncField(sourceType types.Type, destType types.Type, fid FuncID)

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 .

func (OperandType) String

func (v OperandType) String() string

String implements fmt.Stringer.

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.

func NewPrinter

func NewPrinter(dest string) (Printer, error)

NewPrinter creates a new Printer that writes a data to dest.

Jump to

Keyboard shortcuts

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