Documentation ¶
Index ¶
- Variables
- func CanSet(a any) bool
- func Coalesce[S []T, T any](first, second S, dflt T) S
- func ExpandEnvVars(s string) string
- func ExpandFileContents(s string) string
- func Flatten(values any) []any
- func Hack(values ...any) []any
- func ImplementsInterface[Reference any](value any) bool
- func IsProcessor(a any) bool
- func JoinSlices(elementSep string, indexSep string, slices ...[]string) string
- func Process(a any) error
- func ProcessMap(a any) error
- func ProcessRecursively(a any) error
- func ProcessSlice(a any) error
- func ProcessStruct(a any) error
- func RemoveNils(values []any) []any
- func ToAnySlice[T any](value []T) []any
- func ToIndirectReflectValue(value any) (reflect.Value, reflect.Type, bool)
- func ToSlice(values ...any) []any
- func ToStringSlice(values ...any) []string
- type Config
- type Data
- type Field
- func (f Field) ElementType() reflect.Type
- func (f Field) HasTag(keys ...any) bool
- func (f Field) HasTagFalse(keys ...any) bool
- func (f Field) HasTagTrue(keys ...any) bool
- func (f Field) HasTagValue(key, value string) bool
- func (f Field) Identifier() string
- func (f Field) Kind() reflect.Kind
- func (f Field) MultiValued() bool
- func (f Field) NonEmptyTagValue(keys ...any) string
- func (f Field) Pointer() bool
- func (f Field) Tag(key string) Tag
- func (f Field) TagIdentifier(keys ...any) string
- func (f Field) TagName(keys ...any) string
- func (f Field) TagValueAtIndex(index int, keys ...any) string
- func (f Field) Tags() Tags
- func (f Field) ToData() Data
- func (f Field) ToStruct() (Struct, error)
- func (f Field) Type() reflect.Type
- type Fields
- func (f Fields) ByKinds(kinds ...reflect.Kind) Fields
- func (f Fields) ByName(name string) Field
- func (f Fields) ByNames(names ...any) Fields
- func (f Fields) ByTypes(types ...reflect.Type) Fields
- func (f Fields) Field() Field
- func (f Fields) Identifiers() []string
- func (f Fields) Kinds() []reflect.Kind
- func (f Fields) MultiValued() Fields
- func (f Fields) Names() []string
- func (f Fields) NonEmptyTagValues(keys ...any) []string
- func (f Fields) TagIdentifiers(keys ...any) []string
- func (f Fields) TagNames(keys ...any) []string
- func (f Fields) TagTypeNames(keys ...any) []string
- func (f Fields) TagTypes(keys ...any) []string
- func (f Fields) Tags() map[string]Tags
- func (f Fields) ToData() []Data
- func (f Fields) ToDataMap() map[string]Data
- func (f Fields) ToStructs() Structs
- func (f Fields) TypeNames() []string
- func (f Fields) Types() []reflect.Type
- func (f Fields) WithTag(keys ...any) Fields
- func (f Fields) WithTagFalse(key string) Fields
- func (f Fields) WithTagTrue(keys ...any) Fields
- func (f Fields) WithTagValue(key, value string) Fields
- func (f Fields) WithoutTag(keys ...any) Fields
- func (f Fields) WithoutTagValue(key, value string) Fields
- type From
- type Name
- type Namespace
- type Processor
- type SQLResult
- type SQLResults
- type Struct
- func (s *Struct) ExecuteTemplate(tpl string, funcs template.FuncMap, data map[string]any) (string, error)
- func (s *Struct) Extract(names ...any) map[string]Struct
- func (s *Struct) ExtractDataByName(names ...any) map[string]Data
- func (s *Struct) Fields() Fields
- func (s Struct) Identifier() string
- func (s Struct) LastNameSpace() string
- func (s *Struct) NewUUID() string
- func (s *Struct) SetUUID(id string)
- func (s Struct) TagIdentifier(keys ...any) string
- func (s Struct) TagName(keys ...any) string
- func (s Struct) ValueMap(tagKey string) ValueMap
- type StructWithData
- type Structconfig
- type Structs
- type Tag
- type Tags
- func (t Tags) Append(key, value string) Tags
- func (t Tags) ByKeys(keys ...any) Tags
- func (t Tags) Contains(key, value string) bool
- func (t Tags) Exists(keys ...any) bool
- func (t Tags) False(keys ...any) bool
- func (t Tags) Insert(key, value string, index int) Tags
- func (t Tags) NonEmptyValue(keys ...any) Tag
- func (t Tags) NotContains(key, value string) bool
- func (t Tags) Prepend(key, value string) Tags
- func (t Tags) Replace(key, value string, index int) Tags
- func (t Tags) Set(key string, values ...string) Tags
- func (t Tags) Tag(keys ...any) Tag
- func (t Tags) True(keys ...any) bool
- func (t Tags) Value(keys ...any) Tag
- type To
- type TypeMap
- type TypeMaps
- type UpdateStrategy
- type Value
- type ValueMap
- type Values
Constants ¶
This section is empty.
Variables ¶
var ConfigTagFalse = []string{"-"}
var Defaults = Config{}
var ForeignTypes = map[string]map[reflect.Type]any{}
var Postgres = TypeMap{ From: From{ "text": reflect.TypeOf("string"), "varchar": reflect.TypeOf("string"), "smallint": reflect.TypeOf(int(1)), "int": reflect.TypeOf(int(1)), "bigint": reflect.TypeOf(int(1)), }, To: To{ reflect.TypeOf(string("string")): "text", reflect.TypeOf(bool(true)): "boolean", reflect.TypeOf(int(1)): "bigint", reflect.TypeOf(int8(1)): "smallint", reflect.TypeOf(int16(1)): "smallint", reflect.TypeOf(int32(1)): "bigint", reflect.TypeOf(int64(1)): "bigint", reflect.TypeOf(float32(1.0)): "float4", reflect.TypeOf(float64(1.0)): "float8", reflect.TypeOf(time.Time{}): "timestamp with time zone", reflect.TypeOf([]byte{}): "bytea[]", // contains filtered or unexported fields }, }
var TemplateDataNames = map[string]string{
"Struct": "Struct",
}
var TemplateFuncMap = template.FuncMap{ "coalesce": Coalesce[[]string, string], "join": join, "joinslices": JoinSlices, "mapkeys": mapKeys, "mapvalues": mapValues, "replace": replace, "replaceall": replaceAll, "tolower": strings.ToLower, "tolowerslices": toLowerSlices, "toupper": strings.ToUpper, "toupperslices": toUpperSlices, "tostrings": toStrings, "toslice": toSlice, "tovaluemap": ToValueMap, }
TemplateFuncMap includes functions in addition to those already included by text/template: https://pkg.go.dev/text/template#hdr-Functions
var TemplateOptions = []string{
"missingkey=zero",
}
TemplateOptions is passed to all functions/methods that execute a template.Template see https://pkg.go.dev/text/template#Template.Option
var TypeMappings = TypeMaps{ "postgres": Postgres, }
Functions ¶
func Coalesce ¶
func Coalesce[S []T, T any](first, second S, dflt T) S
Coalesce tries to mimic similar sql functions: it iterates through slices at the same time and returns a slice with the first non-zero value for each element. Unequal length slices are not an issue
func ExpandEnvVars ¶
ExpandEnvVars substitutes environment variables of the form ${ENV_VAR_NAME} if you have characters that need to be escaped, they should be surrounded in quotes in the source string.
func ExpandFileContents ¶
ExpandFileContents substitutes the placeholder with the contents on the first line of a file. It only accepts the pattern {file:/path/to/file} if you have characters that need to be escaped, they should be surrounded in quotes in the source string.
func Flatten ¶
Flatten is intended for ragged slices that potentially include slices of slices (of slices of slices...) Don't push your luck
func ImplementsInterface ¶
ImplementsInterface is a simple wrapper for checking if a value implements and interface it is primarily useful in switch statements note: Reference must be passed as a type parameter
func IsProcessor ¶
func ProcessMap ¶
ProcessMap is used for dispatching - prefer Process as it will call ProcessMap for any map type that doesn't implement Processor interface
func ProcessRecursively ¶
ProcessRecursively dive's into any member or element processing. It then attempts to call a's Process method, if applicable.
func ProcessSlice ¶
ProcessSlice will only attempt to process elements, it won't manage the slice header itself - ie - it will modify or set elements to nil, but will not attempt to grow
func ProcessStruct ¶
ProcessStruct doesn't attempt to check/use the struct's Process method.
Instead it iterates through each member and attempts to Process them.
It also makes no effort to process members that are nil pointers or otherwise result in reflect.Kind() == reflect.Invalid.
func RemoveNils ¶
func ToAnySlice ¶
ToAnySlice converts a slice []T to []any by wrapping each element in interfaces. Useful when you have a concrete slice and need to feed it to something that requires []any.
func ToIndirectReflectValue ¶
ToIndirectReflectValue attempts to convert any value to a reflect.Value and indirect it. If value is nil, an invalid reflect.Value, or a nil pointer, the returned value will be invalid. It returns the indrected value and whether the original value was a pointer
func ToSlice ¶
ToSlice should convert a mix of single values and slices to []any, just don't get too tricky
func ToStringSlice ¶
Types ¶
type Config ¶
type Config struct { NameSpace []string NameSpaceSeparator string // default is "." UUID func(any) string Tags Tags // struct tags, not field tags, may optionally be parsed from tags labeled struct RemoveExistingTags bool // remove existing tags - false will simply apopend any included tags to current ones Attributes map[string]any // the kitchen sink... for everything else RemoveExistingAttributes bool // remove existing constraints - false will simply apopend any included constraints to current ones }
Config allows default config options for various functions Note: this is not yet implemented
type Field ¶
type Field struct { Name string Attributes map[string]any //string // catchall for additional attributes Parent *Struct Value reflect.StructField }
Field is a wrapper for reflect.StructField with some additional functionality for tags, templating, etc.
func (Field) ElementType ¶
ElementType returns the type of an element of a map or slice
func (Field) HasTagFalse ¶
HasTagFalse returns true if any Tag satisfies Tags.False
func (Field) HasTagTrue ¶
HasTagTrue returns true if its tags satisfy Tags.True
func (Field) HasTagValue ¶
HasTagValue returns true if it has both the key and value
func (Field) Identifier ¶
Identifier uses the parent's Struct.Identifier and appends the field's Name to it
func (Field) MultiValued ¶
MultiValued returns true for any 'collection' type or any struct with more than one field
func (Field) NonEmptyTagValue ¶
func (Field) TagIdentifier ¶
TagIdentifier uses the parent's Struct.Identifier and appends the field's Name to it
func (Field) TagName ¶
TagName ranges through the provided keys in order and returns the first non-blank, non-false value, or field.Name if none are found.
func (Field) TagValueAtIndex ¶
TagValueAtIndex returns the first tag.True, non-blank value for keys in the given order. If no match is found, it returns the empty string.
type Fields ¶
type Fields []Field
func (Fields) Field ¶
Field is a convenience function to avoid having to index in a pipeline. It panics if Fields isn't length 1
func (Fields) Identifiers ¶
Identifiers returns a slice of the fully namespaced identifiers
func (Fields) MultiValued ¶
func (Fields) NonEmptyTagValues ¶
NonEmptyTagValues returns a slice of the first non-empty, non-nil tag that satisfies Tag.True and matches a key in the order given. In order to avoid slice length mismatches, it actually does use an empty string for fields where no match is found.
func (Fields) TagIdentifiers ¶
TagIdentifiers returns a slice of the fully namespaced identifiers from TagIdentifier
func (Fields) TagTypeNames ¶
func (Fields) TagTypes ¶
TagTypes returns the 'type', prefering a tagged value, then falling back to reflect.Type
func (Fields) ToStructs ¶
ElementsToStructs iterates through each field, attempts to convert it to a Struct, and returns a slice of the valid Structs. Note that it will panic if any field cannot be converted to a struct
func (Fields) WithTagFalse ¶
WithTagFalse returns a subset of Fields whose Tags satisfy Tags.False
func (Fields) WithTagTrue ¶
WithTagTrue returns a subset of Fields whose Tags satisfy Tags.True
func (Fields) WithTagValue ¶
WithTagValue returns a subset of Fields with both the key and value
func (Fields) WithoutTag ¶
WithoutTag returns a subset of Fields that do not have any of the given keys
func (Fields) WithoutTagValue ¶
WithoutTagValue returns a subset of Fields that do not have both the key and value
type From ¶
From is for inbound types - it should map the external system's type name as a key to go's reflect.Type as a value
type Name ¶
type Name struct {
// contains filtered or unexported fields
}
}
func (*Name) SetNamespace ¶
func (*Name) SetSeparator ¶
type SQLResults ¶
SQLResults is a slice of sql.Result that also satisfies the sql.Result interface It is intended to allow either merging or maintaining multiple results
func (SQLResults) AddResult ¶
func (r SQLResults) AddResult(results ...sql.Result) SQLResults
func (SQLResults) LastInsertId ¶
func (r SQLResults) LastInsertId() (int64, error)
func (SQLResults) RowsAffected ¶
func (r SQLResults) RowsAffected() (int64, error)
type Struct ¶
type Struct struct { Name string // default is type (without package namespace), single value children structs default to field name, slices default to type, maps default to key name NameSpace []string // default is []string{"package"} NameSpaceSeparator string // defaults to "." Value UUID string Attributes map[string]any Tags Tags //map[string][]string Parent *Struct Data }
Struct captures the properties of a struct and allows overriding properties using tags or by direct assignment
func NewStruct ¶
func NewStruct(value any, cfg Structconfig) (Struct, error)
NewStruct enables configuration when parsing a struct to a Struct
func (*Struct) ExecuteTemplate ¶
func (s *Struct) ExecuteTemplate(tpl string, funcs template.FuncMap, data map[string]any) (string, error)
ExecuteTemplate parses a string and executes it with any additional funcs and data. All data, including the reciever is passed to text/template as a map. By default, the reciever's map key is its type - eg {{ .Struct }} references a calling Struct. By default, it passes missingkey=zero, you can override this by changing TemplateOptions See TemplateFuncMap for additional functions included by default. See TemplateDataNames if you really need to change data map key names.
func (Struct) Identifier ¶
Identifier returns the full namespaced identifier of the struct
func (Struct) LastNameSpace ¶
func (Struct) TagIdentifier ¶
TagIdentifier returns the full namespaced identifier of the struct, but uses TagName instead of Name
type StructWithData ¶
type Structconfig ¶
type Structconfig struct { Name string NameSpace []string NameSpaceSeparator string // default is "." UUID string Tags Tags // struct tags, not field tags, may optionally be parsed from tags labeled struct RemoveExistingTags bool // remove existing tags - false will simply apopend any included tags to current ones Attributes map[string]any //string // these should be table constraints, not field constraints RemoveExistingAttributes bool // remove existing constraints - false will simply apopend any included constraints to current ones Parent *Struct }
type Structs ¶
type Structs []Struct
func NewStructs ¶
func NewStructs[S []T, T any](value S, cfg Structconfig) Structs
func (Structs) Identifiers ¶
func (Structs) ToStructMap ¶
type Tag ¶
type Tag []string
func (Tag) AtIndex ¶
AtIndex is a safe alternative to slice indexing. It returns empty string if index is out of bounds or the tag doesn't satify Tag.True
func (Tag) Contains ¶
Contains is a wrapper for slices.Contains that returns true if the tag contains any of the given values
func (Tag) False ¶
False only returns true if the tag's first value matches one in ConfigTagFalse (by default this is just "-")
func (Tag) Index ¶
Index is a wrapper for slices.Index that returns the index of the first value found in the tag, in order given, or -1 if not present
func (Tag) NotContains ¶
NotContains is a wrapper for slices.Contains that returns true if the tag doesn't contain any of the given values
type Tags ¶
Tags are the parsed struct field tags as map[tagLabel][]tagvalues
func (Tags) Append ¶
Append adds the value to the end of the key's tags it is safe to use even if the key doesn't currently exist
func (Tags) Contains ¶
Contains returns true if the tag with key has the value, regardless of its index in Tag
func (Tags) Exists ¶
Exists returns true if a tag with any of the given keys exists, even if it is empty
func (Tags) False ¶
False only returns true if the tags exists and the first value matches one in ConfigTagFalse (by default this is just "-")
func (Tags) NonEmptyValue ¶
NonEmptyValue returns the parsed Tag for the first key found, in order given, where the tag is non-nil, non-empty, and satisfies tag.True. It returns nil if no match is found.
func (Tags) NotContains ¶
NotContains returns true if there is no tag with the key and value
func (Tags) Prepend ¶
Prepend inserts the value to the beginning of the key's tags it is safe to use even if the key doesn't currently exist
type To ¶
To is for outbound types - it should map go's reflect.Type as a key to an external type name
type TypeMap ¶
TypeMap combines To/From
type TypeMaps ¶
Maps are a collection of external To/From mappings, they should use external system names as keys.
type UpdateStrategy ¶
type UpdateStrategy int8
const ( AppendChanges UpdateStrategy = iota AppendAll ReplaceChanges ReplaceAll )
func (UpdateStrategy) String ¶
func (u UpdateStrategy) String() string
type Value ¶
type Value struct { Name string // optional - intended for struct field names, map keys, or slice/array indexes reflect.Value Pointer bool Parent *Value // only populated if it is a member of a struct or elements of collections - slice, array, map, or channel }
Value is an attempt to capture a dereferenced reflect.Value. let's hope it works
func ToValue ¶
ToValue should be able to return a valid Value for anything except a nil pointer or an invalid reflect.Value
func (*Value) Child ¶
Child will attempt to return a value's child with the given index/key/field name. It accepts an int for slices, arrays, and structs. It accepts a string for maps and structs.
func (*Value) Children ¶
Children returns the elements of slices, arrays, and maps, and the non-anonymous, exported fields of structs if it can be considered a 'child' in some way, it should be returned by this method
func (Value) NewElement ¶
NewElement returns a blank element for slices, arrays, maps and channels. For everything else it returns an (invalid) Value and a non-nil error.
type ValueMap ¶
ValueMap's keys should match a struct's members' names (or tags) and values are wrapped in any()
func NewValueMap ¶
NewValueMap is a configurable version of ToValueMap - it converts a struct to a ValueMap based on the given tagKey, excludeValue, and includeValue