protopatch

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const PathSegmentSeparator = "."

Variables

View Source
var (
	ErrAccessToNonContainer = errors.New("connot descend into specified key; attempted to access a sub-value in an entity that is not a message, list or map")
	ErrAppendToNonList      = errors.New("connot append to non list field")
	ErrInsertToNonList      = errors.New("connot insert to non list field")
	// ErrDeleteNonKey            = errors.New("connot delete non key value")
	ErrMutationOfReadOnlyValue = errors.New("connot mutate read-only value")
	ErrMismatchingType         = protoops.ErrMismatchingType
)
View Source
var ErrNoContainerTransformationDefined = errors.New("no transformation defined for the provided container")
View Source
var ErrNoConversionDefined = errors.New("no conversion defined for the provided types")

Functions

func Append

func Append(base proto.Message, path string, new any, opts ...Option) error

func Clear

func Clear(base proto.Message, path string, opts ...Option) error

func Copy

func Copy(base proto.Message, targetPath, replacementPath string, opts ...Option) error

func IdentityConverter

func IdentityConverter(to, from any) (any, error)

IdentityConverter converts the provided value to itself ensuring that provided types match. It returns ErrNoConversionDefined error for mismatched types.

func Insert

func Insert(base proto.Message, path string, new any, opts ...Option) error

func Move

func Move(base proto.Message, targetPath, replacementPath string, opts ...Option) error

func NewErrInPath

func NewErrInPath(path string, err error) error

func Set

func Set(base proto.Message, path string, to any, opts ...Option) error

func Swap

func Swap(base proto.Message, targetPath, replacementPath string, opts ...Option) error

Types

type Container

type Container interface {

	// IsReadOnly reports wether underlying container value is read only.
	IsReadOnly() bool

	// Self returns underlying container value. For messages it returns its proto.Message value. For lists and maps it returns List and Map interfaces accordingly.
	Self() any

	// Get returns value associated with the given field / index / key. It returns error if the field / index / key is not found. For scalar types and messages it returns its value. For lists and maps it returns List and Map interfaces accordingly.
	Get(string) (any, error)

	// GetCopy returns a copy of value associated with the given field / index / key. It returns error if the field / index / key is not found. For scalar types and messages it returns its value. For lists and maps it returns List and Map interfaces accordingly.
	GetCopy(string) (any, error)

	// GetNew returns a zero value of the type of value associated with the given field / index / key. It returns error if the field is not found or if index / key is malformed. For scalar types and messages it returns its zero value. For lists and maps it returns List and Map interfaces accordingly without any elements.
	GetNew(string) (any, error)

	// Mutable is a mutable variant of Get method - it returns value associated with the given field / index / key. It returns error if the container is read-only or the field / index / key is not found. For scalar types and messages it returns its value. For lists and maps it returns List and Map interfaces accordingly.
	Mutable(string) (any, error)

	// Access descends into the given field / index / key and returns a new container for that value. It returns error if the field / index / key is not found or is not associated with a composite type.
	Access(string) (Container, error)

	// AccessMutable is a mutable variant of Access method - it descends into the given field / index / key and returns a new container for that value. It returns error if the container is read-only, if the field / index / key is not found or is not associated with a composite type.
	AccessMutable(string) (Container, error)

	// Set stores the provided value under the associated field / index / key. It returns error if the container is read-only, if the field / index / key is not found or if the value has wrong type. When the provided value is nil, it clears the associated field / index / key.
	Set(string, any) error

	// Append adds the provided value to the end of a list. It returns error if the container is read-only, if the container is not a list or if the value has wrong type.
	Append(any) error

	// Insert adds the provided value at specified position to a list or map. It returns error if the container is read-only, if the container is not a list or map container, if the index is not found or if the value has wrong type.
	Insert(string, any) error
}

Container is a wrapper of a composite protocol buffer type (message, list or map) allowing easy traversal and operations.

func Access

func Access(container Container, path Path, opts ...Option) (Container, error)

func AccessMutable

func AccessMutable(container Container, path Path, opts ...Option) (Container, error)

func MessageContainer

func MessageContainer(m proto.Message) Container

type ContainerTransformer

type ContainerTransformer interface {
	// TransformContainer performs container transformation as needed. Returned value must be a valid container or function should return an error. If transformation is not defined for the given container function should return an unwrapped ErrNoContainerTransformationDefined error.
	TransformContainer(container Container) (Container, error)
}

ContainerTransformer represents an entity that can modify freshly accessed container.

type ContainerTransformerFunc

type ContainerTransformerFunc func(container Container) (Container, error)

ContainerTransformerFunc allows to implement ContainerTransformer interface with a function.

func (ContainerTransformerFunc) TransformContainer

func (fn ContainerTransformerFunc) TransformContainer(container Container) (Container, error)

type Converter

type Converter interface {
	// Convert performs type conversion to type of first provided item from the second provided value. Returned value must be of first item type or function should return an error. If conversion is not defined for provided types Convert function should return an unwrapped ErrNoConversionDefined error.
	Convert(to, from any) (any, error)
}

Converter represents an entity that can change type of one entity to another.

type ConverterFunc

type ConverterFunc func(to, from any) (any, error)

ConverterFunc allows to implement Converter interface with a function.

func (ConverterFunc) Convert

func (fn ConverterFunc) Convert(to, from any) (any, error)

type ErrInPath

type ErrInPath struct {
	Path  string
	Cause error
}

func (ErrInPath) Error

func (e ErrInPath) Error() string

func (ErrInPath) Unwrap

func (e ErrInPath) Unwrap() error

type ErrNotFound

type ErrNotFound struct {
	Kind  string // "field", "index" or "key"
	Value string
}

func (ErrNotFound) Error

func (e ErrNotFound) Error() string

type ErrOperationFailed

type ErrOperationFailed struct {
	Op    string
	Cause error
}

func (ErrOperationFailed) Error

func (e ErrOperationFailed) Error() string

func (ErrOperationFailed) Unwrap

func (e ErrOperationFailed) Unwrap() error

type List

type List = protoops.List

List represents a protocol buffer list value.

func NewList

func NewList(parentField protoreflect.FieldDescriptor, li protoreflect.List) List

type Map

type Map = protoops.Map

Map represents a protocol buffer map value.

func NewMap

func NewMap(parentField protoreflect.FieldDescriptor, ma protoreflect.Map) Map

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithContainerTransformation

func WithContainerTransformation(transformers ...ContainerTransformer) Option

func WithConversion

func WithConversion(converters ...Converter) Option

type Path

type Path string

func (Path) First

func (p Path) First() PathSegment

func (Path) Iter

func (p Path) Iter(yield func(PathSegment) bool)

func (Path) Join

func (p Path) Join(other ...Path) Path

Join create a new Path by concatenating all provided paths.

func (Path) JoinSegment

func (p Path) JoinSegment(segments ...PathSegment) Path

JoinSegment create a new Path by concatenating all provided segments at the ent of the give Path.

func (Path) JoinSegmentValue

func (p Path) JoinSegmentValue(segments ...string) Path

JoinSegmentValue create a new Path by concatenating all provided segment values at the ent of the give Path.

func (Path) Last

func (p Path) Last() PathSegment

func (Path) Segments

func (p Path) Segments() []PathSegment

func (Path) SegmentsCount

func (p Path) SegmentsCount() int

type PathSegment

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

func (PathSegment) FollowingPath

func (ps PathSegment) FollowingPath() Path

func (PathSegment) FollowingPathWithCurrentSegment

func (ps PathSegment) FollowingPathWithCurrentSegment() Path

func (PathSegment) IsFirst

func (ps PathSegment) IsFirst() bool

func (PathSegment) IsLast

func (ps PathSegment) IsLast() bool

func (PathSegment) Next

func (ps PathSegment) Next() PathSegment

func (PathSegment) PrecedingPath

func (ps PathSegment) PrecedingPath() Path

func (PathSegment) PrecedingPathWithCurrentSegment

func (ps PathSegment) PrecedingPathWithCurrentSegment() Path

func (PathSegment) Previous

func (ps PathSegment) Previous() PathSegment

func (PathSegment) Value

func (ps PathSegment) Value() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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