aspects

package
v0.0.0-...-8accae8 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aspect

type Aspect struct {
	Name string
	// contains filtered or unexported fields
}

Aspect carries access rules for a particular aspect in a bundle.

func (*Aspect) Get

func (a *Aspect) Get(databag DataBag, request string) (interface{}, error)

Get returns the aspect value identified by the request. If either the named aspect or the corresponding value can't be found, a NotFoundError is returned.

func (*Aspect) Set

func (a *Aspect) Set(databag DataBag, request string, value interface{}) error

Set sets the named aspect to a specified non-nil value.

func (*Aspect) Unset

func (a *Aspect) Unset(databag DataBag, request string) error

type BadRequestError

type BadRequestError struct {
	Account    string
	BundleName string
	Aspect     string
	Operation  string
	Request    string
	Cause      string
}

func (*BadRequestError) Error

func (e *BadRequestError) Error() string

func (*BadRequestError) Is

func (e *BadRequestError) Is(err error) bool

type Bundle

type Bundle struct {
	Account string
	Name    string
	Schema  Schema
	// contains filtered or unexported fields
}

Bundle holds a series of related aspects.

func NewBundle

func NewBundle(account string, bundleName string, aspects map[string]interface{}, schema Schema) (*Bundle, error)

NewBundle returns a new aspect bundle with the specified aspects and their rules.

func (*Bundle) Aspect

func (d *Bundle) Aspect(aspect string) *Aspect

Aspect returns an aspect from the aspect bundle.

type DataBag

type DataBag interface {
	Get(path string) (interface{}, error)
	Set(path string, value interface{}) error
	Unset(path string) error
	Data() ([]byte, error)
}

DataBag controls access to the aspect data storage.

type DatabagRead

type DatabagRead func() (JSONDataBag, error)

type DatabagWrite

type DatabagWrite func(JSONDataBag) error

type JSONDataBag

type JSONDataBag map[string]json.RawMessage

JSONDataBag is a simple DataBag implementation that keeps JSON in-memory.

func NewJSONDataBag

func NewJSONDataBag() JSONDataBag

NewJSONDataBag returns a DataBag implementation that stores data in JSON. The top-level of the JSON structure is always a map.

func (JSONDataBag) Copy

func (s JSONDataBag) Copy() JSONDataBag

Copy returns a copy of the databag.

func (JSONDataBag) Data

func (s JSONDataBag) Data() ([]byte, error)

Data returns all of the bag's data encoded in JSON.

func (JSONDataBag) Get

func (s JSONDataBag) Get(path string) (interface{}, error)

Get takes a path and a pointer to a variable into which the value referenced by the path is written. The path can be dotted. For each dot a JSON object is expected to exist (e.g., "a.b" is mapped to {"a": {"b": <value>}}).

func (JSONDataBag) Set

func (s JSONDataBag) Set(path string, value interface{}) error

Set takes a path to which the value will be written. The path can be dotted, in which case, a nested JSON object is created for each sub-key found after a dot. If the value is nil, the entry is deleted.

func (JSONDataBag) Unset

func (s JSONDataBag) Unset(path string) error

type JSONSchema

type JSONSchema struct{}

JSONSchema is the Schema implementation corresponding to JSONDataBag and it's able to validate its data.

func NewJSONSchema

func NewJSONSchema() JSONSchema

NewJSONSchema returns a Schema able to validate a JSONDataBag's data.

func (JSONSchema) SchemaAt

func (v JSONSchema) SchemaAt(path []string) ([]Schema, error)

SchemaAt always returns the JSONSchema.

func (JSONSchema) Type

func (v JSONSchema) Type() SchemaType

func (JSONSchema) Validate

func (s JSONSchema) Validate(jsonData []byte) error

Validate validates that the specified data can be encoded into JSON.

type NotFoundError

type NotFoundError struct {
	Account    string
	BundleName string
	Aspect     string
	Operation  string
	Requests   []string
	Cause      string
}

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

func (*NotFoundError) Is

func (e *NotFoundError) Is(err error) bool

type PathError

type PathError string

func (PathError) Error

func (e PathError) Error() string

func (PathError) Is

func (e PathError) Is(err error) bool

type Schema

type Schema interface {
	Validate(data []byte) error

	// SchemaAt returns the schemas (e.g., string, int, etc) that may be at the
	// provided path. If the path cannot be followed, an error is returned.
	SchemaAt(path []string) ([]Schema, error)

	// Type returns the SchemaType corresponding to the Schema.
	Type() SchemaType
}

Schema takes in data from the DataBag and validates that it's valid and could be committed.

type SchemaType

type SchemaType uint
const (
	Int SchemaType = iota
	Number
	String
	Bool
	Map
	Array
	Any
	Alt
)

func (SchemaType) String

func (v SchemaType) String() string

type StorageSchema

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

StorageSchema represents an aspect schema and can be used to validate JSON aspects against it.

func ParseSchema

func ParseSchema(raw []byte) (*StorageSchema, error)

ParseSchema parses a JSON aspect schema and returns a Schema that can be used to validate aspects.

func (*StorageSchema) SchemaAt

func (s *StorageSchema) SchemaAt(path []string) ([]Schema, error)

SchemaAt returns the types that may be stored at the specified path.

func (*StorageSchema) Type

func (s *StorageSchema) Type() SchemaType

func (*StorageSchema) Validate

func (s *StorageSchema) Validate(raw []byte) error

Validate validates the provided JSON object.

type Transaction

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

Transaction performs read and writes to a databag in an atomic way.

func NewTransaction

func NewTransaction(readDatabag DatabagRead, writeDatabag DatabagWrite, schema Schema) (*Transaction, error)

NewTransaction takes a getter and setter to read and write the databag.

func (*Transaction) Commit

func (t *Transaction) Commit() error

Commit applies the previous writes and validates the final databag. If any error occurs, the original databag is kept.

func (*Transaction) Data

func (t *Transaction) Data() ([]byte, error)

Data returns the transaction's committed data.

func (*Transaction) Get

func (t *Transaction) Get(path string) (interface{}, error)

Get reads a value from the transaction's databag including uncommitted changes.

func (*Transaction) Set

func (t *Transaction) Set(path string, value interface{}) error

Set sets a value in the transaction's databag. The change isn't persisted until Commit returns without errors.

func (*Transaction) Unset

func (t *Transaction) Unset(path string) error

Unset unsets a value in the transaction's databag. The change isn't persisted until Commit returns without errors.

type ValidationError

type ValidationError struct {
	Path []interface{}
	Err  error
}

TODO: keep a list of expected types (to support alternatives), an actual type/value and then optional unmet constraints for the expected types. Then this could be used to have more concise errors when there are many possible types https://github.com/snapcore/snapd/pull/13502#discussion_r1463658230

func (*ValidationError) Error

func (v *ValidationError) Error() string

Jump to

Keyboard shortcuts

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