Documentation ¶
Overview ¶
Package protobuf provides a bridge between resources and protobuf interface.
Index ¶
- func CreateResource(resourceType resource.Type) (resource.Resource, error)
- func ProtoEqual(a, b proto.Message) bool
- func ProtoMarshal(m proto.Message) ([]byte, error)
- func ProtoUnmarshal(b []byte, m proto.Message) error
- func RegisterDynamic[R DeepCopyable[R], T any, RS TypedResource[T, R]](resourceType resource.Type, r RS) error
- func RegisterResource[T any, R Res[T]](resourceType resource.Type, _ R) error
- func UnmarshalResource(r *Resource) (resource.Resource, error)
- type DeepCopyable
- type FromResourceOption
- type FromResourceOptions
- type ProtoMarshaler
- type ProtoUnmarshaler
- type Res
- type Resource
- type ResourceSpec
- func (spec ResourceSpec[T, S]) DeepCopy() ResourceSpec[T, S]
- func (spec *ResourceSpec[T, S]) Equal(other any) bool
- func (spec *ResourceSpec[T, S]) GetValue() proto.Message
- func (spec *ResourceSpec[T, S]) MarshalJSON() ([]byte, error)
- func (spec *ResourceSpec[T, S]) MarshalProto() ([]byte, error)
- func (spec *ResourceSpec[T, S]) MarshalYAML() (any, error)
- func (spec *ResourceSpec[T, S]) UnmarshalJSON(bytes []byte) error
- func (spec *ResourceSpec[T, S]) UnmarshalProto(protoBytes []byte) error
- func (spec *ResourceSpec[T, S]) UnmarshalYAML(node *yaml.Node) error
- type ResourceUnmarshaler
- type Spec
- type TypedResource
- type YAMLResource
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateResource ¶
CreateResource creates an empty resource for a type.
func ProtoEqual ¶ added in v0.3.0
ProtoEqual returns true if the two messages are equal.
This is a wrapper around proto.Equal which also supports vtproto messages.
func ProtoMarshal ¶
ProtoMarshal returns the wire-format encoding of m.
func ProtoUnmarshal ¶
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 ¶
RegisterResource creates a mapping between resource type and its protobuf unmarshaller.
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 ¶
ProtoMarshaler is an interface which should be implemented by Resource spec to support conversion to protobuf.Resource.
type ProtoUnmarshaler ¶
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 (*Resource) Unmarshal ¶
func (r *Resource) Unmarshal(res ResourceUnmarshaler) error
Unmarshal into specific Resource instance.
type ResourceSpec ¶
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 ¶
ResourceUnmarshaler is an interface which should be implemented by Resource to support conversion from protobuf.Resource.
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.