types

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

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

View Source
var Cache = &cache{}

Cache is a global cache of struct meta information.

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

func (f *Field) CheckDecoderScope(context scopes.Context, scope scopes.Decoding) bool

CheckDecoderScope checks if the field can be decoded in the given context.

func (*Field) CheckEncoderScope

func (f *Field) CheckEncoderScope(context scopes.Context) bool

CheckEncoderScope checks if the field can be encoded in the given context.

func (*Field) ParseTag

func (f *Field) ParseTag(st reflect.StructTag)

ParseTag parses the struct tag and populates the field with the data.

type NestedField added in v0.9.0

type NestedField struct {
	Path   string
	GoPath string
	Field  *StructField
}

type Operation

type Operation int
const (
	OPERATION_ALL Operation = iota
	OPERATION_READ
	OPERATION_WRITE
	OPERATION_CREATE
	OPERATION_READ_CREATE
	OPERATION_UPDATE
	OPERATION_READ_UPDATE
	OPERATION_IGNORE
)

func (Operation) CanRead

func (s Operation) CanRead() bool

func (Operation) CanWrite

func (s Operation) CanWrite(scope scopes.Decoding) bool

func (Operation) String

func (s Operation) String() string

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

func (c *Struct) GetFieldDBPath(path string, sep string) (*StructField, string, bool)

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

func (c *Struct) GetNestedFields(ctx scopes.Context, decoding scopes.Decoding) []NestedField

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.

Jump to

Keyboard shortcuts

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