mapping

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2022 License: MIT Imports: 15 Imported by: 5

README

State mappings

Primary key

Primary key based on one or multiple fields
Entity (schema) as primary keyer

Additional indexes (keys)

Unique key
Unique Key with multiple values

Documentation

Index

Constants

View Source
const KeyRefNamespace = `_idx`

KeyRefNamespace namespace for uniq indexes

View Source
const (
	TimestampKeyLayout = `2006-01-02`
)

Variables

View Source
var (
	// ErrEntryTypeNotSupported entry type has no appropriate mapper type
	ErrEntryTypeNotSupported = errors.New(`entry type not supported for mapping`)

	// ErrStateMappingNotFound occurs when mapping for state entry is not defined
	ErrStateMappingNotFound = errors.New(`state mapping not found`)

	// ErrEventMappingNotFound occurs when mapping for event is not defined
	ErrEventMappingNotFound = errors.New(`event mapping not found`)

	// ErrFieldTypeNotSupportedForKeyExtraction key cannot extracted from field
	ErrFieldTypeNotSupportedForKeyExtraction = errors.New(`field type not supported for key extraction`)

	ErrMappingUniqKeyExists = errors.New(`mapping uniq key exists`)

	ErrFieldNotExists         = errors.New(`field is not exists`)
	ErrPrimaryKeyerNotDefined = errors.New(`primary keyer is not defined`)

	// ErrIndexAlreadyExists occurs when when trying to add index to mapping with existent name
	ErrIndexAlreadyExists = errors.New(`index already exists`)

	// ErrIndexReferenceNotFound occurs when trying to find entry by index
	ErrIndexReferenceNotFound = errors.New(`index reference not found`)
)
View Source
var (
	DefaultSerializer = &state.ProtoSerializer{}
)
View Source
var (
	ErrEventNameNotFound = errors.New(`event name not found`)
)
View Source
var (
	ErrEventPayloadEmpty = errors.New(`event payload empty`)
)
View Source
var KeyRefIDKeyer = attrsKeyer([]string{`Schema`, `Idx`, `RefKey`})

KeyRefIDKeyer keyer for KeyRef entity

View Source
var KeyRefIDMapper = &StateMapping{
	schema:       &schema.KeyRefId{},
	namespace:    state.Key{KeyRefNamespace},
	primaryKeyer: KeyRefIDKeyer,
}
View Source
var KeyRefMapper = &StateMapping{
	schema:       &schema.KeyRef{},
	namespace:    state.Key{KeyRefNamespace},
	primaryKeyer: KeyRefIDKeyer,
}

Functions

func EventNameForPayload added in v0.8.1

func EventNameForPayload(payload interface{}) string

func KeyRefsDiff added in v0.6.8

func KeyRefsDiff(prevKeys []state.KeyValue, newKeys []state.KeyValue) (deleted, inserted []state.KeyValue, err error)

KeyRefsDiff calculates diff between key reference set

func MapEvents

func MapEvents(eventMappings EventMappings) router.MiddlewareFunc

func MapStates

func MapStates(stateMappings StateMappings) router.MiddlewareFunc

func NewKeyRef added in v0.4.3

func NewKeyRef(target interface{}, idx string, refKey, pKey state.Key) *schema.KeyRef

func NewKeyRefID added in v0.4.5

func NewKeyRefID(target interface{}, idx string, refKey state.Key) *schema.KeyRefId

func SchemaNamespace added in v0.5.4

func SchemaNamespace(schema interface{}) state.Key

SchemaNamespace produces string representation of Schema type

Types

type Command added in v0.8.1

type Command interface {
	Execute(state.State) error
	fmt.Stringer
}

type CommandInsert added in v0.8.1

type CommandInsert struct {
	Entry interface{}
}

func (*CommandInsert) Execute added in v0.8.1

func (ci *CommandInsert) Execute(state state.State) error

func (*CommandInsert) String added in v0.8.1

func (ci *CommandInsert) String() string

type CommandPut added in v0.8.1

type CommandPut struct {
	Entry interface{}
}

func (*CommandPut) Execute added in v0.8.1

func (ci *CommandPut) Execute(state state.State) error

func (*CommandPut) String added in v0.8.1

func (ci *CommandPut) String() string

type Commands added in v0.8.1

type Commands []Command

func (*Commands) Insert added in v0.8.1

func (cc *Commands) Insert(entry interface{}) *Commands

func (*Commands) Put added in v0.8.1

func (cc *Commands) Put(entry interface{}) *Commands

type EntryMapper added in v0.8.1

type EntryMapper struct {
	Commands Commands
	Event    *Event
}

func NewEntryMapper added in v0.8.1

func NewEntryMapper() *EntryMapper

func (*EntryMapper) Apply added in v0.8.1

func (em *EntryMapper) Apply(state state.State, event state.Event) error

type Event added in v0.8.1

type Event struct {
	Name    string
	Payload interface{}
}

func EventFromPayload added in v0.8.1

func EventFromPayload(payload interface{}) *Event

type EventImpl

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

func NewEvent

func NewEvent(stub shim.ChaincodeStubInterface, mappings EventMappings) *EventImpl

func WrapEvent added in v0.5.3

func WrapEvent(event state.Event, mappings EventMappings) *EventImpl

func (*EventImpl) Set

func (e *EventImpl) Set(entry interface{}, value ...interface{}) error

func (*EventImpl) UseNameTransformer

func (e *EventImpl) UseNameTransformer(nt state.StringTransformer) state.Event

func (*EventImpl) UseSetTransformer

func (e *EventImpl) UseSetTransformer(tb state.ToBytesTransformer) state.Event

type EventInstance added in v0.9.1

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

func NewEventInstance added in v0.9.1

func NewEventInstance(instance interface{}, eventMapper EventMapper, serializer state.Serializer) (*EventInstance, error)

func (EventInstance) Name added in v0.9.1

func (ei EventInstance) Name() (string, error)

func (EventInstance) ToBytes added in v0.9.1

func (ei EventInstance) ToBytes() ([]byte, error)

type EventMapped added in v0.4.3

type EventMapped interface {
	state.NameValue
}

type EventMapper

type EventMapper interface {
	Schema() interface{}
	Name(instance interface{}) (string, error)
}

type EventMappers

type EventMappers interface {
	Exists(schema interface{}) (exists bool)
	Map(schema interface{}) (keyValue state.KeyValue, err error)
	Get(schema interface{}) (eventMapper EventMapper, err error)
}

type EventMapping

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

func (EventMapping) Name

func (em EventMapping) Name(instance interface{}) (string, error)

func (EventMapping) Schema

func (em EventMapping) Schema() interface{}

type EventMappingOpt

type EventMappingOpt func(*EventMapping)

type EventMappings

type EventMappings map[string]*EventMapping

func MergeEventMappings added in v0.10.1

func MergeEventMappings(one EventMappings, more ...EventMappings) EventMappings

func (EventMappings) Add

func (emm EventMappings) Add(schema interface{}, opts ...EventMappingOpt) EventMappings

func (EventMappings) Exists

func (emm EventMappings) Exists(entry interface{}) bool

func (EventMappings) Get

func (emm EventMappings) Get(entry interface{}) (EventMapper, error)

func (EventMappings) Map

func (emm EventMappings) Map(entry interface{}) (instance *EventInstance, err error)

func (EventMappings) Resolve added in v0.9.1

func (emm EventMappings) Resolve(eventName string, payload []byte) (event interface{}, err error)

type EventResolver added in v0.10.0

type EventResolver interface {
	Resolve(eventName string, payload []byte) (event interface{}, err error)
}

type Impl added in v0.4.5

type Impl struct {
	state.State
	// contains filtered or unexported fields
}

func WrapState

func WrapState(s state.State, mappings StateMappings) *Impl

func (*Impl) Delete added in v0.4.5

func (s *Impl) Delete(entry interface{}) error

func (*Impl) DeletePrivate added in v0.5.0

func (s *Impl) DeletePrivate(collection string, entry interface{}) (err error)

func (*Impl) Exists added in v0.4.5

func (s *Impl) Exists(entry interface{}) (bool, error)

func (*Impl) ExistsPrivate added in v0.5.0

func (s *Impl) ExistsPrivate(collection string, entry interface{}) (exists bool, err error)

func (*Impl) Get added in v0.4.5

func (s *Impl) Get(entry interface{}, target ...interface{}) (interface{}, error)

func (*Impl) GetByKey added in v0.6.8

func (s *Impl) GetByKey(
	entry interface{}, idx string, idxVal []string, target ...interface{}) (result interface{}, err error)

func (*Impl) GetByUniqKey added in v0.4.5

func (s *Impl) GetByUniqKey(
	entry interface{}, idx string, idxVal []string, target ...interface{}) (result interface{}, err error)

func (*Impl) GetHistory added in v0.4.5

func (s *Impl) GetHistory(entry interface{}, target interface{}) (state.HistoryEntryList, error)

func (*Impl) GetPrivate added in v0.5.0

func (s *Impl) GetPrivate(collection string, entry interface{}, target ...interface{}) (result interface{}, err error)

func (*Impl) Insert added in v0.4.5

func (s *Impl) Insert(entry interface{}, value ...interface{}) error

func (*Impl) InsertPrivate added in v0.5.0

func (s *Impl) InsertPrivate(collection string, entry interface{}, value ...interface{}) (err error)

func (*Impl) List added in v0.4.5

func (s *Impl) List(entry interface{}, target ...interface{}) (interface{}, error)

func (*Impl) ListPaginated added in v0.7.7

func (s *Impl) ListPaginated(entry interface{}, pageSize int32, bookmark string, target ...interface{}) (
	interface{}, *pb.QueryResponseMetadata, error)

func (*Impl) ListPaginatedWith added in v0.7.7

func (s *Impl) ListPaginatedWith(
	schema interface{}, key state.Key, pageSize int32, bookmark string) (
	result interface{}, metadata *pb.QueryResponseMetadata, err error)

func (*Impl) ListPrivate added in v0.5.0

func (s *Impl) ListPrivate(collection string, usePrivateDataIterator bool, namespace interface{}, target ...interface{}) (result interface{}, err error)

func (*Impl) ListWith added in v0.4.5

func (s *Impl) ListWith(entry interface{}, key state.Key) (result interface{}, err error)

func (*Impl) Logger added in v0.4.5

func (s *Impl) Logger() *zap.Logger

func (*Impl) MappingNamespace added in v0.4.5

func (s *Impl) MappingNamespace(schema interface{}) (state.Key, error)

func (*Impl) Put added in v0.4.5

func (s *Impl) Put(entry interface{}, value ...interface{}) error

func (*Impl) PutPrivate added in v0.5.0

func (s *Impl) PutPrivate(collection string, entry interface{}, value ...interface{}) (err error)

func (*Impl) UseKeyTransformer added in v0.4.5

func (s *Impl) UseKeyTransformer(kt state.KeyTransformer) state.State

type InstanceKeyer

type InstanceKeyer func(instance interface{}) (state.Key, error)

InstanceKeyer returns key of an state entry instance

type InstanceMultiKeyer added in v0.6.8

type InstanceMultiKeyer func(instance interface{}) ([]state.Key, error)

type MappedState

type MappedState interface {
	state.State

	// ListWith allows to refine search criteria by adding to namespace key parts
	ListWith(schema interface{}, key state.Key) (result interface{}, err error)

	// ListPaginatedWith allows to refine search criteria by adding to namespace key parts with pagination
	ListPaginatedWith(schema interface{}, key state.Key, pageSize int32, bookmark string) (
		result interface{}, metadata *pb.QueryResponseMetadata, err error)

	// GetByUniqKey return one entry
	// Deprecated: use GetByKey
	GetByUniqKey(schema interface{}, idx string, idxVal []string, target ...interface{}) (result interface{}, err error)

	// GetByKey
	GetByKey(schema interface{}, idx string, idxVal []string, target ...interface{}) (result interface{}, err error)
}

type Namer

type Namer func(entity interface{}) string

type Query added in v0.8.1

type Query interface {
	Query(state.State) error
}

type StateIndex added in v0.6.8

type StateIndex struct {
	Name     string
	Uniq     bool
	Required bool
	Keyer    InstanceMultiKeyer // index can have multiple keys
}

StateIndex additional index of entity instance

type StateIndexDef added in v0.6.8

type StateIndexDef struct {
	Name     string
	Fields   []string
	Required bool
	Multi    bool
	Keyer    InstanceMultiKeyer
}

StateIndexDef additional index definition

type StateInstance added in v0.9.1

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

func NewKeyRefIDInstance added in v0.9.1

func NewKeyRefIDInstance(target interface{}, idx string, refKey state.Key) *StateInstance

func NewKeyRefInstance added in v0.9.1

func NewKeyRefInstance(target interface{}, idx string, refKey, pKey state.Key) *StateInstance

func NewStateInstance added in v0.9.1

func NewStateInstance(instance interface{}, stateMapper StateMapper, serializer state.Serializer) *StateInstance

func (*StateInstance) Key added in v0.9.1

func (si *StateInstance) Key() (state.Key, error)

func (*StateInstance) Keys added in v0.9.1

func (si *StateInstance) Keys() ([]state.KeyValue, error)

func (*StateInstance) Mapper added in v0.9.1

func (si *StateInstance) Mapper() StateMapper

func (*StateInstance) ToBytes added in v0.9.1

func (si *StateInstance) ToBytes() ([]byte, error)

type StateMapper

type StateMapper interface {
	Schema() interface{}
	List() interface{}
	Namespace() state.Key
	// PrimaryKey returns primary key for entry
	PrimaryKey(instance interface{}) (key state.Key, err error)
	// Keys returns additional keys for
	Keys(instance interface{}) (key []state.KeyValue, err error)
	//KeyerFor returns target entity if mapper is key mapper
	KeyerFor() (schema interface{})
	Indexes() []*StateIndex
}

StateMapper interface for dealing with mapped state

type StateMappers

type StateMappers interface {
	Exists(schema interface{}) (exists bool)
	Map(schema interface{}) (keyValue state.KeyValue, err error)
	Get(schema interface{}) (stateMapper StateMapper, err error)
	PrimaryKey(schema interface{}) (key state.Key, err error)
}

StateMappers interface for mappers collection

type StateMapping

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

StateMapping defines metadata for mapping from schema to state keys/values

func (*StateMapping) AddIndex added in v0.6.8

func (sm *StateMapping) AddIndex(idx *StateIndex) error

func (*StateMapping) DefaultNamespace added in v0.7.8

func (sm *StateMapping) DefaultNamespace() state.Key

func (*StateMapping) Index added in v0.6.8

func (sm *StateMapping) Index(name string) *StateIndex

func (*StateMapping) Indexes added in v0.6.8

func (sm *StateMapping) Indexes() []*StateIndex

func (*StateMapping) KeyerFor added in v0.6.3

func (sm *StateMapping) KeyerFor() interface{}

func (*StateMapping) Keys added in v0.4.3

func (sm *StateMapping) Keys(entity interface{}) ([]state.KeyValue, error)

Keys prepares primary and additional uniq/non-uniq keys for storage

func (*StateMapping) List added in v0.4.2

func (sm *StateMapping) List() interface{}

func (*StateMapping) Namespace

func (sm *StateMapping) Namespace() state.Key

func (*StateMapping) PrimaryKey

func (sm *StateMapping) PrimaryKey(entity interface{}) (state.Key, error)

func (*StateMapping) Schema

func (sm *StateMapping) Schema() interface{}

type StateMappingOpt

type StateMappingOpt func(*StateMapping, StateMappings)

func KeyerFor added in v0.6.3

func KeyerFor(schema interface{}) StateMappingOpt

func List added in v0.4.2

func List(list proto.Message) StateMappingOpt

List defined list container, it must have `Items` attr

func PKeyAttr

func PKeyAttr(attrs ...string) StateMappingOpt

func PKeyComplexId added in v0.4.2

func PKeyComplexId(pkeySchema interface{}) StateMappingOpt

PKeyComplexId sets Id as key field, also adds mapping for pkeySchema with namespace from mapping schema

func PKeyConst added in v0.5.5

func PKeyConst(key state.Key) StateMappingOpt

PKeyConst use constant as state entry key

func PKeyId

func PKeyId() StateMappingOpt

PKeyId use Id attr as source for mapped state entry key

func PKeySchema

func PKeySchema(pkeySchema interface{}) StateMappingOpt

PKeySchema registers all fields from pkeySchema as part of primary key. Same fields should exists in mapped entity. Also register keyer for pkeySchema with with namespace from current schema.

func PKeyer

func PKeyer(pkeyer InstanceKeyer) StateMappingOpt

func UniqKey added in v0.4.3

func UniqKey(name string, fields ...[]string) StateMappingOpt

UniqKey defined uniq key in entity

func WithConstPKey added in v0.7.8

func WithConstPKey(keys ...state.Key) StateMappingOpt

WithStaticPKey set static key for all instances of mapped entry

func WithIndex added in v0.6.8

func WithIndex(idx *StateIndexDef) StateMappingOpt

func WithNamespace added in v0.7.8

func WithNamespace(namespace state.Key) StateMappingOpt

WithNamespace sets namespace for mapping

type StateMappings

type StateMappings map[string]*StateMapping

func MergeStateMappings added in v0.10.1

func MergeStateMappings(one StateMappings, more ...StateMappings) StateMappings

func (StateMappings) Add

func (smm StateMappings) Add(schema interface{}, opts ...StateMappingOpt) StateMappings

func (StateMappings) Exists

func (smm StateMappings) Exists(entry interface{}) bool

func (StateMappings) Get

func (smm StateMappings) Get(entry interface{}) (StateMapper, error)

Get mapper for mapped entry

func (StateMappings) GetByNamespace added in v0.4.5

func (smm StateMappings) GetByNamespace(namespace state.Key) (StateMapper, error)

GetByNamespace returns mapper by string namespace. It can be used in block explorer: we know state key, but don't know type actually mapped to state

func (StateMappings) GetBySchema added in v0.7.8

func (smm StateMappings) GetBySchema(schema interface{}) (StateMapper, error)

func (*StateMappings) IdxKey added in v0.5.4

func (smm *StateMappings) IdxKey(entity interface{}, idx string, idxVal state.Key) (state.Key, error)

func (StateMappings) Map

func (smm StateMappings) Map(entry interface{}) (instance *StateInstance, err error)

func (StateMappings) PrimaryKey added in v0.4.3

func (smm StateMappings) PrimaryKey(entry interface{}) (pkey state.Key, err error)

func (StateMappings) Resolve added in v0.10.1

func (smm StateMappings) Resolve(objectType string, value []byte) (entry interface{}, err error)

Jump to

Keyboard shortcuts

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