Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Fix go bindings and allow additional type attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Schrodi authored and ccwienk committed Sep 25, 2020
1 parent 6450013 commit 4bd928e
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 223 deletions.
65 changes: 42 additions & 23 deletions bindings-go/apis/v2/accesstypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import (
"github.com/ghodss/yaml"
)

// KnownAccessTypes contains all known access serializer
var KnownAccessTypes = KnownTypes{
OCIRegistryType: ociCodec,
GitHubAccessType: githubAccessCodec,
WebType: webCodec,
}

// OCIRegistryType is the access type of a oci registry.
const OCIRegistryType = "ociRegistry"

Expand All @@ -34,7 +41,7 @@ type OCIRegistryAccess struct {
ImageReference string `json:"imageReference"`
}

var _ AccessAccessor = &OCIRegistryAccess{}
var _ TypedObjectAccessor = &OCIRegistryAccess{}

func (O OCIRegistryAccess) GetData() ([]byte, error) {
return json.Marshal(O)
Expand All @@ -50,15 +57,15 @@ func (O *OCIRegistryAccess) SetData(bytes []byte) error {
return nil
}

var ociCodec = &AccessCodecWrapper{
AccessDecoder: AccessDecoderFunc(func(data []byte) (AccessAccessor, error) {
var ociCodec = &TypedObjectCodecWrapper{
TypedObjectDecoder: TypedObjectDecoderFunc(func(data []byte) (TypedObjectAccessor, error) {
var ociImage OCIRegistryAccess
if err := json.Unmarshal(data, &ociImage); err != nil {
return nil, err
}
return &ociImage, nil
}),
AccessEncoder: AccessEncoderFunc(func(accessor AccessAccessor) ([]byte, error) {
TypedObjectEncoder: TypedObjectEncoderFunc(func(accessor TypedObjectAccessor) ([]byte, error) {
ociImage, ok := accessor.(*OCIRegistryAccess)
if !ok {
return nil, fmt.Errorf("accessor is not of type %s", OCIImageType)
Expand All @@ -78,7 +85,7 @@ type Web struct {
URL string `json:"url"`
}

var _ AccessAccessor = &Web{}
var _ TypedObjectAccessor = &Web{}

func (w Web) GetData() ([]byte, error) {
return yaml.Marshal(w)
Expand All @@ -94,15 +101,15 @@ func (w *Web) SetData(bytes []byte) error {
return nil
}

var webCodec = &AccessCodecWrapper{
AccessDecoder: AccessDecoderFunc(func(data []byte) (AccessAccessor, error) {
var webCodec = &TypedObjectCodecWrapper{
TypedObjectDecoder: TypedObjectDecoderFunc(func(data []byte) (TypedObjectAccessor, error) {
var web Web
if err := json.Unmarshal(data, &web); err != nil {
return nil, err
}
return &web, nil
}),
AccessEncoder: AccessEncoderFunc(func(accessor AccessAccessor) ([]byte, error) {
TypedObjectEncoder: TypedObjectEncoderFunc(func(accessor TypedObjectAccessor) ([]byte, error) {
web, ok := accessor.(*Web)
if !ok {
return nil, fmt.Errorf("accessor is not of type %s", OCIImageType)
Expand All @@ -124,7 +131,7 @@ type GitHubAccess struct {
Ref string `json:"ref"`
}

var _ AccessAccessor = &GitHubAccess{}
var _ TypedObjectAccessor = &GitHubAccess{}

func (w GitHubAccess) GetData() ([]byte, error) {
return yaml.Marshal(w)
Expand All @@ -141,15 +148,15 @@ func (w *GitHubAccess) SetData(bytes []byte) error {
return nil
}

var githubAccessCodec = &AccessCodecWrapper{
AccessDecoder: AccessDecoderFunc(func(data []byte) (AccessAccessor, error) {
var githubAccessCodec = &TypedObjectCodecWrapper{
TypedObjectDecoder: TypedObjectDecoderFunc(func(data []byte) (TypedObjectAccessor, error) {
var github GitHubAccess
if err := json.Unmarshal(data, &github); err != nil {
return nil, err
}
return &github, nil
}),
AccessEncoder: AccessEncoderFunc(func(accessor AccessAccessor) ([]byte, error) {
TypedObjectEncoder: TypedObjectEncoderFunc(func(accessor TypedObjectAccessor) ([]byte, error) {
github, ok := accessor.(*GitHubAccess)
if !ok {
return nil, fmt.Errorf("accessor is not of type %s", OCIImageType)
Expand All @@ -158,19 +165,32 @@ var githubAccessCodec = &AccessCodecWrapper{
}),
}

// CustomAccess describes a generic dependency of a resolvable component.
type CustomAccess struct {
// CustomType describes a generic dependency of a resolvable component.
type CustomType struct {
ObjectType `json:",inline"`
Data map[string]interface{} `json:"-"`
}

var _ AccessAccessor = &CustomAccess{}
// NewTypeOnly creates a new typed object without additional data.
func NewTypeOnly(ttype string) TypedObjectAccessor {
return NewCustomType(ttype, nil)
}

func (c CustomAccess) GetData() ([]byte, error) {
// NewCustomType creates a new custom typed object.
func NewCustomType(ttype string, data map[string]interface{}) TypedObjectAccessor {
ct := CustomType{}
ct.SetType(ttype)
ct.Data = data
return &ct
}

var _ TypedObjectAccessor = &CustomType{}

func (c CustomType) GetData() ([]byte, error) {
return json.Marshal(c.Data)
}

func (c *CustomAccess) SetData(data []byte) error {
func (c *CustomType) SetData(data []byte) error {
var values map[string]interface{}
if err := yaml.Unmarshal(data, &values); err != nil {
return err
Expand All @@ -179,9 +199,9 @@ func (c *CustomAccess) SetData(data []byte) error {
return nil
}

var customCodec = &AccessCodecWrapper{
AccessDecoder: AccessDecoderFunc(func(data []byte) (AccessAccessor, error) {
var acc CustomAccess
var customCodec = &TypedObjectCodecWrapper{
TypedObjectDecoder: TypedObjectDecoderFunc(func(data []byte) (TypedObjectAccessor, error) {
var acc CustomType
if err := yaml.Unmarshal(data, &acc); err != nil {
return nil, err
}
Expand All @@ -190,12 +210,11 @@ var customCodec = &AccessCodecWrapper{
if err := json.Unmarshal(data, &values); err != nil {
return nil, err
}

acc.Data = values
return &acc, nil
}),
AccessEncoder: AccessEncoderFunc(func(accessor AccessAccessor) ([]byte, error) {
custom, ok := accessor.(*CustomAccess)
TypedObjectEncoder: TypedObjectEncoderFunc(func(accessor TypedObjectAccessor) ([]byte, error) {
custom, ok := accessor.(*CustomType)
if !ok {
return nil, errors.New("accessor is not a custom type %s")
}
Expand Down
55 changes: 27 additions & 28 deletions bindings-go/apis/v2/codecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,47 @@ import (
"strings"
)

// KnownAccessTypes contains all known access serializer
var KnownAccessTypes = map[string]AccessCodec{
OCIRegistryType: ociCodec,
GitHubAccessType: githubAccessCodec,
WebType: webCodec,
}
// KnownTypes defines a set of known types.
type KnownTypes map[string]TypedObjectCodec

// KnownTypeValidationFunc defines a function that can optionally validate types.
type KnownTypeValidationFunc func(ttype string) error

// AccessCodec describes a known component type and how it is decoded and encoded
type AccessCodec interface {
AccessDecoder
AccessEncoder
// TypedObjectCodec describes a known component type and how it is decoded and encoded
type TypedObjectCodec interface {
TypedObjectDecoder
TypedObjectEncoder
}

// AccessCodecWrapper is a simple struct that implements the AccessCodec interface
type AccessCodecWrapper struct {
AccessDecoder
AccessEncoder
// TypedObjectCodecWrapper is a simple struct that implements the TypedObjectCodec interface
type TypedObjectCodecWrapper struct {
TypedObjectDecoder
TypedObjectEncoder
}

// AccessDecoder decodes a component dependency.
type AccessDecoder interface {
Decode(data []byte) (AccessAccessor, error)
// TypedObjectDecoder defines a decoder for a typed object.
type TypedObjectDecoder interface {
Decode(data []byte) (TypedObjectAccessor, error)
}

// AccessEncoder encodes a component dependency.
type AccessEncoder interface {
Encode(accessor AccessAccessor) ([]byte, error)
// TypedObjectEncoder defines a encoder for a typed object.
type TypedObjectEncoder interface {
Encode(accessor TypedObjectAccessor) ([]byte, error)
}

// AccessDecoderFunc is a simple function that implements the AccessDecoder interface.
type AccessDecoderFunc func(data []byte) (AccessAccessor, error)
// TypedObjectDecoderFunc is a simple function that implements the TypedObjectDecoder interface.
type TypedObjectDecoderFunc func(data []byte) (TypedObjectAccessor, error)

// Decode is the Decode implementation of the AccessDecoder interface.
func (e AccessDecoderFunc) Decode(data []byte) (AccessAccessor, error) {
// Decode is the Decode implementation of the TypedObjectDecoder interface.
func (e TypedObjectDecoderFunc) Decode(data []byte) (TypedObjectAccessor, error) {
return e(data)
}

// AccessEncoderFunc is a simple function that implements the AccessEncoder interface.
type AccessEncoderFunc func(accessor AccessAccessor) ([]byte, error)
// TypedObjectEncoderFunc is a simple function that implements the TypedObjectEncoder interface.
type TypedObjectEncoderFunc func(accessor TypedObjectAccessor) ([]byte, error)

// Encode is the Encode implementation of the AccessEncoder interface.
func (e AccessEncoderFunc) Encode(accessor AccessAccessor) ([]byte, error) {
// Encode is the Encode implementation of the TypedObjectEncoder interface.
func (e TypedObjectEncoderFunc) Encode(accessor TypedObjectAccessor) ([]byte, error) {
return e(accessor)
}

Expand Down
Loading

0 comments on commit 4bd928e

Please sign in to comment.