field

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	ErrNilPointer    = fieldError("ptr to struct to be parsed by field spec is nil")
	ErrInvalidStruct = fieldError("field spec requires a pointer to a struct; invalid type")
)

Variables

This section is empty.

Functions

func AddReservedType

func AddReservedType(t string)

AddReservedType register a type as reserved

Example

ExampleAddReservedType registers a struct type that must be mapped as a single column when it is embedded, like time.Time. Register reserved types at start-up, before GetStructMeta first sees a struct that uses them: metadata is cached per type.

package main

import (
	"fmt"
	"reflect"

	"github.com/oddbit-project/gohan/field"
)

// Timestamp is a struct-based value type that a database driver handles
// itself, so it must be mapped as one column rather than flattened.
type Timestamp struct {
	unix int64
}

func main() {
	type Event struct {
		Timestamp
		Name string `db:"name"`
	}

	name := reflect.TypeOf(Timestamp{}).String()
	fmt.Println(name, field.IsReservedType(name))

	field.AddReservedType(name)
	fmt.Println(name, field.IsReservedType(name))

	meta, _ := field.GetStructMeta(reflect.TypeOf(Event{}))
	for _, m := range meta {
		fmt.Println(m.Name, m.DbName, m.TypeName)
	}
}
Output:
field_test.Timestamp false
field_test.Timestamp true
Timestamp timestamp field_test.Timestamp
Name name string

func GetReservedTypes

func GetReservedTypes() []string

GetReservedTypes get a copy of the list of reserved types

func IsReservedType

func IsReservedType(t string) bool

**

  • Check if a given type string is reserved
  • Reserved means the value is passed "as is"
  • This is a workaround to allow to pass struct-based types that are recognized by the driver

Types

type Metadata

type Metadata struct {
	Name       string `json:"fieldName"`  // actual field name
	DbName     string `json:"dbName"`     // database field name
	Alias      string `json:"fieldAlias"` // serialized name (json,xml, etc)
	Sortable   bool   `json:"sortable"`   // true if field is sortable
	Filterable bool   `json:"filterable"` // true if field is filterable
	Searchable bool   `json:"searchable"` // true if field is searchable
	Auto       bool   `json:"auto"`       // true if field is automatically generated
	OmitNil    bool   `json:"omitNil"`    // true if field is skipped on insert if nil
	OmitEmpty  bool   `json:"omitEmpty"`  // true if field is skipped on insert if empty
	TypeName   string `json:"typeName"`   // field type name (from reflect.Type.Name())
	Type       reflect.Type
	DbOptions  []string `json:"dbOptions"` // additional db options like goqu, etc
}

func GetStructMeta

func GetStructMeta(t reflect.Type) ([]Metadata, error)
Example

ExampleGetStructMeta reads the column mapping of a struct. Embedded structs are flattened, db:"-" excludes a field, and the alias comes from the alias, json or xml tag.

package main

import (
	"fmt"
	"reflect"

	"github.com/oddbit-project/gohan/field"
)

func main() {
	type Audit struct {
		CreatedBy string `db:"created_by"`
	}
	type Product struct {
		ID       int64   `db:"id" auto:"true"`
		Name     string  `db:"name" json:"title" grid:"sort,search"`
		Price    float64 `db:"price" grid:"sort,filter"`
		Discount *int    `db:"discount" goqu:"omitnil"`
		Secret   string  `db:"-"`
		Audit
	}

	meta, err := field.GetStructMeta(reflect.TypeOf(Product{}))
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, m := range meta {
		fmt.Printf("%-9s db=%-10s alias=%-9s auto=%-5v omitnil=%-5v sort=%-5v filter=%-5v search=%v\n",
			m.Name, m.DbName, m.Alias, m.Auto, m.OmitNil, m.Sortable, m.Filterable, m.Searchable)
	}
}
Output:
ID        db=id         alias=ID        auto=true  omitnil=false sort=false filter=false search=false
Name      db=name       alias=title     auto=false omitnil=false sort=true  filter=false search=true
Price     db=price      alias=Price     auto=false omitnil=false sort=true  filter=true  search=false
Discount  db=discount   alias=Discount  auto=false omitnil=true  sort=false filter=false search=false
CreatedBy db=created_by alias=CreatedBy auto=false omitnil=false sort=false filter=false search=false

Jump to

Keyboard shortcuts

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