models

package
v3.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2020 License: Apache-2.0 Imports: 12 Imported by: 25

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultRegistry represents a global registry for models.
	DefaultRegistry = NewRegistry()
)

Functions

func GetKey

func GetKey(x proto.Message) (string, error)

GetKey returns complete key for gived model, including key prefix defined by model specification. It returns error if given model is not registered.

func GetKeyForItem

func GetKeyForItem(item *api.Item) (string, error)

GetKeyForItem returns key for given item.

func GetName

func GetName(x proto.Message) (string, error)

GetName

func Key

func Key(x proto.Message) string

Key is a helper for the GetKey which panics on errors.

func MarshalItem

func MarshalItem(pb proto.Message) (*api.Item, error)

Marshal is helper function for marshalling model instance into item.

func Name

func Name(x proto.Message) string

Name is a helper for the GetName which panics on errors.

func UnmarshalItem

func UnmarshalItem(item *api.Item) (proto.Message, error)

Unmarshal is helper function for unmarshalling items.

Types

type KnownModel

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

KnownModel represents a registered model.

func GetModel

func GetModel(name string) (KnownModel, error)

GetModel returns registered model for given model name.

func GetModelFor

func GetModelFor(x proto.Message) (KnownModel, error)

GetModelFor returns model registered in DefaultRegistry for given proto message.

func GetModelForItem

func GetModelForItem(item *api.Item) (KnownModel, error)

GetModelForItem returns model for given item.

func GetModelForKey

func GetModelForKey(key string) (KnownModel, error)

GetModelForKey returns model registered in DefaultRegistry which matches key.

func Register

func Register(pb proto.Message, spec Spec, opts ...ModelOption) *KnownModel

Register registers model in DefaultRegistry.

func RegisteredModels

func RegisteredModels() []KnownModel

RegisteredModels returns models registered in the DefaultRegistry.

func (KnownModel) GoType

func (m KnownModel) GoType() string

GoType returns go type for the model.

func (KnownModel) IsKeyValid

func (m KnownModel) IsKeyValid(key string) bool

IsKeyValid returns true if given key is valid for this model.

func (KnownModel) KeyPrefix

func (m KnownModel) KeyPrefix() string

KeyPrefix returns key prefix for the model.

func (KnownModel) ModelDetail

func (m KnownModel) ModelDetail() *generic.ModelDetail

ModelDescriptor returns descriptor for the model.

func (KnownModel) Name

func (m KnownModel) Name() string

Path returns path for the model.

func (KnownModel) NameTemplate

func (m KnownModel) NameTemplate() string

NameTemplate returns name template for the model.

func (KnownModel) NewInstance

func (m KnownModel) NewInstance() proto.Message

NewInstance creates new instance value for model type.

func (KnownModel) ParseKey

func (m KnownModel) ParseKey(key string) (name string, valid bool)

ParseKey parses the given key and returns item name or returns empty name and valid as false if the key is not valid.

func (KnownModel) ProtoName

func (m KnownModel) ProtoName() string

ProtoName returns proto message name registered with the model.

func (KnownModel) Spec

func (m KnownModel) Spec() *Spec

Spec returns model specification for the model.

func (KnownModel) StripKeyPrefix

func (m KnownModel) StripKeyPrefix(key string) string

StripKeyPrefix returns key with prefix stripped.

type ModelOption

type ModelOption func(*modelOptions)

ModelOption defines function type which sets model options.

func WithNameTemplate

func WithNameTemplate(t string) ModelOption

WithNameTemplate returns option for models which sets function for generating name of instances using custom template.

type NameFunc

type NameFunc func(obj interface{}) (string, error)

NameFunc represents function which can name model instance.

func NameTemplate

func NameTemplate(t string) NameFunc

type Registry

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

Registry defines model registry for managing registered models.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns initialized Registry.

func (*Registry) GetModel

func (r *Registry) GetModel(name string) (KnownModel, error)

GetModel returns registered model for the given model name or error if model is not found.

func (*Registry) GetModelFor

func (r *Registry) GetModelFor(x interface{}) (KnownModel, error)

GetModelFor returns registered model for the given proto message.

func (*Registry) GetModelForKey

func (r *Registry) GetModelForKey(key string) (KnownModel, error)

GetModelForKey returns registered model for the given key or error.

func (*Registry) Register

func (r *Registry) Register(x interface{}, spec Spec, opts ...ModelOption) (*KnownModel, error)

Register registers a protobuf message with given model specification. If spec.Class is unset empty it defaults to 'config'.

func (*Registry) RegisteredModels

func (r *Registry) RegisteredModels() []KnownModel

RegisteredModels returns all registered modules.

type Spec

type Spec struct {
	Module  string
	Version string
	Type    string
	Class   string
}

Spec defines model specification used for registering model.

func ToSpec

func ToSpec(s *api.ModelSpec) Spec

func (Spec) KeyPrefix

func (spec Spec) KeyPrefix() string

func (Spec) ModelName

func (spec Spec) ModelName() string

func (Spec) Proto

func (spec Spec) Proto() *api.ModelSpec

func (Spec) Validate

func (spec Spec) Validate() error

Validate validates Spec fields.

Jump to

Keyboard shortcuts

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