model

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MultiValueIndex

func MultiValueIndex(values ...string) string

func RegisterType

func RegisterType(typ Type, p proto.Message, fn func() Object) error

RegisterType handles mapping an object's type name to its model & an optional, generated Protobuf message. An error will be returned when the type is already registered.

func SetResource

func SetResource(o Object, r Resource) error

SetResource replaces the object's underlining resource with the provided resource.

func ValidType

func ValidType(typ Type) bool

Types

type Index

type Index struct {
	// Name of the Index.
	Name string
	// FieldName is the name of the field this constraint applies on.
	// This is used for annotating errors.
	// Use JSON path notation (foo.bar.baz) for nested fields.
	FieldName string
	// Type is the type of the Index.
	Type TypeIndex
	// ForeignType denotes the type of the foreign object.
	// This must be populated for IndexForeign and otherwise must be empty.
	ForeignType Type
	// Value is the value of the field for this Index.
	Value string
}

type Object

type Object interface {
	ID() string
	Type() Type
	Resource() Resource
	Validate(ctx context.Context) error
	Indexes() []Index
	ProcessDefaults(ctx context.Context) error

	// SetResource replaces the object's underlining resource with the provided resource.
	SetResource(Resource) error
}

func NewObject

func NewObject(typ Type) (Object, error)

func ObjectFromProto

func ObjectFromProto(p proto.Message) (Object, error)

ObjectFromProto returns the relevant model object from the given Protobuf message, while retaining all provided fields that are set. When the relevant object cannot be found, an error is returned.

type ObjectList

type ObjectList interface {
	Type() Type
	Add(Object)
	GetAll() []Object
	// GetTotalCount returns the count of objects in the underlying store across all pages.
	GetTotalCount() int
	SetTotalCount(count int)
	SetNextPage(pageNum int)
	GetNextPage() int
}

type ObjectWithResourceDTO

type ObjectWithResourceDTO interface {
	// MarshalResourceJSON is just like json.Marshaler, but specifically
	// for marshalling the resource for use in the persistence store.
	MarshalResourceJSON() ([]byte, error)

	// UnmarshalResourceJSON is just like json.Unmarshaler, but specifically for unmarshalling
	// a resource's JSON representation outputted by MarshalResourceJSON().
	UnmarshalResourceJSON([]byte) error
}

ObjectWithResourceDTO defines a model object that may not store the exact JSON-encoded representation of the underlining Protobuf resource in the persistence store. All callers will still work with the underlining Protobuf resource returned by Object.Resource(), however, when it comes time to store/fetch the resource from the persistence store, it will be translated to/from automatically.

A practical use-case of this interface is to ensure commonality across JSON keys, in the event such key needs to be indexed. For example, there could be `$.name` key that is an indexed value in the datastore, however on one resource, instead of calling it `name`, it's called `description`. These (un)marhsal methods can then be used to re-write that key to `name` only within the datastore, in order to prevent the need of creating another index.

Given the above example, another use-case is such key may be called the same, however, it may be within a nested object, e.g.: `$.metadata.name`. In that case, these (un)marhsal methods can then be used to rewrite that nested `name` field to be stored at the root of the JSON object, in order to take advantage of an already existing index.

type Registry

type Registry interface{}

type Resource

type Resource interface {
	proto.Message
}

type Type

type Type string

func AllTypes

func AllTypes() []Type

type TypeIndex

type TypeIndex string
const (
	IndexUnique  TypeIndex = "unique"
	IndexForeign TypeIndex = "foreign"
)

Directories

Path Synopsis
json
generator
Package generator provides utilities to generate a JSON-schema.
Package generator provides utilities to generate a JSON-schema.

Jump to

Keyboard shortcuts

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