protobuf

package
v0.3.19 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2023 License: MPL-2.0 Imports: 11 Imported by: 36

Documentation

Overview

Package protobuf provides a bridge between resources and protobuf interface.

Index

Examples

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 ProtoEqual added in v0.3.0

func ProtoEqual(a, b proto.Message) bool

ProtoEqual returns true if the two messages are equal.

This is a wrapper around proto.Equal which also supports vtproto messages.

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[T any, R Res[T]](resourceType resource.Type, _ R) 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 FromResourceOption added in v0.2.0

type FromResourceOption func(*FromResourceOptions)

FromResourceOption is an option for FromResource.

func WithoutYAML added in v0.2.0

func WithoutYAML() FromResourceOption

WithoutYAML disables YAML spec.

type FromResourceOptions added in v0.2.0

type FromResourceOptions struct {
	NoYAML bool
}

FromResourceOptions is a set of options for FromResource.

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 Res added in v0.2.0

type Res[T any] interface {
	*T
	ResourceUnmarshaler
	resource.Resource
}

Res is type parameter constraint for RegisterResource.

type Resource

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

Resource which can be marshaled and unmarshaled from protobuf.

Example (TestInline)
// This is a test to ensure that our custom 'inline' does work.
res := typed.NewResource[resourceDefinition, resourceDefinitionExtension](
	resource.NewMetadata("default", "testResourceYAML", "aaa", resource.VersionUndefined),
	protobuf.NewResourceSpec(&v1alpha1.ResourceDefinitionSpec{
		Aliases: []string{"test", "test2"},
	}),
)

created, err := time.Parse("2006-01-02", "2006-01-02")
if err != nil {
	panic(err)
}

res.Metadata().SetCreated(created)
res.Metadata().SetUpdated(created)

mRes, err := resource.MarshalYAML(res)
if err != nil {
	panic(err)
}

out, err := yaml.Marshal(mRes)
if err != nil {
	panic(err)
}

fmt.Println(string(out))
Output:

metadata:
    namespace: default
    type: testResourceYAML
    id: aaa
    version: undefined
    owner:
    phase: running
    created: 2006-01-02T00:00:00Z
    updated: 2006-01-02T00:00:00Z
spec:
    resourcetype: ""
    displaytype: ""
    defaultnamespace: ""
    aliases:
        - test
        - test2
    allaliases: []
    printcolumns: []
    sensitivity: 0

func FromResource

func FromResource(r resource.Resource, opts ...FromResourceOption) (*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() any

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
}

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 any) 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]) MarshalYAML added in v0.3.0

func (spec *ResourceSpec[T, S]) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler interface. We want it to inline `Value` field, without using `inline` tag.

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.

func (*ResourceSpec[T, S]) UnmarshalYAML added in v0.3.0

func (spec *ResourceSpec[T, S]) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler interface. We want it to inline `Value` field, without using `inline` tag.

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.

type YAMLResource added in v0.2.0

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

YAMLResource is a wrapper around Resource which implements yaml.Unmarshaler. Its here and not in resource package to avoid circular dependency.

func (*YAMLResource) Resource added in v0.2.0

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

Resource returns the underlying resource.

func (*YAMLResource) UnmarshalYAML added in v0.2.0

func (r *YAMLResource) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

Jump to

Keyboard shortcuts

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