entity

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2022 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const UnsetCombinedId = CombinedId("unset")
View Source
const UnsetId = Id("unset")

Variables

This section is empty.

Functions

func IsErrMultipleMatch

func IsErrMultipleMatch(err error) bool

func Resolve added in v0.8.0

func Resolve[T Interface](rs Resolvers, id Id) (T, error)

Resolve use the appropriate sub-resolver for the given type and find the Entity matching the Id.

func SeparateIds added in v0.8.0

func SeparateIds(prefix string) (primaryPrefix string, secondaryPrefix string)

SeparateIds extract primary and secondary prefix from an arbitrary length prefix of an Id created with CombineIds.

Types

type CachedResolver added in v0.8.0

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

CachedResolver is a resolver ensuring that loading is done only once through another Resolver.

func NewCachedResolver added in v0.8.0

func NewCachedResolver(resolver Resolver) *CachedResolver

func (*CachedResolver) Resolve added in v0.8.0

func (c *CachedResolver) Resolve(id Id) (Interface, error)

type CombinedId added in v0.8.0

type CombinedId string

CombinedId is an Id holding information from both a primary Id and a secondary Id. While it looks like a regular Id, do not just cast from one to another. Instead, use CombineIds and SeparateIds to create it and split it.

func CombineIds added in v0.8.0

func CombineIds(primary Id, secondary Id) CombinedId

CombineIds compute a merged Id holding information from both the primary Id and the secondary Id.

This allows to later find efficiently a secondary element because we can access the primary one directly instead of searching for a primary that has a secondary matching the Id.

An example usage is Comment in a Bug. The interleaved Id will hold part of the Bug Id and part of the Comment Id.

To allow the use of an arbitrary length prefix of this Id, Ids from primary and secondary are interleaved with this irregular pattern to give the best chance to find the secondary even with a 7 character prefix.

Format is: PSPSPSPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPP

A complete interleaved Id hold 50 characters for the primary and 14 for the secondary, which give a key space of 36^50 for the primary (~6 * 10^77) and 36^14 for the secondary (~6 * 10^21). This asymmetry assumes a reasonable number of secondary within a primary Entity, while still allowing for a vast key space for the primary (that is, a globally merged database) with a low risk of collision.

Here is the breakdown of several common prefix length:

5: 3P, 2S 7: 4P, 3S 10: 6P, 4S 16: 11P, 5S

func (CombinedId) HasPrefix added in v0.8.0

func (ci CombinedId) HasPrefix(prefix string) bool

func (CombinedId) Human added in v0.8.0

func (ci CombinedId) Human() string

Human return the identifier, shortened for human consumption

func (CombinedId) MarshalGQL added in v0.8.0

func (ci CombinedId) MarshalGQL(w io.Writer)

MarshalGQL implement the Marshaler interface for gqlgen

func (CombinedId) PrimaryPrefix added in v0.8.0

func (ci CombinedId) PrimaryPrefix() string

PrimaryPrefix is a helper to extract the primary prefix. If practical, use SeparateIds instead.

func (CombinedId) SecondaryPrefix added in v0.8.0

func (ci CombinedId) SecondaryPrefix() string

SecondaryPrefix is a helper to extract the secondary prefix. If practical, use SeparateIds instead.

func (CombinedId) String added in v0.8.0

func (ci CombinedId) String() string

String return the identifier as a string

func (*CombinedId) UnmarshalGQL added in v0.8.0

func (ci *CombinedId) UnmarshalGQL(v interface{}) error

UnmarshalGQL implement the Unmarshaler interface for gqlgen

func (CombinedId) Validate added in v0.8.0

func (ci CombinedId) Validate() error

Validate tell if the Id is valid

type ErrInvalidFormat added in v0.8.0

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

func NewErrInvalidFormat added in v0.8.0

func NewErrInvalidFormat(version uint, expected uint) *ErrInvalidFormat

func NewErrUnknownFormat added in v0.8.0

func NewErrUnknownFormat(expected uint) *ErrInvalidFormat

func (ErrInvalidFormat) Error added in v0.8.0

func (e ErrInvalidFormat) Error() string

type ErrMultipleMatch

type ErrMultipleMatch struct {
	Matching []Id
	// contains filtered or unexported fields
}

func NewErrMultipleMatch

func NewErrMultipleMatch(entityType string, matching []Id) *ErrMultipleMatch

func (ErrMultipleMatch) Error

func (e ErrMultipleMatch) Error() string

type Id

type Id string

Id is an identifier for an entity or part of an entity

func DeriveId added in v0.8.0

func DeriveId(data []byte) Id

DeriveId generate an Id from the serialization of the object or part of the object.

func RefToId added in v0.8.0

func RefToId(ref string) Id

RefToId parse a git reference and return the corresponding Entity's Id.

func RefsToIds added in v0.8.0

func RefsToIds(refs []string) []Id

RefsToIds parse a slice of git references and return the corresponding Entity's Id.

func (Id) HasPrefix

func (i Id) HasPrefix(prefix string) bool

func (Id) Human

func (i Id) Human() string

Human return the identifier, shortened for human consumption

func (Id) MarshalGQL

func (i Id) MarshalGQL(w io.Writer)

MarshalGQL implement the Marshaler interface for gqlgen

func (Id) String

func (i Id) String() string

String return the identifier as a string

func (*Id) UnmarshalGQL

func (i *Id) UnmarshalGQL(v interface{}) error

UnmarshalGQL implement the Unmarshaler interface for gqlgen

func (Id) Validate

func (i Id) Validate() error

Validate tell if the Id is valid

type Interface

type Interface interface {
	// Id return the Entity identifier
	//
	// This Id need to be immutable without having to store the entity somewhere (ie, an entity only in memory
	// should have a valid Id, and it should not change if further edit are done on this entity).
	// How to achieve that is up to the entity itself. A common way would be to take a hash of an immutable data at
	// the root of the entity.
	// It is acceptable to use such a hash and keep mutating that data as long as Id() is not called.
	Id() Id
}

type MergeResult

type MergeResult struct {
	// Err is set when a terminal error occur in the process
	Err error

	Id     Id
	Status MergeStatus

	// Only set for Invalid status
	Reason string

	// Only set for New or Updated status
	Entity Interface
}

MergeResult hold the result of a merge operation on an Entity.

func NewMergeError

func NewMergeError(err error, id Id) MergeResult

func NewMergeInvalidStatus

func NewMergeInvalidStatus(id Id, reason string) MergeResult

func NewMergeNewStatus added in v0.8.0

func NewMergeNewStatus(id Id, entity Interface) MergeResult

func NewMergeNothingStatus added in v0.8.0

func NewMergeNothingStatus(id Id) MergeResult

func NewMergeUpdatedStatus added in v0.8.0

func NewMergeUpdatedStatus(id Id, entity Interface) MergeResult

func (MergeResult) String

func (mr MergeResult) String() string

type MergeStatus

type MergeStatus int

MergeStatus represent the result of a merge operation of an entity

const (
	MergeStatusNew     MergeStatus // a new Entity was created locally
	MergeStatusInvalid             // the remote data is invalid
	MergeStatusUpdated             // a local Entity has been updated
	MergeStatusNothing             // no changes were made to a local Entity (already up to date)
	MergeStatusError               // a terminal error happened
)

type Resolver added in v0.8.0

type Resolver interface {
	Resolve(id Id) (Interface, error)
}

Resolver is an interface to find an Entity from its Id

func MakeResolver added in v0.8.0

func MakeResolver(entities ...Interface) Resolver

MakeResolver create a resolver able to return the given entities.

type ResolverFunc added in v0.8.0

type ResolverFunc func(id Id) (Interface, error)

ResolverFunc is a helper to morph a function resolver into a Resolver

func (ResolverFunc) Resolve added in v0.8.0

func (fn ResolverFunc) Resolve(id Id) (Interface, error)

type Resolvers added in v0.8.0

type Resolvers map[Interface]Resolver

Resolvers is a collection of Resolver, for different type of Entity

Directories

Path Synopsis
Package dag contains the base common code to define an entity stored in a chain of git objects, supporting actions like Push, Pull and Merge.
Package dag contains the base common code to define an entity stored in a chain of git objects, supporting actions like Push, Pull and Merge.

Jump to

Keyboard shortcuts

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