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

Align return of UnstructuredTypedObject #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions bindings-go/apis/v2/codecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package v2
import (
"encoding/json"
"fmt"

"github.com/modern-go/reflect2"
)

// KnownTypeValidationFunc defines a function that can validate types.
Expand Down Expand Up @@ -133,6 +135,8 @@ func NewCodec(knownTypes KnownTypes, defaultCodec TypedObjectCodec, validationFu
}
}

var defaultCodec = NewDefaultCodec()

// Decode unmarshals a unstructured typed object into a TypedObjectAccessor.
// The given known types are used to decode the data into a specific.
// The given defaultCodec is used if no matching type is known.
Expand Down Expand Up @@ -176,8 +180,28 @@ func (c *codec) Encode(acc TypedObjectAccessor) ([]byte, error) {
return codec.Encode(acc)
}

func DecodeInto(obj TypedObjectAccessor, into TypedObjectAccessor) error {
un, err := ToUnstructuredTypedObject(nil, obj)
if err != nil {
return err
}
if reflect2.IsNil(un) {
return fmt.Errorf("no accessor")
}
return un.DecodeInto(into)
}

// ToUnstructuredTypedObject converts a typed object to a unstructured object.
func ToUnstructuredTypedObject(codec TypedObjectCodec, obj TypedObjectAccessor) (*UnstructuredTypedObject, error) {
if obj == nil {
return nil, nil
}
if un, ok := obj.(*UnstructuredTypedObject); ok {
return un, nil
}
if codec == nil {
codec = defaultCodec
}
data, err := codec.Encode(obj)
if err != nil {
return nil, err
Expand Down
11 changes: 7 additions & 4 deletions bindings-go/apis/v2/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewNameSelector(name string) selector.Interface {
}

// GetEffectiveRepositoryContext returns the current active repository context.
func (c ComponentDescriptor) GetEffectiveRepositoryContext() *UnstructuredTypedObject {
func (c ComponentDescriptor) GetEffectiveRepositoryContext() Repository {
if len(c.RepositoryContexts) == 0 {
return nil
}
Expand All @@ -82,13 +82,16 @@ func (c ComponentDescriptor) GetEffectiveRepositoryContext() *UnstructuredTypedO
// InjectRepositoryContext appends the given repository context to components descriptor repository history.
// The context is not appended if the effective repository context already matches the current context.
func InjectRepositoryContext(cd *ComponentDescriptor, repoCtx TypedObjectAccessor) error {
effective := cd.GetEffectiveRepositoryContext()
effective, err := NewUnstructured(cd.GetEffectiveRepositoryContext())
if err != nil {
return err
}
uRepoCtx, err := NewUnstructured(repoCtx)
if err != nil {
return err
}
if !UnstructuredTypesEqual(effective, &uRepoCtx) {
cd.RepositoryContexts = append(cd.RepositoryContexts, &uRepoCtx)
if !UnstructuredTypesEqual(effective, uRepoCtx) {
cd.RepositoryContexts = append(cd.RepositoryContexts, uRepoCtx)
}
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions bindings-go/apis/v2/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ func TypedObjectEqual(a, b TypedObjectAccessor) bool {
if err != nil {
return false
}
return UnstructuredTypesEqual(&uA, &uB)
return UnstructuredTypesEqual(uA, uB)
}

// NewUnstructured creates a new unstructured object from a typed object using the default codec.
func NewUnstructured(obj TypedObjectAccessor) (UnstructuredTypedObject, error) {
uObj, err := ToUnstructuredTypedObject(NewDefaultCodec(), obj)
func NewUnstructured(obj TypedObjectAccessor) (*UnstructuredTypedObject, error) {
uObj, err := ToUnstructuredTypedObject(nil, obj)
if err != nil {
return UnstructuredTypedObject{}, nil
return &UnstructuredTypedObject{}, err
}
return *uObj, nil
return uObj, nil
}

// NewEmptyUnstructured creates a new typed object without additional data.
Expand Down
4 changes: 2 additions & 2 deletions bindings-go/apis/v2/v2_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ = Describe("Helper", func() {
},
},
Relation: v2.ExternalRelation,
Access: &unstrucOCIRegistry1,
Access: unstrucOCIRegistry1,
}
ociRegistry2 = &v2.OCIRegistryAccess{
ObjectType: v2.ObjectType{
Expand All @@ -80,7 +80,7 @@ var _ = Describe("Helper", func() {
},
},
Relation: v2.ExternalRelation,
Access: &unstrucOCIRegistry2,
Access: unstrucOCIRegistry2,
}

comp = &v2.ComponentDescriptor{
Expand Down
4 changes: 2 additions & 2 deletions bindings-go/apis/v2/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var _ = Describe("Validation", func() {
Version: "1.2.3",
},
Relation: v2.ExternalRelation,
Access: &unstrucOCIRegistry1,
Access: unstrucOCIRegistry1,
}
ociRegistry2 = &v2.OCIRegistryAccess{
ObjectType: v2.ObjectType{
Expand All @@ -74,7 +74,7 @@ var _ = Describe("Validation", func() {
Version: "1.2.3",
},
Relation: v2.ExternalRelation,
Access: &unstrucOCIRegistry2,
Access: unstrucOCIRegistry2,
}

comp = &v2.ComponentDescriptor{
Expand Down
6 changes: 3 additions & 3 deletions bindings-go/ctf/componentarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (ca *ComponentArchive) AddResource(res *v2.Resource, info BlobInfo, reader
if err != nil {
return fmt.Errorf("unable to convert local filesystem type to untructured type: %w", err)
}
res.Access = &unstructuredType
res.Access = unstructuredType

if id == -1 {
ca.ComponentDescriptor.Resources = append(ca.ComponentDescriptor.Resources, *res)
Expand Down Expand Up @@ -209,7 +209,7 @@ func (ca *ComponentArchive) AddSource(src *v2.Source, info BlobInfo, reader io.R
if err != nil {
return fmt.Errorf("unable to convert local filesystem type to untructured type: %w", err)
}
src.Access = &unstructuredType
src.Access = unstructuredType

if id == -1 {
ca.ComponentDescriptor.Sources = append(ca.ComponentDescriptor.Sources, *src)
Expand Down Expand Up @@ -257,7 +257,7 @@ func (ca *ComponentArchive) AddResourceFromResolver(ctx context.Context, res *v2
if err != nil {
return fmt.Errorf("unable to convert local filesystem type to untructured type: %w", err)
}
res.Access = &unstructuredType
res.Access = unstructuredType

if id == -1 {
ca.ComponentDescriptor.Resources = append(ca.ComponentDescriptor.Resources, *res)
Expand Down
4 changes: 2 additions & 2 deletions bindings-go/ctf/ctf.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ const (
)

type CTF struct {
fs vfs.FileSystem
fs vfs.FileSystem
ctfPath string
tempDir string
tempFs vfs.FileSystem
tempFs vfs.FileSystem
}

// NewCTF reads a CTF archive from a file.
Expand Down
2 changes: 1 addition & 1 deletion bindings-go/ctf/listresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// ListResolver describes a ComponentResolver using a list of Component Descriptors
type ListResolver struct {
List *cdv2.ComponentDescriptorList
List *cdv2.ComponentDescriptorList
blobResolver *AggregatedBlobResolver
}

Expand Down
16 changes: 8 additions & 8 deletions bindings-go/ctf/listresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ var _ = Describe("ListResolver", func() {
cd.Name = "example.com/a"
cd.Version = "0.0.0"
repoCtx, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry", ""))
cd.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx)
cd.RepositoryContexts = append(cd.RepositoryContexts, repoCtx)

lr, err := ctf.NewListResolver(&cdv2.ComponentDescriptorList{
Components: []cdv2.ComponentDescriptor{cd},
})
Expect(err).ToNot(HaveOccurred())
res, err := lr.Resolve(context.TODO(), &repoCtx, "example.com/a", "0.0.0")
res, err := lr.Resolve(context.TODO(), repoCtx, "example.com/a", "0.0.0")
Expect(err).ToNot(HaveOccurred())
Expect(res.Name).To(Equal("example.com/a"))
})
Expand All @@ -46,7 +46,7 @@ var _ = Describe("ListResolver", func() {
cd.Name = "example.com/a"
cd.Version = "0.0.0"
repoCtx, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry", ""))
cd.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx)
cd.RepositoryContexts = append(cd.RepositoryContexts, repoCtx)

cd2 := cdv2.ComponentDescriptor{}
cd2.Name = "example.com/a"
Expand All @@ -57,20 +57,20 @@ var _ = Describe("ListResolver", func() {
},
}
repoCtx2, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry2", ""))
cd2.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx2)
cd2.RepositoryContexts = append(cd.RepositoryContexts, repoCtx2)

lr, err := ctf.NewListResolver(&cdv2.ComponentDescriptorList{
Components: []cdv2.ComponentDescriptor{cd, cd2},
})
Expect(err).ToNot(HaveOccurred())
res, err := lr.Resolve(context.TODO(), &repoCtx2, "example.com/a", "0.0.0")
res, err := lr.Resolve(context.TODO(), repoCtx2, "example.com/a", "0.0.0")
Expect(err).ToNot(HaveOccurred())
Expect(res.Name).To(Equal("example.com/a"))
Expect(res.Labels).To(ContainElement(cdv2.Label{
Name: "test",
}))

res, err = lr.Resolve(context.TODO(), &repoCtx, "example.com/a", "0.0.0")
res, err = lr.Resolve(context.TODO(), repoCtx, "example.com/a", "0.0.0")
Expect(err).ToNot(HaveOccurred())
Expect(res.Name).To(Equal("example.com/a"))
Expect(res.Labels).ToNot(ContainElement(cdv2.Label{
Expand All @@ -83,13 +83,13 @@ var _ = Describe("ListResolver", func() {
cd.Name = "example.com/a"
cd.Version = "0.0.0"
repoCtx, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry", ""))
cd.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx)
cd.RepositoryContexts = append(cd.RepositoryContexts, repoCtx)

lr, err := ctf.NewListResolver(&cdv2.ComponentDescriptorList{
Components: []cdv2.ComponentDescriptor{cd},
})
Expect(err).ToNot(HaveOccurred())
_, err = lr.Resolve(context.TODO(), &repoCtx, "example.com/b", "0.0.0")
_, err = lr.Resolve(context.TODO(), repoCtx, "example.com/b", "0.0.0")
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(ctf.NotFoundError))
})
Expand Down
2 changes: 1 addition & 1 deletion bindings-go/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ component:
// get the latest repository context.
// the context is returned as unstructured object (similar to the access types) as differnt repository types
// with different attributes are possible.
unstructuredRepoCtx := component.GetEffectiveRepositoryContext()
unstructuredRepoCtx, err := v2.ToUnstructuredTypedObject(nil, component.GetEffectiveRepositoryContext())
// decode the unstructured type into a specific type
ociRepo := &v2.OCIRegistryRepository{}
check(unstructuredRepoCtx.DecodeInto(ociRepo))
Expand Down
1 change: 1 addition & 0 deletions bindings-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/go-logr/logr v0.4.0
github.com/mandelsoft/vfs v0.0.0-20210530103237-5249dc39ce91
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/opencontainers/go-digest v1.0.0
Expand Down
14 changes: 7 additions & 7 deletions bindings-go/oci/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type BlobStore interface {
// ManifestBuilder converts a component descriptor with local defined blobs
// into a oci component descriptor with blobs as layers of the component descriptor.
type ManifestBuilder struct {
store BlobStore
archive *ctf.ComponentArchive
store BlobStore
archive *ctf.ComponentArchive
componentDescriptorStorageType string
}

Expand Down Expand Up @@ -127,10 +127,10 @@ func (b *ManifestBuilder) addComponentDescriptorDesc() (ocispecv1.Descriptor, er
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: ctf.ComponentDescriptorFileName,
Size: int64(len(data)),
ModTime: time.Now(),
Typeflag: tar.TypeReg,
Name: ctf.ComponentDescriptorFileName,
Size: int64(len(data)),
ModTime: time.Now(),
}); err != nil {
return ocispecv1.Descriptor{}, fmt.Errorf("unable to add component descriptor header: %w", err)
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func (b *ManifestBuilder) addLocalBlobs(ctx context.Context) ([]ocispecv1.Descri
if err != nil {
return nil, fmt.Errorf("unable to convert ociBlob to untructured type: %w", err)
}
res.Access = &unstructuredType
res.Access = unstructuredType
b.archive.ComponentDescriptor.Resources[i] = res
blobDescriptors = append(blobDescriptors, desc)
}
Expand Down
9 changes: 4 additions & 5 deletions bindings-go/oci/oci_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestConfig(t *testing.T) {
RunSpecs(t, "oci Test Suite")
}

var _ = Describe("helper", func(){
var _ = Describe("helper", func() {

Context("OCIRef", func() {

Expand Down Expand Up @@ -56,7 +56,7 @@ var _ = Describe("helper", func(){

It("should correctly parse a repository url with a sha256-digest name mapping", func() {
repoCtx := cdv2.OCIRegistryRepository{
BaseURL: "example.com:443",
BaseURL: "example.com:443",
ComponentNameMapping: cdv2.OCIRegistryDigestMapping,
}
ref, err := oci.OCIRef(repoCtx, "somecomp", "v0.0.0")
Expand All @@ -71,7 +71,7 @@ var _ = Describe("helper", func(){
// testClient describes a test oci client.
type testClient struct {
getManifest func(ctx context.Context, ref string) (*ocispecv1.Manifest, error)
fetch func(ctx context.Context, ref string, desc ocispecv1.Descriptor, writer io.Writer) error
fetch func(ctx context.Context, ref string, desc ocispecv1.Descriptor, writer io.Writer) error
}

var _ oci.Client = &testClient{}
Expand All @@ -86,7 +86,7 @@ func (t testClient) Fetch(ctx context.Context, ref string, desc ocispecv1.Descri

// testCache describes a test resolve cache.
type testCache struct {
get func (ctx context.Context, repoCtx cdv2.OCIRegistryRepository, name, version string) (*cdv2.ComponentDescriptor, error)
get func(ctx context.Context, repoCtx cdv2.OCIRegistryRepository, name, version string) (*cdv2.ComponentDescriptor, error)
store func(ctx context.Context, descriptor *cdv2.ComponentDescriptor) error
}

Expand All @@ -99,4 +99,3 @@ func (t testCache) Get(ctx context.Context, repoCtx cdv2.OCIRegistryRepository,
func (t testCache) Store(ctx context.Context, descriptor *cdv2.ComponentDescriptor) error {
return t.store(ctx, descriptor)
}

14 changes: 7 additions & 7 deletions bindings-go/oci/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ type Cache interface {
// Resolver is a generic resolve to resolve a component descriptor from a oci registry.
// This resolver implements the ctf.ComponentResolver interface.
type Resolver struct {
log logr.Logger
client Client
cache Cache
log logr.Logger
client Client
cache Cache
decodeOpts []codec.DecodeOption
}

// NewResolver creates a new resolver.
func NewResolver(client Client, decodeOpts ...codec.DecodeOption) *Resolver {
return &Resolver{
log: logr.Discard(),
client: client,
log: logr.Discard(),
client: client,
decodeOpts: decodeOpts,
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *Resolver) resolve(ctx context.Context, repoCtx v2.Repository, name, ver
}
} else {
if withBlobResolver {
manifest, ref , err := r.fetchManifest(ctx, repo, name, version)
manifest, ref, err := r.fetchManifest(ctx, repo, name, version)
if err != nil {
return nil, nil, err
}
Expand All @@ -172,7 +172,7 @@ func (r *Resolver) resolve(ctx context.Context, repoCtx v2.Repository, name, ver
}
}

manifest, ref , err := r.fetchManifest(ctx, repo, name, version)
manifest, ref, err := r.fetchManifest(ctx, repo, name, version)
if err != nil {
return nil, nil, err
}
Expand Down
Loading