protobuf

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MPL-2.0 Imports: 10 Imported by: 36

Documentation

Overview

Package protobuf provides a bridge between resources and protobuf interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateResource

func CreateResource(resourceType resource.Type) (resource.Resource, error)

CreateResource creates an empty resource for a type.

func ProtoMarshal

func ProtoMarshal(m proto.Message) ([]byte, error)

ProtoMarshal returns the wire-format encoding of m.

func ProtoUnmarshal

func ProtoUnmarshal(b []byte, m proto.Message) error

ProtoUnmarshal parses the wire-format message in b and places the result in m. The provided message must be mutable (e.g., a non-nil pointer to a message).

func RegisterDynamic

func RegisterDynamic[R DeepCopyable[R], T any, RS TypedResource[T, R]](resourceType resource.Type, r RS) error

RegisterDynamic creates a mapping between resource type and its protobuf marshaller and unmarshaller. It relies on TypedSpec returning Spec struct `with protobuf:<n>` tags.

func RegisterResource

func RegisterResource(resourceType resource.Type, r ResourceUnmarshaler) error

RegisterResource creates a mapping between resource type and its protobuf unmarshaller.

func UnmarshalResource

func UnmarshalResource(r *Resource) (resource.Resource, error)

UnmarshalResource converts proto.Resource to real resource if possible.

If conversion is not registered, proto.Resource is returned.

Types

type DeepCopyable

type DeepCopyable[T any] interface {
	DeepCopy() T
}

DeepCopyable is a duplicate of github.com/cosi-project/runtime/pkg/resource/typed.DeepCopyable to prevent import cycles.

type ProtoMarshaler

type ProtoMarshaler interface {
	MarshalProto() ([]byte, error)
}

ProtoMarshaler is an interface which should be implemented by Resource spec to support conversion to protobuf.Resource.

type ProtoUnmarshaler

type ProtoUnmarshaler interface {
	UnmarshalProto([]byte) error
}

ProtoUnmarshaler is an interface which should be implemented by Resource spec to support conversion from protobuf.Resource.

type Resource

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

Resource which can be marshaled and unmarshaled from protobuf.

func FromResource

func FromResource(r resource.Resource) (*Resource, error)

FromResource converts a resource which supports spec protobuf marshaling to protobuf.Resource.

func Unmarshal

func Unmarshal(protoResource *v1alpha1.Resource) (*Resource, error)

Unmarshal protobuf marshaled resource into Resource.

func (*Resource) DeepCopy

func (r *Resource) DeepCopy() resource.Resource

DeepCopy of the resource.

func (*Resource) Marshal

func (r *Resource) Marshal() (*v1alpha1.Resource, error)

Marshal into protobuf resource.

func (*Resource) Metadata

func (r *Resource) Metadata() *resource.Metadata

Metadata for the resource.

func (*Resource) Spec

func (r *Resource) Spec() interface{}

Spec of the resource.

func (*Resource) String

func (r *Resource) String() string

func (*Resource) Unmarshal

func (r *Resource) Unmarshal(res ResourceUnmarshaler) error

Unmarshal into specific Resource instance.

type ResourceSpec

type ResourceSpec[T any, S Spec[T]] struct {
	Value S `yaml:",inline"`
}

ResourceSpec wraps proto.Message structures and adds DeepCopy and marshaling methods. T is a protobuf generated structure. S is a pointer to T. Example usage: type WrappedSpec = ResourceSpec[ProtoSpec, *ProtoSpec].

func NewResourceSpec

func NewResourceSpec[T any, S Spec[T]](value S) ResourceSpec[T, S]

NewResourceSpec creates new ResourceSpec[T, S]. T is a protobuf generated structure. S is a pointer to T.

func (ResourceSpec[T, S]) DeepCopy

func (spec ResourceSpec[T, S]) DeepCopy() ResourceSpec[T, S]

DeepCopy creates a copy of the wrapped proto.Message.

func (*ResourceSpec[T, S]) Equal

func (spec *ResourceSpec[T, S]) Equal(other interface{}) bool

Equal implements spec equality check.

func (*ResourceSpec[T, S]) GetValue

func (spec *ResourceSpec[T, S]) GetValue() proto.Message

GetValue returns wrapped protobuf object.

func (*ResourceSpec[T, S]) MarshalJSON

func (spec *ResourceSpec[T, S]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*ResourceSpec[T, S]) MarshalProto

func (spec *ResourceSpec[T, S]) MarshalProto() ([]byte, error)

MarshalProto implements ProtoMarshaler.

func (*ResourceSpec[T, S]) UnmarshalJSON

func (spec *ResourceSpec[T, S]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*ResourceSpec[T, S]) UnmarshalProto

func (spec *ResourceSpec[T, S]) UnmarshalProto(protoBytes []byte) error

UnmarshalProto implements protobuf.ResourceUnmarshaler.

type ResourceUnmarshaler

type ResourceUnmarshaler interface {
	UnmarshalProto(*resource.Metadata, []byte) error
}

ResourceUnmarshaler is an interface which should be implemented by Resource to support conversion from protobuf.Resource.

type Spec

type Spec[T any] interface {
	proto.Message
	*T
}

Spec should be proto.Message and pointer.

type TypedResource

type TypedResource[T any, R DeepCopyable[R]] interface {
	*T
	TypedSpec() *R
	resource.Resource
}

TypedResource is similar to github.com/cosi-project/runtime/pkg/resource/typed.Resource and required to prevent import cycles.

Jump to

Keyboard shortcuts

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