Documentation ¶
Index ¶
- Constants
- Variables
- type Field
- type NestedField
- type Operation
- type Struct
- func (c *Struct) GetDecoderField(name string, context scopes.Context, scope scopes.Decoding) (*StructField, bool)
- func (c *Struct) GetField(name string) (*StructField, bool)
- func (c *Struct) GetFieldDBPath(path string, sep string) (*StructField, string, bool)
- func (c *Struct) GetNestedFields(ctx scopes.Context, decoding scopes.Decoding) []NestedField
- type StructField
Constants ¶
const ( // Tag names TAG_NAME_JSON = "json" TAG_NAME_BLAZE = "blaze" // `blaze` tag value TAG_SCOPE_CLIENT = "client" TAG_SCOPE_ADMIN = "admin" TAG_KEEP = "keep" TAG_NO_DB = "no-db" TAG_NO_HTTP = "no-http" TAG_SHORT = "short" TAG_TRANSFORM_STRING = "string" TAG_ENCODE_STRING = "string.encoder" TAG_DECODE_STRING = "string.decoder" // `blaze` tag operations, can be combined with a dot, e.g. `read.update` TAG_SV_IGNORE = "-" TAG_SV_READ = "read" TAG_SV_WRITE = "write" TAG_SV_CREATE = "create" TAG_SV_UPDATE = "update" TAG_SV_ALL = "all" )
Variables ¶
var Cache = &cache{}
Cache is a global cache of struct meta information.
var GetDBName = func(f reflect.StructField, fi *StructField) string { gorm := f.Tag.Get("gorm") if gorm != "" { for { var s string s, gorm, _ = strings.Cut(gorm, ";") col, name, _ := strings.Cut(s, ":") if col == "column" && name != "" { return name } if gorm == "" { break } } } return stringer.ToSnakeCase(f.Name) }
GetDBName is a callback function that returns the database name of a field. By default, it searches for the "column" tag in the field's gorm tag. If it is not found, it converts the [Field.Name] to snake case.
Functions ¶
This section is empty.
Types ¶
type Field ¶
type Field struct { // The native go name of the field. TitleCase string // The name of the field in the JSON representation. If not set in `json` tag, it will be converted from [Field.TitleCase] to camelCase. Name string // Precomputed key for the field in the json object. ObjectKey []byte // Defines if the field should be included in the database JSON. DBScope bool // Defines if the field should be included in the client marshaling/unmarshaling ClientScope Operation // Defines if the field should be included in the admin marshaling/unmarshaling AdminScope Operation // Defines if the field should be kept in the JSON object even if it's empty. KeepEmpty bool // If the field is a struct, then it will point to the struct definition. Otherwise, it will be nil. Struct *Struct Type reflect.Type Kind reflect.Kind // Defines if the field should be marshaled as a short version. Short bool // The database name of the field. Populated by [GetDBName] function. DBName string StringEncoding bool StringDecoding bool }
Field represents a meta info about field in a struct.
func (*Field) CheckDecoderScope ¶
CheckDecoderScope checks if the field can be decoded in the given context.
func (*Field) CheckEncoderScope ¶
CheckEncoderScope checks if the field can be encoded in the given context.
type NestedField ¶ added in v0.9.0
type NestedField struct { Path string GoPath string Field *StructField }
type Struct ¶
type Struct struct { Type reflect.Type // Fields is a list of fields in the struct. Fields []*StructField // contains filtered or unexported fields }
Struct represents a type meta information of a struct.
func (*Struct) GetDecoderField ¶
func (c *Struct) GetDecoderField(name string, context scopes.Context, scope scopes.Decoding) (*StructField, bool)
GetDecoderField returns a field by its name. The field must be accessible in the given scope.
func (*Struct) GetField ¶
func (c *Struct) GetField(name string) (*StructField, bool)
GetField returns a field by its name. If the field is not found, the second return value is [false].
func (*Struct) GetFieldDBPath ¶
GetFieldDBPath returns a field by its path. The path can be a dot-separated string of field names, e.g. "user.address.phoneNumber". The second return value is the database name of the field. It will use the [sep] as a separator, e.g. `"user"->"address"->"phone_number"`. The third return value is [true] if the field is found.
func (*Struct) GetNestedFields ¶ added in v0.9.0
GetNestedFields returns a list of all fields in the struct and its nested structs. Paths are dot-separated, e.g. "user.address.phoneNumber".
type StructField ¶
type StructField struct { // Link to the actual field definition. Each field stored once and shared between all structs. Field *Field // Shows if the field is anonymous (doesn't have name). Anonymous bool Embedded bool // Reflect path to the field in the struct. Usually there's only one index, but in case of anonymous structs, there can be more. Idx []int }
StructField represents a field instance in a particular struct.
func (*StructField) PostgreSQLType ¶
func (e *StructField) PostgreSQLType() string
func (*StructField) Value ¶
func (e *StructField) Value(v reflect.Value) reflect.Value
Value returns the reflect.Value of the field in the given struct. Accepts a struct reflect.Value.