Documentation
¶
Index ¶
- Constants
- func ExtractIdentifierField(ent any) (reflect.StructField, reflect.Value, bool)
- func Get[ID, ENT any](ent ENT) ID
- func Lookup[ID, ENT any](ent ENT) (id ID, ok bool)
- func RegisterType[ENT, ID any](Get func(ENT) ID, Set func(*ENT, ID)) func()
- func Set[ID any](ptr any, id ID) error
- type Accessor
- func (fn Accessor[ENT, ID]) Get(ent ENT) ID
- func (fn Accessor[ENT, ID]) Lookup(ent ENT) (ID, bool)
- func (fn Accessor[ENT, ID]) ReflectLookup(rENT reflect.Value) (rID reflect.Value, ok bool)
- func (fn Accessor[ENT, ID]) ReflectSet(ptrENT reflect.Value, id reflect.Value) error
- func (fn Accessor[ENT, ID]) Set(ent *ENT, id ID) error
- type LookupIDFunc
- type ReflectAccessor
Examples ¶
Constants ¶
const ErrIDFieldNotFound errorkit.Error = "ErrIDFieldNotFound"
Variables ¶
This section is empty.
Functions ¶
func ExtractIdentifierField ¶ added in v0.261.0
func Lookup ¶
Lookup checks if the given ENT struct type contains a field of type ID.
It returns two values: - The ID field, if found. - A boolean OK indicating whether an ID field exists in the given ENT struct.
The function prioritises the following when selecting an ID field: - Any field of type ID. - If multiple fields of type ID exist, the one tagged as 'ext:"id"' is preferred.
This function helps identify the primary ID field in ENT structs consistently.
Example ¶
package main import ( "fmt" "go.llib.dev/frameless/port/crud/extid" ) type Entity struct { ID string `ext:"id"` } func main() { ent := Entity{} _, ok := extid.Lookup[string](ent) fmt.Println(`found:`, ok) // false ent.ID = `42` id, ok := extid.Lookup[string](ent) fmt.Println(`found:`, ok) // true fmt.Println(`id value:`, id) // "42" }
func RegisterType ¶
func RegisterType[ENT, ID any]( Get func(ENT) ID, Set func(*ENT, ID), ) func()
Types ¶
type Accessor ¶ added in v0.239.0
type Accessor[ENT, ID any] func(*ENT) *ID
Accessor is a function that allows describing how to access an ID field in an ENT type. The returned id pointer will be used to Lookup its value, or to set new value to this ID pointer. Its functions will panic if func is provided, but it returns a nil pointer, as it is considered as implementation error.
Example implementation:
extid.Accessor[Foo, FooID](func(v Foo) *FooID { return &v.ID })
default: extid.Lookup, extid.Set, which will use either the `ext:"id"` tag, or the `ENT.ID()` & `ENT.SetID()` methods.