pobj

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: MIT Imports: 8 Imported by: 2

README

GoDoc

pobj

Object library

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownType   = errors.New("pobj: unknown object type")
	ErrMissingAction = errors.New("pobj: no such action exists")
)

Functions

func ById added in v0.1.0

func ById[T any](ctx context.Context, id string) (*T, error)

func RegisterActions

func RegisterActions[T any](name string, actions *ObjectActions)

RegisterActions is used for static REST methods such as get (factory) and list. Methods such as update and delete require an object.

func RegisterStatic

func RegisterStatic(name string, fn any)

RegisterStatic adds a static method to an object

Types

type Object

type Object struct {
	Action *ObjectActions
	// contains filtered or unexported fields
}

func Get added in v0.1.0

func Get(name string) *Object

Get returns the Object matching the given name, or nil if no such object exists

func GetByType added in v0.1.0

func GetByType[T any]() *Object

GetByType returns the Object matching the type given on the command line

func Register

func Register[T any](name string) *Object

Register adds the given object to the registry of name-instanciable objects

func Root

func Root() *Object

Root returns the root object holder

func (*Object) ById added in v0.1.1

func (o *Object) ById(ctx context.Context, id string) (any, error)

func (*Object) Child

func (o *Object) Child(name string) *Object

func (*Object) New

func (o *Object) New() any

New returns a new instance of the given object

func (*Object) Static

func (o *Object) Static(name string) *StaticMethod

Static returns the given static method

func (*Object) String

func (o *Object) String() string

type ObjectActions

type ObjectActions struct {
	Fetch  *StaticMethod // Fetch action receives "id" and returns an instance (factory)
	List   *StaticMethod // List action returns a list of object
	Clear  *StaticMethod // Clear action deletes all objects and returns nothing
	Create *StaticMethod // Create action creates a new object and returns it
}

ObjectActions defines generic factories for usage in API calls

type StaticMethod

type StaticMethod struct {
	// contains filtered or unexported fields
}

func Static

func Static(method any) *StaticMethod

Static returns a StaticMethod object for a func that accepts a context.Context and/or a struct object that is its arguments.

func (*StaticMethod) Call

func (s *StaticMethod) Call(ctx context.Context) (any, error)

func (*StaticMethod) CallArg

func (s *StaticMethod) CallArg(ctx context.Context, arg any) (any, error)

Jump to

Keyboard shortcuts

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