extid

package
v0.296.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractIdentifierField added in v0.261.0

func ExtractIdentifierField(ent any) (reflect.StructField, reflect.Value, bool)

func Lookup

func Lookup[ID, Ent any](ent Ent) (id ID, ok bool)
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"
}
Output:

func RegisterType

func RegisterType[ENT, ID any](
	Get func(ENT) ID,
	Set func(*ENT, ID),
) func()

func Set

func Set[ID any](ptr any, id ID) error
Example
package main

import (
	"fmt"

	"go.llib.dev/frameless/port/crud/extid"
)

type Entity struct {
	ID string `ext:"id"`
}

func main() {
	ent := Entity{}
	id := "id-value"

	if err := extid.Set(&ent, id); err != nil {
		panic(err)
	}

	fmt.Println(`ent.ID == id:`, ent.ID == id) // true
}
Output:

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.

func (Accessor[ENT, ID]) Get added in v0.296.0

func (fn Accessor[ENT, ID]) Get(ent ENT) ID

func (Accessor[ENT, ID]) Lookup added in v0.239.0

func (fn Accessor[ENT, ID]) Lookup(ent ENT) (ID, bool)

func (Accessor[ENT, ID]) ReflectLookup added in v0.261.0

func (fn Accessor[ENT, ID]) ReflectLookup(rENT reflect.Value) (rID reflect.Value, ok bool)

func (Accessor[ENT, ID]) ReflectSet added in v0.261.0

func (fn Accessor[ENT, ID]) ReflectSet(ptrENT reflect.Value, id reflect.Value) error

func (Accessor[ENT, ID]) Set added in v0.239.0

func (fn Accessor[ENT, ID]) Set(ent *ENT, id ID) error

type LookupIDFunc added in v0.242.0

type LookupIDFunc[ENT, ID any] func(ENT) (ID, bool)

type ReflectAccessor added in v0.261.0

type ReflectAccessor func(ptrENT reflect.Value) (ptrID reflect.Value)

func (ReflectAccessor) ReflectLookup added in v0.261.0

func (fn ReflectAccessor) ReflectLookup(rENT reflect.Value) (rID reflect.Value, ok bool)

func (ReflectAccessor) ReflectSet added in v0.261.0

func (fn ReflectAccessor) ReflectSet(ptrENT reflect.Value, id reflect.Value) (rErr error)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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