models

package
v3.5.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2022 License: Apache-2.0 Imports: 22 Imported by: 25

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DynamicLocallyKnownMessageToGeneratedMessage added in v3.3.0

func DynamicLocallyKnownMessageToGeneratedMessage(dynamicMessage *dynamicpb.Message) (proto.Message, error)

DynamicLocallyKnownMessageToGeneratedMessage converts locally registered/known proto dynamic message to corresponding statically generated proto message. This function will fail when there is no registration of statically-generated proto message, i.e. dynamic message refers to remotely known model. This conversion method should help handling dynamic proto messages in mostly protoc-generated proto message oriented codebase (i.e. help for type conversions to named, help handle missing data fields as seen in generated proto messages,...)

func GetKey

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

GetKey returns complete key for given 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 GetKeyForItemUsingModelRegistry added in v3.3.0

func GetKeyForItemUsingModelRegistry(item *api.Item, modelRegistry Registry) (string, error)

GetKeyForItem returns key for given item (using given model registry)

func GetKeyUsingModelRegistry added in v3.3.0

func GetKeyUsingModelRegistry(message proto.Message, modelRegistry Registry) (string, error)

GetKeyUsingModelRegistry returns complete key for given model from given model registry, including key prefix defined by model specification. It returns error if given model is not registered.

func GetName

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

GetName returns instance name for given model. It returns error if given model is not registered.

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)

MarshalItem is helper function for marshalling model instance into item

func MarshalItemUsingModelRegistry added in v3.3.0

func MarshalItemUsingModelRegistry(pb proto.Message, modelRegistry Registry) (*api.Item, error)

MarshalItemUsingModelRegistry is helper function for marshalling model instance into item (using given model registry)

func Name

func Name(x proto.Message) string

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

func RegisterRemote added in v3.3.0

func RegisterRemote(remoteModel *ModelInfo, remoteRegistry *RemoteRegistry)

RegisterRemote registers remotely known model in given RemoteRegistry

func UnmarshalItem

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

UnmarshalItem is helper function for unmarshalling items.

func UnmarshalItemUsingModelRegistry added in v3.3.0

func UnmarshalItemUsingModelRegistry(item *api.Item, modelRegistry Registry) (proto.Message, error)

UnmarshalItemUsingModelRegistry is helper function for unmarshalling items (using given model registry)

Types

type KnownModel

type KnownModel interface {
	// Spec returns model specification for the model.
	Spec() *Spec

	// ModelDetail returns descriptor for the model.
	ModelDetail() *generic.ModelDetail

	// NewInstance creates new instance value for model type.
	NewInstance() proto.Message

	// ProtoName returns proto message name registered with the model.
	ProtoName() string

	// ProtoFile returns proto file name for the model.
	ProtoFile() string

	// NameTemplate returns name template for the model.
	NameTemplate() string

	// GoType returns go type for the model.
	GoType() string

	// LocalGoType returns reflect go type for the model. The reflect type can be retrieved only
	// for locally registered model that provide locally known go types. The remotely retrieved model
	// can't provide reflect type so if known model information is retrieved remotely, this method
	// will return nil.
	LocalGoType() reflect.Type

	// PkgPath returns package import path for the model definition.
	PkgPath() string

	// Name returns name for the model.
	Name() string

	// KeyPrefix returns key prefix for the model.
	KeyPrefix() string

	// ParseKey parses the given key and returns item name
	// or returns empty name and valid as false if the key is not valid.
	ParseKey(key string) (name string, valid bool)

	// IsKeyValid returns true if given key is valid for this model.
	IsKeyValid(key string) bool

	// StripKeyPrefix returns key with prefix stripped.
	StripKeyPrefix(key string) string

	// InstanceName computes message name for given proto message using name template (if present).
	InstanceName(x interface{}) (string, error)
}

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 GetModelFromModelRegistryForItem added in v3.3.0

func GetModelFromModelRegistryForItem(item *api.Item, modelRegistry Registry) (KnownModel, error)

GetModelFromModelRegistryForItem returns model for given item (using given model registry)

func GetModelFromRegistry added in v3.3.0

func GetModelFromRegistry(name string, modelRegistry Registry) (KnownModel, error)

GetModelFromRegistry returns registered model in given registry for given model name.

func GetModelFromRegistryFor added in v3.3.0

func GetModelFromRegistryFor(x proto.Message, modelRegistry Registry) (KnownModel, error)

GetModelFromRegistryFor returns model registered in modelRegistry for given proto message

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.

type LocalRegistry added in v3.3.0

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

LocalRegistry defines model registry for managing registered local models. Local models are locally compiled into the program binary and hence some additional information in compare to remote models, i.e. go type.

func NewRegistry

func NewRegistry() *LocalRegistry

NewRegistry returns initialized Registry.

func (*LocalRegistry) GetModel added in v3.3.0

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

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

func (*LocalRegistry) GetModelFor added in v3.3.0

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

GetModelFor returns registered model for the given proto message.

func (*LocalRegistry) GetModelForKey added in v3.3.0

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

GetModelForKey returns registered model for the given key or error.

func (*LocalRegistry) MessageTypeRegistry added in v3.3.0

func (r *LocalRegistry) MessageTypeRegistry() *protoregistry.Types

MessageTypeRegistry creates new message type registry from registered proto messages

func (*LocalRegistry) Register added in v3.3.0

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

Register registers either a protobuf message known at compile-time together with the given model specification, or a remote model represented by an instance of ModelInfo obtained via KnownModels RPC from MetaService. While the former case is prevalent, the latter option is useful for scenarios with multiple agents and configuration requests being proxied from one to another (remote model registered into LocalRegistry may act as a proxy for the agent from which it was learned). If spec.Class is unset then it defaults to 'config'.

func (*LocalRegistry) RegisteredModels added in v3.3.0

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

RegisteredModels returns all registered modules.

type LocallyKnownModel added in v3.3.0

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

LocallyKnownModel represents a registered local model (local model has go types compiled into program binary)

func (*LocallyKnownModel) GoType added in v3.3.0

func (m *LocallyKnownModel) GoType() string

GoType returns go type for the model.

func (*LocallyKnownModel) InstanceName added in v3.3.0

func (m *LocallyKnownModel) InstanceName(x interface{}) (string, error)

InstanceName computes message name for given proto message using name template (if present).

func (*LocallyKnownModel) IsKeyValid added in v3.3.0

func (m *LocallyKnownModel) IsKeyValid(key string) bool

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

func (*LocallyKnownModel) KeyPrefix added in v3.3.0

func (m *LocallyKnownModel) KeyPrefix() string

KeyPrefix returns key prefix for the model.

func (*LocallyKnownModel) LocalGoType added in v3.3.0

func (m *LocallyKnownModel) LocalGoType() reflect.Type

LocalGoType returns reflect go type for the model.

func (*LocallyKnownModel) ModelDetail added in v3.3.0

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

ModelDetail returns descriptor for the model.

func (*LocallyKnownModel) Name added in v3.3.0

func (m *LocallyKnownModel) Name() string

Name returns name for the model.

func (*LocallyKnownModel) NameTemplate added in v3.3.0

func (m *LocallyKnownModel) NameTemplate() string

NameTemplate returns name template for the model.

func (*LocallyKnownModel) NewInstance added in v3.3.0

func (m *LocallyKnownModel) NewInstance() proto.Message

NewInstance creates new instance value for model type.

func (*LocallyKnownModel) ParseKey added in v3.3.0

func (m *LocallyKnownModel) 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 (*LocallyKnownModel) PkgPath added in v3.3.0

func (m *LocallyKnownModel) PkgPath() string

PkgPath returns package import path for the model definition.

func (*LocallyKnownModel) ProtoFile added in v3.3.0

func (m *LocallyKnownModel) ProtoFile() string

ProtoFile returns proto file name for the model.

func (*LocallyKnownModel) ProtoName added in v3.3.0

func (m *LocallyKnownModel) ProtoName() string

ProtoName returns proto message name registered with the model.

func (*LocallyKnownModel) Spec added in v3.3.0

func (m *LocallyKnownModel) Spec() *Spec

Spec returns model specification for the model.

func (*LocallyKnownModel) StripKeyPrefix added in v3.3.0

func (m *LocallyKnownModel) StripKeyPrefix(key string) string

StripKeyPrefix returns key with prefix stripped.

type ModelInfo added in v3.3.0

type ModelInfo struct {
	*generic.ModelDetail

	// MessageDescriptor is the proto message descriptor of the message represented by this ModelInfo struct
	MessageDescriptor protoreflect.MessageDescriptor
}

ModelInfo represents model information retrieved using meta service

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 interface {
	// GetModel returns registered model for the given model name
	// or error if model is not found.
	GetModel(name string) (KnownModel, error)

	// GetModelFor returns registered model for the given proto message.
	GetModelFor(x interface{}) (KnownModel, error)

	// GetModelForKey returns registered model for the given key or error.
	GetModelForKey(key string) (KnownModel, error)

	// MessageTypeRegistry creates new message type registry from registered proto messages
	MessageTypeRegistry() *protoregistry.Types

	// RegisteredModels returns all registered modules.
	RegisteredModels() []KnownModel

	// Register registers either a protobuf message known at compile-time together
	// with the given model specification (for LocalRegistry),
	// or a remote model represented by an instance of ModelInfo obtained via KnownModels RPC from MetaService
	// (for RemoteRegistry or also for LocalRegistry but most likely just proxied to a remote agent).
	// If spec.Class is unset, then it defaults to 'config'.
	Register(x interface{}, spec Spec, opts ...ModelOption) (KnownModel, error)
}

Registry defines model registry for managing registered models

var (
	// DefaultRegistry represents a global registry for local models (models known in compile time)
	DefaultRegistry Registry = NewRegistry()
)

type RemoteRegistry added in v3.3.0

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

RemoteRegistry defines model registry for managing registered remote models. The remote model have no included compiled code in program binary so only information available are from remote sources (i.e. generic.Client's known models)

func NewRemoteRegistry added in v3.3.0

func NewRemoteRegistry() *RemoteRegistry

NewRemoteRegistry returns initialized RemoteRegistry.

func (*RemoteRegistry) GetModel added in v3.3.0

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

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

func (*RemoteRegistry) GetModelFor added in v3.3.0

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

GetModelFor returns registered model for the given proto message.

func (*RemoteRegistry) GetModelForKey added in v3.3.0

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

GetModelForKey returns registered model for the given key or error.

func (*RemoteRegistry) MessageTypeRegistry added in v3.3.0

func (r *RemoteRegistry) MessageTypeRegistry() *protoregistry.Types

MessageTypeRegistry creates new message type registry from registered proto messages

func (*RemoteRegistry) Register added in v3.3.0

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

Register registers remote model ModelInfo (given as interface{} for common register interface flexibility). The given spec and options are already in ModelInfo and therefore these input arguments are ignored.

func (*RemoteRegistry) RegisteredModels added in v3.3.0

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

RegisteredModels returns all registered modules.

type RemotelyKnownModel added in v3.3.0

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

RemotelyKnownModel represents a registered remote model (remote model has only information about model from remote source, i.e. missing go type because VPP-Agent meta service doesn't provide it)

func (*RemotelyKnownModel) GoType added in v3.3.0

func (m *RemotelyKnownModel) GoType() string

GoType returns go type for the model.

func (*RemotelyKnownModel) InstanceName added in v3.3.0

func (m *RemotelyKnownModel) InstanceName(x interface{}) (string, error)

InstanceName computes message name for given proto message using name template (if present).

func (*RemotelyKnownModel) IsKeyValid added in v3.3.0

func (m *RemotelyKnownModel) IsKeyValid(key string) bool

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

func (*RemotelyKnownModel) KeyPrefix added in v3.3.0

func (m *RemotelyKnownModel) KeyPrefix() string

KeyPrefix returns key prefix for the model.

func (*RemotelyKnownModel) LocalGoType added in v3.3.0

func (m *RemotelyKnownModel) LocalGoType() reflect.Type

LocalGoType should returns reflect go type for the model, but remotely known model doesn't have locally known reflect go type. It always returns nil.

func (*RemotelyKnownModel) ModelDetail added in v3.3.0

func (m *RemotelyKnownModel) ModelDetail() *api.ModelDetail

ModelDetail returns descriptor for the model.

func (*RemotelyKnownModel) Name added in v3.3.0

func (m *RemotelyKnownModel) Name() string

Name returns name for the model.

func (*RemotelyKnownModel) NameTemplate added in v3.3.0

func (m *RemotelyKnownModel) NameTemplate() string

NameTemplate returns name template for the model.

func (*RemotelyKnownModel) NewInstance added in v3.3.0

func (m *RemotelyKnownModel) NewInstance() proto.Message

NewInstance creates new instance value for model type. Due to missing go type for remote models, the created instance won't have the same go type as in case of local models, but dynamic proto message's go type (the proto descriptor will be the same).

func (*RemotelyKnownModel) ParseKey added in v3.3.0

func (m *RemotelyKnownModel) 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 (*RemotelyKnownModel) PkgPath added in v3.3.0

func (m *RemotelyKnownModel) PkgPath() string

PkgPath returns package import path for the model definition.

func (*RemotelyKnownModel) ProtoFile added in v3.3.0

func (m *RemotelyKnownModel) ProtoFile() string

ProtoFile returns proto file name for the model.

func (*RemotelyKnownModel) ProtoName added in v3.3.0

func (m *RemotelyKnownModel) ProtoName() string

ProtoName returns proto message name registered with the model.

func (*RemotelyKnownModel) Spec added in v3.3.0

func (m *RemotelyKnownModel) Spec() *Spec

Spec returns model specification for the model.

func (*RemotelyKnownModel) StripKeyPrefix added in v3.3.0

func (m *RemotelyKnownModel) StripKeyPrefix(key string) string

StripKeyPrefix returns key with prefix stripped.

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