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

Commit

Permalink
add localFilesystem access to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Schrodi authored and ccwienk committed Nov 27, 2020
1 parent 81a06a9 commit 4fd6d60
Show file tree
Hide file tree
Showing 18 changed files with 299 additions and 157 deletions.
19 changes: 6 additions & 13 deletions bindings-go/apis/v2/accesstypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (a *OCIBlobAccess) SetData(bytes []byte) error {
}

// LocalOCIBlobType is the access type of a oci blob in the current component descriptor manifest.
const LocalOCIBlobType = "LocalOCIBlob"
const LocalOCIBlobType = "localOciBlob"

// NewLocalOCIBlobAccess creates a new LocalOCIBlob accessor
func NewLocalOCIBlobAccess(digest string) TypedObjectAccessor {
Expand All @@ -131,8 +131,6 @@ func NewLocalOCIBlobAccess(digest string) TypedObjectAccessor {
// LocalOCIBlobAccess describes the access for a blob that is stored in the component descriptors oci manifest.
type LocalOCIBlobAccess struct {
ObjectType `json:",inline"`
// MediaType is the media type of the object this schema refers to.
MediaType string `json:"mediaType,omitempty"`
// Digest is the digest of the targeted content.
Digest string `json:"digest"`
}
Expand All @@ -147,32 +145,28 @@ func (a *LocalOCIBlobAccess) SetData(bytes []byte) error {
return err
}
a.Digest = newAccess.Digest
a.MediaType = newAccess.MediaType
return nil
}

// LocalBlobType is the access type of a oci blob in a manifest.
const LocalFilesystemBlobType = "localFilesystemBlob"

// NewLocalFilesystemBlobAccess creates a new localFilesystemBlob accessor.
func NewLocalFilesystemBlobAccess(mediaType, path string) TypedObjectAccessor {
func NewLocalFilesystemBlobAccess(path string) TypedObjectAccessor {
return &LocalFilesystemBlobAccess{
ObjectType: ObjectType{
Type: LocalFilesystemBlobType,
},
Name: path,
MediaType: mediaType,
Filename: path,
}
}

// LocalFilesystemBlobAccess describes the access for a blob on the filesystem.
type LocalFilesystemBlobAccess struct {
ObjectType `json:",inline"`
// Name is the name of the blob in the local filesystem.
// Filename is the name of the blob in the local filesystem.
// The blob is expected to be at <fs-root>/blobs/<name>
Name string `json:"path"`
// MediaType defines the media type of the current read blob.
MediaType string `json:"mediaType"`
Filename string `json:"filename"`
}

func (a LocalFilesystemBlobAccess) GetData() ([]byte, error) {
Expand All @@ -184,8 +178,7 @@ func (a *LocalFilesystemBlobAccess) SetData(bytes []byte) error {
if err := json.Unmarshal(bytes, &newAccess); err != nil {
return err
}
a.Name = newAccess.Name
a.MediaType = newAccess.MediaType
a.Filename = newAccess.Filename
return nil
}

Expand Down
56 changes: 0 additions & 56 deletions bindings-go/apis/v2/ctf/ctf.go

This file was deleted.

77 changes: 39 additions & 38 deletions bindings-go/apis/v2/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,36 @@ func (c ComponentDescriptor) GetEffectiveRepositoryContext() RepositoryContext {
return c.RepositoryContexts[len(c.RepositoryContexts)-1]
}

// GetComponentReferences returns all component references that matches the given selectors.
func (c ComponentDescriptor) GetComponentReferences(selectors ...IdentitySelector) ([]ComponentReference, error) {
refs := make([]ComponentReference, 0)
for _, ref := range c.ComponentReferences {
ok, err := selector.MatchSelectors(ref.GetIdentity(), selectors...)
if err != nil {
return nil, fmt.Errorf("unable to match selector for resource %s: %w", ref.Name, err)
}
if ok {
refs = append(refs, ref)
}
}
if len(refs) == 0 {
return refs, NotFound
}
return refs, nil
}

// GetComponentReferencesByName returns all component references with a given name.
func (c ComponentDescriptor) GetComponentReferencesByName(name string) ([]ComponentReference, error) {
return c.GetComponentReferences(NewNameSelector(name))
}

// GetResourceByDefaultSelector returns resources that match the given selectors.
func (c ComponentDescriptor) GetResourceByJSONScheme(sel interface{}) ([]Resource, error) {
return nil, nil
func (c ComponentDescriptor) GetResourceByJSONScheme(src interface{}) ([]Resource, error) {
sel, err := selector.NewJSONSchemaSelectorFromGoStruct(src)
if err != nil {
return nil, err
}
return c.GetResourcesBySelector(sel)
}

// GetResourceByDefaultSelector returns resources that match the given selectors.
Expand All @@ -87,11 +114,11 @@ func (c ComponentDescriptor) GetResourceByDefaultSelector(sel interface{}) ([]Re
if err != nil {
return nil, fmt.Errorf("unable to parse selector: %w", err)
}
return c.GetResourceBySelector(identitySelector)
return c.GetResourcesBySelector(identitySelector)
}

// GetResourceBySelector returns resources that match the given selector.
func (c ComponentDescriptor) GetResourceBySelector(selectors ...IdentitySelector) ([]Resource, error) {
// GetResourcesBySelector returns resources that match the given selector.
func (c ComponentDescriptor) GetResourcesBySelector(selectors ...IdentitySelector) ([]Resource, error) {
resources := make([]Resource, 0)
for _, res := range c.Resources {
ok, err := selector.MatchSelectors(res.GetIdentity(), selectors...)
Expand All @@ -108,7 +135,7 @@ func (c ComponentDescriptor) GetResourceBySelector(selectors ...IdentitySelector
return resources, nil
}

// GetResourceBySelector returns resources that match the given selector.
// GetResourcesBySelector returns resources that match the given selector.
func (c ComponentDescriptor) getResourceBySelectors(selectors []IdentitySelector, resourceSelectors []ResourceSelectorFunc) ([]Resource, error) {
resources := make([]Resource, 0)
for _, res := range c.Resources {
Expand All @@ -134,16 +161,6 @@ func (c ComponentDescriptor) getResourceBySelectors(selectors []IdentitySelector
return resources, nil
}

// GetResource returns a external or local resource with the given type, name and version.
func (c ComponentDescriptor) GetResource(rtype, name, version string) (Resource, error) {
for _, res := range c.Resources {
if res.GetType() == rtype && res.GetName() == name && res.GetVersion() == version {
return res, nil
}
}
return Resource{}, NotFound
}

// GetExternalResources returns a external resource with the given type, name and version.
func (c ComponentDescriptor) GetExternalResources(rtype, name, version string) ([]Resource, error) {
return c.getResourceBySelectors(
Expand Down Expand Up @@ -188,35 +205,19 @@ func (c ComponentDescriptor) GetLocalResource(rtype, name, version string) (Reso
return resources[0], nil
}

// GetResourcesByType returns all local and external resources of a specific resource type.
func (c ComponentDescriptor) GetResourcesByType(rtype string) []Resource {
return append(c.GetLocalResourcesByType(rtype), c.GetLocalResourcesByType(rtype)...)
}

// GetLocalResourcesByType returns all local resources of a specific resource type.
func (c ComponentDescriptor) GetLocalResourcesByType(rtype string) []Resource {
return getResourcesByType(c, rtype)
}

// GetExternalResourcesByType returns all external resources of a specific resource type.
func (c ComponentDescriptor) GetExternalResourcesByType(rtype string) []Resource {
return getResourcesByType(c, rtype)
}

func getResourcesByType(c ComponentDescriptor, rtype string) []Resource {
// error can never happen as the type resource selector does not throw an error
res, _ := c.getResourceBySelectors(
[]selector.Interface{},
// GetResourcesByType returns all resources that match the given type and selectors.
func (c ComponentDescriptor) GetResourcesByType(rtype string, selectors ...IdentitySelector) ([]Resource, error) {
return c.getResourceBySelectors(
selectors,
[]ResourceSelectorFunc{
NewTypeResourceSelector(rtype),
})
return res
}

// GetResourcesByType returns all local and external resources of a specific resource type.
func (c ComponentDescriptor) GetResourcesByName(rtype, name string) ([]Resource, error) {
func (c ComponentDescriptor) GetResourcesByName(name string, selectors ...IdentitySelector) ([]Resource, error) {
return c.getResourceBySelectors(
[]selector.Interface{NewNameSelector(name)},
append(selectors, NewNameSelector(name)),
nil)
}

Expand Down
12 changes: 0 additions & 12 deletions bindings-go/apis/v2/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,3 @@ func (c *ComponentDescriptorList) GetComponentByName(name string) []ComponentDes
}
return comps
}

// GetResources returns all resources of a given type, name and version of all components in the list.
func (c *ComponentDescriptorList) GetResources(rtype, name, version string) []Resource {
resources := make([]Resource, 0)
for _, comp := range c.Components {
resource, err := comp.GetResource(rtype, name, version)
if err == nil {
resources = append(resources, resource)
}
}
return resources
}
8 changes: 5 additions & 3 deletions bindings-go/apis/v2/v2_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ var _ = Describe("Helper", func() {
})

It("should select 2 resources by a their type", func() {
res := comp.GetExternalResourcesByType(v2.OCIImageType)
res, err := comp.GetResourcesByType(v2.OCIImageType)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(HaveLen(2))
})

It("should select no resources by a their type", func() {
res := comp.GetExternalResourcesByType(v2.GitType)
Expect(res).To(HaveLen(0))
_, err := comp.GetResourcesByType(v2.GitType)
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(v2.NotFound))
})

})
4 changes: 2 additions & 2 deletions bindings-go/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Encode(obj interface{}) ([]byte, error) {
if err := v2.DefaultComponent(comp); err != nil {
return nil, err
}
return yaml.Marshal(comp)
return json.Marshal(comp)
}

if objType.Elem() == reflect.TypeOf(v2.ComponentDescriptorList{}) {
Expand All @@ -103,7 +103,7 @@ func Encode(obj interface{}) ([]byte, error) {
if err := v2.DefaultList(list); err != nil {
return nil, err
}
return yaml.Marshal(list)
return json.Marshal(list)
}

// todo: implement conversion
Expand Down
Loading

0 comments on commit 4fd6d60

Please sign in to comment.