From 3b7e1cd3d6e68e872cdece66659500506667a68a Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:04:24 +0200 Subject: [PATCH] removed a LOT of deprecated functions from the interface --- api/ocm/add_test.go | 29 +- api/ocm/compdesc/deprecated.go | 156 ---- api/ocm/compdesc/helper.go | 15 +- api/ocm/compdesc/op.go | 91 --- api/ocm/compdesc/selectors.go | 684 ------------------ api/ocm/cpi/compose_test.go | 5 +- api/ocm/cpi/dummy.go | 35 - api/ocm/cpi/repocpi/view_cv.go | 130 ---- .../repositories/comparch/comparch_test.go | 19 +- .../composition/repository_test.go | 5 +- .../repositories/composition/version_test.go | 7 +- api/ocm/internal/repository.go | 28 - .../selector.go => ocm/selectors/json.go} | 14 +- .../02-basic-credential-management.go | 3 +- .../03-working-with-credentials/README.md | 2 +- 15 files changed, 53 insertions(+), 1170 deletions(-) delete mode 100644 api/ocm/compdesc/deprecated.go delete mode 100644 api/ocm/compdesc/op.go delete mode 100644 api/ocm/compdesc/selectors.go rename api/{utils/selector/selector.go => ocm/selectors/json.go} (93%) diff --git a/api/ocm/add_test.go b/api/ocm/add_test.go index 7890d50161..66e6ab709a 100644 --- a/api/ocm/add_test.go +++ b/api/ocm/add_test.go @@ -4,6 +4,7 @@ import ( . "github.com/mandelsoft/goutils/testutils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "ocm.software/ocm/api/ocm/selectors" . "ocm.software/ocm/api/ocm/testhelper" "ocm.software/ocm/api/datacontext" @@ -38,7 +39,7 @@ var _ = Describe("add resources", func() { meta := ocm.NewResourceMeta("test", resourcetypes.PLAIN_TEXT, metav1.ExternalRelation) MustBeSuccessful(cv.SetResourceBlob(meta.WithVersion("v1"), blobaccess.ForString(mime.MIME_TEXT, S_TESTDATA), "", nil)) - Expect(Must(cv.GetResourcesByName("test"))[0].Meta().Digest).To(Equal(DS_TESTDATA)) + Expect(Must(cv.SelectResources(selectors.Name("test")))[0].Meta().Digest).To(Equal(DS_TESTDATA)) }) It("replaces resource", func() { @@ -48,7 +49,7 @@ var _ = Describe("add resources", func() { MustBeSuccessful(cv.SetResourceBlob(meta.WithVersion("v1"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil)) - Expect(Must(cv.GetResourcesByName("test"))[0].Meta().Digest).To(Equal(DS_OTHERDATA)) + Expect(Must(cv.SelectResources(selectors.Name("test")))[0].Meta().Digest).To(Equal(DS_OTHERDATA)) }) It("replaces resource (enforced)", func() { @@ -58,11 +59,11 @@ var _ = Describe("add resources", func() { MustBeSuccessful(cv.SetResourceBlob(meta.WithVersion("v1"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil, ocm.UpdateElement)) - Expect(Must(cv.GetResourcesByName("test"))[0].Meta().Digest).To(Equal(DS_OTHERDATA)) + Expect(Must(cv.SelectResources(selectors.Name("test")))[0].Meta().Digest).To(Equal(DS_OTHERDATA)) MustBeSuccessful(cv.SetResourceBlob(meta.WithVersion("v2"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil, ocm.UpdateElement)) - Expect(Must(cv.GetResourcesByName("test"))[0].Meta().Digest).To(Equal(DS_OTHERDATA)) + Expect(Must(cv.SelectResources(selectors.Name("test")))[0].Meta().Digest).To(Equal(DS_OTHERDATA)) }) It("fails replace non-existent resource)", func() { @@ -81,9 +82,9 @@ var _ = Describe("add resources", func() { blobaccess.ForString(mime.MIME_TEXT, S_TESTDATA), "", nil)) MustBeSuccessful(cv.SetResourceBlob(meta.WithVersion("v2"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil, ocm.AppendElement)) - Expect(len(Must(cv.GetResourcesByName("test")))).To(Equal(2)) - Expect(Must(cv.GetResourcesByName("test"))[0].Meta().Digest).To(Equal(DS_TESTDATA)) - Expect(Must(cv.GetResourcesByName("test"))[1].Meta().Digest).To(Equal(DS_OTHERDATA)) + Expect(len(Must(cv.SelectResources(selectors.Name("test"))))).To(Equal(2)) + Expect(Must(cv.SelectResources(selectors.Name("test")))[0].Meta().Digest).To(Equal(DS_TESTDATA)) + Expect(Must(cv.SelectResources(selectors.Name("test")))[1].Meta().Digest).To(Equal(DS_OTHERDATA)) }) It("rejects duplicate resource with same version", func() { @@ -120,7 +121,7 @@ var _ = Describe("add resources", func() { MustBeSuccessful(cv.SetSourceBlob(meta.WithVersion("v1"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil)) - Expect(len(Must(cv.GetSourcesByName("test")))).To(Equal(1)) + Expect(len(Must(cv.SelectSources(selectors.Name("test"))))).To(Equal(1)) }) It("replaces source (enforced)", func() { @@ -130,11 +131,11 @@ var _ = Describe("add resources", func() { MustBeSuccessful(cv.SetSourceBlob(meta.WithVersion("v1"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil, ocm.UpdateElement)) - Expect(len(Must(cv.GetSourcesByName("test")))).To(Equal(1)) + Expect(len(Must(cv.SelectSources(selectors.Name("test"))))).To(Equal(1)) MustBeSuccessful(cv.SetSourceBlob(meta.WithVersion("v2"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil, ocm.UpdateElement)) - Expect(len(Must(cv.GetSourcesByName("test")))).To(Equal(1)) + Expect(len(Must(cv.SelectSources(selectors.Name("test"))))).To(Equal(1)) }) It("fails replace non-existent source)", func() { @@ -153,7 +154,7 @@ var _ = Describe("add resources", func() { blobaccess.ForString(mime.MIME_TEXT, S_TESTDATA), "", nil)) MustBeSuccessful(cv.SetSourceBlob(meta.WithVersion("v2"), blobaccess.ForString(mime.MIME_TEXT, S_OTHERDATA), "", nil, ocm.AppendElement)) - Expect(len(Must(cv.GetSourcesByName("test")))).To(Equal(2)) + Expect(len(Must(cv.SelectSources(selectors.Name("test"))))).To(Equal(2)) }) It("rejects duplicate source with same version", func() { @@ -187,7 +188,7 @@ var _ = Describe("add resources", func() { MustBeSuccessful(cv.SetReference(ref)) MustBeSuccessful(cv.SetReference(ref.WithVersion("v1"))) - Expect(len(Must(cv.GetReferencesByName("test")))).To(Equal(1)) + Expect(len(Must(cv.SelectReferences(selectors.Name("test"))))).To(Equal(1)) }) It("replaces source (enforced)", func() { @@ -195,7 +196,7 @@ var _ = Describe("add resources", func() { MustBeSuccessful(cv.SetReference(ref)) MustBeSuccessful(cv.SetReference(ref.WithVersion("v2"))) - Expect(len(Must(cv.GetReferencesByName("test")))).To(Equal(1)) + Expect(len(Must(cv.SelectReferences(selectors.Name("test"))))).To(Equal(1)) }) It("fails replace non-existent source)", func() { @@ -210,7 +211,7 @@ var _ = Describe("add resources", func() { ref := ocm.NewComponentReference("test", COMPONENT+"/sub", "v1") MustBeSuccessful(cv.SetReference(ref)) MustBeSuccessful(cv.SetReference(ref.WithVersion("v2"), ocm.AppendElement)) - Expect(len(Must(cv.GetReferencesByName("test")))).To(Equal(2)) + Expect(len(Must(cv.SelectReferences(selectors.Name("test"))))).To(Equal(2)) }) It("rejects duplicate reference with same version", func() { diff --git a/api/ocm/compdesc/deprecated.go b/api/ocm/compdesc/deprecated.go deleted file mode 100644 index 631f9c9ddc..0000000000 --- a/api/ocm/compdesc/deprecated.go +++ /dev/null @@ -1,156 +0,0 @@ -package compdesc - -import ( - "bytes" - "fmt" - - "github.com/mandelsoft/goutils/sliceutils" - - v1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" - "ocm.software/ocm/api/utils/selector" -) - -// GetResourcesByIdentitySelectors returns resources that match the given identity selectors. -// Deprecated: use GetResources with appropriate selectors. -func (cd *ComponentDescriptor) GetResourcesByIdentitySelectors(selectors ...IdentitySelector) (Resources, error) { - return cd.GetResourcesBySelectors(selectors, nil) -} - -// GetResourcesBySelectors returns resources that match the given selector. -// -// Deprecated: use GetResources with appropriate selectors. -func (cd *ComponentDescriptor) GetResourcesBySelectors(selectors []IdentitySelector, resourceSelectors []ResourceSelector) (Resources, error) { - resources := make(Resources, 0) - for i := range cd.Resources { - selctx := NewResourceSelectionContext(i, cd.Resources) - if len(selectors) > 0 { - ok, err := selector.MatchSelectors(selctx.Identity(), selectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - } - ok, err := MatchResourceByResourceSelector(selctx, resourceSelectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - resources = append(resources, *selctx.Resource) - } - if len(resources) == 0 { - return resources, NotFound - } - return resources, nil -} - -// GetResourcesByName returns all local and external resources with a name. -// -// Deprecated: use GetResources with appropriate selectors. -func (cd *ComponentDescriptor) GetResourcesByName(name string, selectors ...IdentitySelector) (Resources, error) { - return cd.GetResourcesBySelectors( - sliceutils.CopyAppend[IdentitySelector](selectors, ByName(name)), - nil) -} - -// GetSourcesByIdentitySelectors returns references that match the given selector. -// -// Deprecated: use GetSources with appropriate selectors. -func (cd *ComponentDescriptor) GetSourcesByIdentitySelectors(selectors ...IdentitySelector) (Sources, error) { - srcs := make(Sources, 0) - for _, src := range cd.Sources { - ok, err := selector.MatchSelectors(src.GetIdentity(cd.Sources), selectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for source %s: %w", src.Name, err) - } - if ok { - srcs = append(srcs, src) - } - } - if len(srcs) == 0 { - return srcs, NotFound - } - return srcs, nil -} - -// GetSourcesByName returns all sources with a name. -// -// Deprecated: use GetSources with appropriate selectors. -func (cd *ComponentDescriptor) GetSourcesByName(name string, selectors ...IdentitySelector) (Sources, error) { - return cd.GetSourcesByIdentitySelectors( - sliceutils.CopyAppend[IdentitySelector](selectors, ByName(name))...) -} - -//////////////////////////////////////////////////////////////////////////////// - -// GetComponentReferenceIndex returns the index of a given component reference. -// If the index is not found -1 is returned. -// Deprecated: use GetReferenceIndex. -func (cd *ComponentDescriptor) GetComponentReferenceIndex(ref Reference) int { - return cd.GetReferenceIndex(ref.GetMeta()) -} - -// GetReferenceAccessByIdentity returns a pointer to the reference that matches the given identity. -// Deprectated: use GetReferenceByIdentity. -func (cd *ComponentDescriptor) GetReferenceAccessByIdentity(id v1.Identity) *Reference { - dig := id.Digest() - for i, ref := range cd.References { - if bytes.Equal(ref.GetIdentityDigest(cd.Resources), dig) { - return &cd.References[i] - } - } - return nil -} - -// GetReferencesByIdentitySelectors returns resources that match the given identity selectors. -// Deprectated: use GetReferences with appropriate selectors. -func (cd *ComponentDescriptor) GetReferencesByIdentitySelectors(selectors ...IdentitySelector) (References, error) { - return cd.GetReferencesBySelectors(selectors, nil) -} - -// GetReferencesByReferenceSelectors returns resources that match the given resource selectors. -// Deprectated: use GetReferences with appropriate selectors. -func (cd *ComponentDescriptor) GetReferencesByReferenceSelectors(selectors ...ReferenceSelector) (References, error) { - return cd.GetReferencesBySelectors(nil, selectors) -} - -// GetReferencesBySelectors returns resources that match the given selector. -// Deprectated: use GetReferences with appropriate selectors. -func (cd *ComponentDescriptor) GetReferencesBySelectors(selectors []IdentitySelector, referenceSelectors []ReferenceSelector) (References, error) { - references := make(References, 0) - for i := range cd.References { - selctx := NewReferenceSelectionContext(i, cd.References) - if len(selectors) > 0 { - ok, err := selector.MatchSelectors(selctx.Identity(), selectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - } - ok, err := MatchReferencesByReferenceSelector(selctx, referenceSelectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - references = append(references, *selctx.Reference) - } - if len(references) == 0 { - return references, NotFound - } - return references, nil -} - -// GetReferencesByName returns references that match the given name. -// Deprectated: use GetReferences with appropriate selectors. -func (cd *ComponentDescriptor) GetReferencesByName(name string, selectors ...IdentitySelector) (References, error) { - return cd.GetReferencesBySelectors( - sliceutils.CopyAppend[IdentitySelector](selectors, ByName(name)), - nil) -} diff --git a/api/ocm/compdesc/helper.go b/api/ocm/compdesc/helper.go index fa48889fe5..dfd3add225 100644 --- a/api/ocm/compdesc/helper.go +++ b/api/ocm/compdesc/helper.go @@ -2,7 +2,6 @@ package compdesc import ( "bytes" - "fmt" "github.com/mandelsoft/goutils/errors" @@ -12,7 +11,6 @@ import ( "ocm.software/ocm/api/ocm/selectors/rscsel" "ocm.software/ocm/api/ocm/selectors/srcsel" "ocm.software/ocm/api/utils/runtime" - "ocm.software/ocm/api/utils/selector" ) // GetEffectiveRepositoryContext returns the currently active repository context. @@ -94,20 +92,11 @@ func (cd *ComponentDescriptor) GetResourceIndexByIdentity(id v1.Identity) int { // GetResourceByJSONScheme returns resources that match the given selectors. func (cd *ComponentDescriptor) GetResourceByJSONScheme(src interface{}) (Resources, error) { - sel, err := selector.NewJSONSchemaSelectorFromGoStruct(src) + sel, err := selectors.NewJSONSchemaSelectorFromGoStruct(src) if err != nil { return nil, err } - return cd.GetResourcesByIdentitySelectors(sel) -} - -// GetResourceByDefaultSelector returns resources that match the given selectors. -func (cd *ComponentDescriptor) GetResourceByDefaultSelector(sel interface{}) (Resources, error) { - identitySelector, err := selector.ParseDefaultSelector(sel) - if err != nil { - return nil, fmt.Errorf("unable to parse selector: %w", err) - } - return cd.GetResourcesByIdentitySelectors(identitySelector) + return cd.SelectResources(sel) } // GetResourceIndex returns the index of a given resource. diff --git a/api/ocm/compdesc/op.go b/api/ocm/compdesc/op.go deleted file mode 100644 index 6289e4e5a5..0000000000 --- a/api/ocm/compdesc/op.go +++ /dev/null @@ -1,91 +0,0 @@ -package compdesc - -import ( - "reflect" - - "github.com/mandelsoft/goutils/sliceutils" -) - -// unfortunately this does not work as expected in Go, because result parameters -// are not used for type inference. Therefore, these methods cannot be used -// without specifying the type parameter. - -func And[T any](sel ...any) T { - var r T - h := selhandler[reflect.TypeOf(r)].(handler[T]) - return h.And(sliceutils.Convert[T](sel)...) -} - -func Or[T any](sel ...any) T { - var r T - h := selhandler[reflect.TypeOf(r)].(handler[T]) - return h.Or(sliceutils.Convert[T](sel)...) -} - -func Not[T any](sel any) T { - var r T - h := selhandler[reflect.TypeOf(r)].(handler[T]) - return h.Not(sel.(T)) -} - -//////////////////////////////////////////////////////////////////////////////// - -type handler[T any] interface { - And(sel ...T) T - Or(sel ...T) T - Not(sel T) T -} - -var ( - l_type LabelSelector - r_type ResourceSelector - c_type ReferenceSelector -) - -var selhandler = map[reflect.Type]any{ - reflect.TypeOf(l_type): l_handler{}, - reflect.TypeOf(r_type): r_handler{}, - reflect.TypeOf(c_type): c_handler{}, -} - -type l_handler struct{} - -func (h l_handler) And(sel ...LabelSelector) LabelSelector { - return AndL(sel...) -} - -func (h l_handler) Or(sel ...LabelSelector) LabelSelector { - return OrL(sel...) -} - -func (h l_handler) Not(sel LabelSelector) LabelSelector { - return NotL(sel) -} - -type r_handler struct{} - -func (h r_handler) And(sel ...ResourceSelector) ResourceSelector { - return AndR(sel...) -} - -func (h r_handler) Or(sel ...ResourceSelector) ResourceSelector { - return OrR(sel...) -} - -func (h r_handler) Not(sel ResourceSelector) ResourceSelector { - return NotR(sel) -} - -type c_handler struct{} - -func (h c_handler) And(sel ...ReferenceSelector) ReferenceSelector { - return AndC(sel...) -} - -func (h c_handler) Or(sel ...ReferenceSelector) ReferenceSelector { - return OrC(sel...) -} - -func (h c_handler) Not(sel ReferenceSelector) ReferenceSelector { - return NotC(sel) -} diff --git a/api/ocm/compdesc/selectors.go b/api/ocm/compdesc/selectors.go deleted file mode 100644 index 0e079d3ec4..0000000000 --- a/api/ocm/compdesc/selectors.go +++ /dev/null @@ -1,684 +0,0 @@ -package compdesc - -import ( - "encoding/json" - "reflect" - "runtime" - - v1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" - resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes" - "ocm.software/ocm/api/ocm/extraid" - "ocm.software/ocm/api/utils" - "ocm.software/ocm/api/utils/selector" -) - -// Deprecated: use package selectors and its sub packages. -type IdentityBasedSelector interface { - IdentitySelector - ElementSelector - ResourceSelector - ReferenceSelector -} - -// Deprecated: use package selectors and its sub packages. -type ElementBasedSelector interface { - ElementSelector - ResourceSelector - ReferenceSelector -} - -// Deprecated: use package selectors and its sub packages. -type LabelBasedSelector interface { - LabelSelector - ElementSelector - ResourceSelector - ReferenceSelector -} - -//////////////////////////////////////////////////////////////////////////////// - -// Deprecated: use package selectors and its sub packages. -type IdentitySelector = selector.Interface - -type byVersion struct { - version string -} - -var _ IdentityBasedSelector = (*byVersion)(nil) - -func (b *byVersion) Match(obj map[string]string) (bool, error) { - return obj[SystemIdentityVersion] == b.version, nil -} - -func (b *byVersion) MatchElement(obj ElementSelectionContext) (bool, error) { - return obj.GetVersion() == b.version, nil -} - -func (b *byVersion) MatchResource(obj ResourceSelectionContext) (bool, error) { - return obj.GetVersion() == b.version, nil -} - -func (b *byVersion) MatchReference(obj ReferenceSelectionContext) (bool, error) { - return obj.GetVersion() == b.version, nil -} - -// ByVersion creates a new selector that -// selects an element based on its version. -// -// Deprecated: use package selectors and its sub packages. -func ByVersion(version string) IdentityBasedSelector { - return &byVersion{version: version} -} - -type byName struct { - name string -} - -var _ IdentityBasedSelector = (*byName)(nil) - -func (b *byName) Match(obj map[string]string) (bool, error) { - return obj[SystemIdentityName] == b.name, nil -} - -func (b *byName) MatchElement(obj ElementSelectionContext) (bool, error) { - return obj.GetName() == b.name, nil -} - -func (b *byName) MatchResource(obj ResourceSelectionContext) (bool, error) { - return obj.GetName() == b.name, nil -} - -func (b *byName) MatchReference(obj ReferenceSelectionContext) (bool, error) { - return obj.GetName() == b.name, nil -} - -// ByName creates a new selector that -// selects an element based on its name. -// -// Deprecated: use package selectors and its sub packages. -func ByName(name string) IdentityBasedSelector { - return &byName{name: name} -} - -type byIdentity struct { - id v1.Identity - partial bool -} - -var _ IdentityBasedSelector = (*byIdentity)(nil) - -func (b *byIdentity) Match(obj map[string]string) (bool, error) { - if !b.partial && len(b.id) != len(obj) { - return false, nil - } - for k, v := range b.id { - e, ok := obj[k] - if !ok || e != v { - return false, nil - } - } - return true, nil -} - -func (b *byIdentity) MatchElement(obj ElementSelectionContext) (bool, error) { - return b.Match(obj.Identity()) -} - -func (b *byIdentity) MatchResource(obj ResourceSelectionContext) (bool, error) { - return b.Match(obj.Identity()) -} - -func (b *byIdentity) MatchReference(obj ReferenceSelectionContext) (bool, error) { - return b.Match(obj.Identity()) -} - -// ByIdentity creates a new resource and identity selector that -// selects a resource based on its identity. -// -// Deprecated: use package selectors and its sub packages. -func ByIdentity(name string, extras ...string) IdentityBasedSelector { - id := v1.NewIdentity(name, extras...) - return &byIdentity{id: id} -} - -// ByPartialIdentity creates a new resource and identity selector that -// selects a resource based on its partial identity. -// All given attributes must match, but potential additional attributes -// of a resource identity are ignored. -// -// Deprecated: use package selectors and its sub packages. -func ByPartialIdentity(name string, extras ...string) IdentityBasedSelector { - id := v1.NewIdentity(name, extras...) - return &byIdentity{id: id, partial: true} -} - -type withExtraId struct { - ids v1.Identity -} - -var _ ElementBasedSelector = (*withExtraId)(nil) - -func (b *withExtraId) Match(obj map[string]string) (bool, error) { - if len(obj) == 0 { - return len(b.ids) == 0, nil - } - for id, v := range b.ids { - if obj[id] != v { - return false, nil - } - } - return true, nil -} - -func (b *withExtraId) MatchElement(obj ElementSelectionContext) (bool, error) { - return b.Match(obj.ExtraIdentity) -} - -func (b *withExtraId) MatchResource(obj ResourceSelectionContext) (bool, error) { - return b.Match(obj.ExtraIdentity) -} - -func (b *withExtraId) MatchReference(obj ReferenceSelectionContext) (bool, error) { - return b.Match(obj.ExtraIdentity) -} - -// WithExtraIdentity creates a new selector that -// selects an element based on extra identities. -// -// Deprecated: use package selectors and its sub packages. -func WithExtraIdentity(args ...string) IdentityBasedSelector { - ids := v1.Identity{} - for i := 0; i < len(args); i += 2 { - if i+1 < len(args) { - ids[args[i]] = args[i+1] - } - } - return &withExtraId{ids: ids} -} - -//////////////////////////////////////////////////////////////////////////////// - -// ResourceSelectorFunc defines a function to filter a resource. -// -// Deprecated: use package selectors and its sub packages. -type ResourceSelectorFunc func(obj ResourceSelectionContext) (bool, error) - -var _ ResourceSelector = ResourceSelectorFunc(nil) - -func (s ResourceSelectorFunc) MatchResource(obj ResourceSelectionContext) (bool, error) { - return s(obj) -} - -type resourceSelectionContext struct { - *Resource - identity -} - -// Deprecated: use package selectors and its sub packages. -func NewResourceSelectionContext(index int, rscs Resources) ResourceSelectionContext { - return &resourceSelectionContext{ - Resource: &rscs[index], - identity: identity{ - accessor: rscs, - index: index, - }, - } -} - -// ResourceSelectionContext describes the selction context for a resource -// selector. It contains the resource and provides access to its -// identity in the context of its component descriptor. -// -// Deprecated: use package selectors and its sub packages. -type ResourceSelectionContext = *resourceSelectionContext - -// ResourceSelector defines a selector based on resource attributes. -// -// Deprecated: use package selectors and its sub packages. -type ResourceSelector interface { - MatchResource(obj ResourceSelectionContext) (bool, error) -} - -// MatchResourceByResourceSelector applies all resource selector against the given resource object. -// -// Deprecated: use package selectors and its sub packages. -func MatchResourceByResourceSelector(obj ResourceSelectionContext, resourceSelectors ...ResourceSelector) (bool, error) { - for _, sel := range resourceSelectors { - ok, err := sel.MatchResource(obj) - if err != nil { - return false, err - } - if !ok { - return false, nil - } - } - return true, nil -} - -// AndR is an AND resource selector. -// -// Deprecated: use package labelsel. -func AndR(sel ...ResourceSelector) ResourceSelector { - return ResourceSelectorFunc(func(obj ResourceSelectionContext) (bool, error) { - for _, s := range sel { - ok, err := s.MatchResource(obj) - if !ok || err != nil { - return ok, err - } - } - return true, nil - }) -} - -// OrR is an OR resource selector. -// -// Deprecated: use package labelsel. -func OrR(sel ...ResourceSelector) ResourceSelector { - return ResourceSelectorFunc(func(obj ResourceSelectionContext) (bool, error) { - for _, s := range sel { - ok, err := s.MatchResource(obj) - if ok || err != nil { - return ok, err - } - } - return false, nil - }) -} - -// NotR is a negated resource selector. -// -// Deprecated: use package labelsel. -func NotR(sel ResourceSelector) ResourceSelector { - return ResourceSelectorFunc(func(obj ResourceSelectionContext) (bool, error) { - ok, err := sel.MatchResource(obj) - if err != nil { - return false, err - } - return !ok, nil - }) -} - -// ByResourceType creates a new resource selector that -// selects a resource based on its type. -// -// Deprecated: use package selectors and its sub packages. -func ByResourceType(ttype string) ResourceSelector { - return ResourceSelectorFunc(func(obj ResourceSelectionContext) (bool, error) { - return ttype == "" || obj.GetType() == ttype, nil - }) -} - -// ByRelation creates a new resource selector that -// selects a resource based on its relation type. -// -// Deprecated: use package selectors and its sub packages. -func ByRelation(relation v1.ResourceRelation) ResourceSelectorFunc { - return ResourceSelectorFunc(func(obj ResourceSelectionContext) (bool, error) { - return obj.Relation == relation, nil - }) -} - -// ByAccessMethod creates a new selector that matches a resource access method type. -// -// Deprecated: use package selectors and its sub packages. -func ByAccessMethod(name string) ResourceSelector { - return ResourceSelectorFunc(func(obj ResourceSelectionContext) (bool, error) { - if obj.Access == nil { - return name == "", nil - } - return obj.Access.GetType() == name || obj.Access.GetKind() == name, nil - }) -} - -// ForExecutable creates a new selector that matches a resource for an executable. -// -// Deprecated: use package selectors and its sub packages. -func ForExecutable(name string) ResourceSelector { - return ResourceSelectorFunc(func(obj ResourceSelectionContext) (bool, error) { - return obj.Name == name && obj.Type == resourcetypes.EXECUTABLE && obj.ExtraIdentity != nil && - obj.ExtraIdentity[extraid.ExecutableOperatingSystem] == runtime.GOOS && - obj.ExtraIdentity[extraid.ExecutableArchitecture] == runtime.GOARCH, nil - }) -} - -//////////////////////////////////////////////////////////////////////////////// - -// LabelSelector is used to match a label in a label set. -// -// Deprecated: use package selectors and its sub packages. -type LabelSelector interface { - MatchLabel(l v1.Label) (bool, error) -} - -// LabelSelectorFunc is a function used as LabelSelector. -// -// Deprecated: use package selectors and its sub packages. -type LabelSelectorFunc func(l v1.Label) (bool, error) - -func (l LabelSelectorFunc) MatchLabel(label v1.Label) (bool, error) { - return l(label) -} - -// AndL is an AND label selector. -// -// Deprecated: use package selectors and its sub packages. -func AndL(sel ...LabelSelector) LabelSelector { - return LabelSelectorFunc(func(obj v1.Label) (bool, error) { - for _, s := range sel { - ok, err := s.MatchLabel(obj) - if !ok || err != nil { - return ok, err - } - } - return true, nil - }) -} - -// OrL is an OR label selector. -// -// Deprecated: use package selectors and its sub packages. -func OrL(sel ...LabelSelector) LabelSelector { - return LabelSelectorFunc(func(obj v1.Label) (bool, error) { - for _, s := range sel { - ok, err := s.MatchLabel(obj) - if ok || err != nil { - return ok, err - } - } - return false, nil - }) -} - -// NotL is a negated label selector. -// -// Deprecated: use package selectors and its sub packages. -func NotL(sel LabelSelector) LabelSelector { - return LabelSelectorFunc(func(obj v1.Label) (bool, error) { - ok, err := sel.MatchLabel(obj) - if err != nil { - return false, err - } - return !ok, nil - }) -} - -type byLabel struct { - selector LabelSelector -} - -var _ LabelBasedSelector = (*byLabel)(nil) - -func (b *byLabel) MatchElement(obj ElementSelectionContext) (bool, error) { - return b.MatchLabels(obj.Labels) -} - -func (b *byLabel) MatchResource(obj ResourceSelectionContext) (bool, error) { - return b.MatchLabels(obj.Labels) -} - -func (b *byLabel) MatchReference(obj ReferenceSelectionContext) (bool, error) { - return b.MatchLabels(obj.Labels) -} - -func (b *byLabel) MatchLabels(obj v1.Labels) (bool, error) { - for _, l := range obj { - if ok, err := b.selector.MatchLabel(l); ok || err != nil { - return true, nil - } - } - return false, nil -} - -func (b *byLabel) MatchLabel(l v1.Label) (bool, error) { - return b.selector.MatchLabel(l) -} - -// ByLabel matches a resource or element for a list of given label selectors -// matching the same label. -// If multiple label related selectors should be used, they should -// be grouped into a single label selector to be applied in -// combination. Otherwise, a resource might match if the label -// selectors all match, but different labels. -// -// Deprecated: use package selectors and its sub packages. -func ByLabel(sel ...LabelSelector) LabelBasedSelector { - return &byLabel{selector: LabelSelectorFunc(func(l v1.Label) (bool, error) { - return MatchLabels(v1.Labels{l}, sel...) - })} -} - -// ByLabelName matches an element by a label name. -// -// Deprecated: use package selectors and its sub packages. -func ByLabelName(name string) LabelBasedSelector { - return &byLabel{selector: LabelSelectorFunc(func(l v1.Label) (bool, error) { return l.Name == name, nil })} -} - -// ByLabelValue matches a resource or label by a label value. -// This selector should typically be combined with ByLabelName. -// -// Deprecated: use package selectors and its sub packages. -func ByLabelValue(value interface{}) LabelBasedSelector { - return &byLabel{selector: LabelSelectorFunc(func(l v1.Label) (bool, error) { - var data interface{} - if err := json.Unmarshal(l.Value, &data); err != nil { - return false, err - } - return reflect.DeepEqual(data, value), nil - })} -} - -// ByLabelVersion matches a resource or label by a label version. -// This selector should typically be combined with ByLabelName. -// -// Deprecated: use package selectors and its sub packages. -func ByLabelVersion(version string) LabelBasedSelector { - return &byLabel{selector: LabelSelectorFunc(func(l v1.Label) (bool, error) { return l.Version == version, nil })} -} - -// BySignedLabel matches a resource or label by a label indicated to be signed. -// This selector should typically be combined with ByLabelName. -// -// Deprecated: use package selectors and its sub packages. -func BySignedLabel(flags ...bool) LabelBasedSelector { - flag := utils.OptionalDefaultedBool(true, flags...) - return &byLabel{selector: LabelSelectorFunc(func(l v1.Label) (bool, error) { return l.Signing == flag, nil })} -} - -// MatchLabels checks whether a set of labels matches the given label selectors. -// -// Deprecated: use package selectors and its sub packages. -func MatchLabels(labels v1.Labels, sel ...LabelSelector) (bool, error) { - if len(labels) == 0 && len(sel) == 0 { - return true, nil - } - found := false -outer: - for _, l := range labels { - for _, s := range sel { - ok, err := s.MatchLabel(l) - if err != nil { - return false, err - } - if !ok { - continue outer - } - } - found = true - break - } - - return found, nil -} - -// SelectLabels returns labels matching the given label selectors. -// -// Deprecated: use package selectors and its sub packages. -func SelectLabels(labels v1.Labels, sel ...LabelSelector) (v1.Labels, error) { - list := make(v1.Labels, 0) -outer: - for _, l := range labels { - for _, s := range sel { - ok, err := s.MatchLabel(l) - if err != nil { - return nil, err - } - if !ok { - continue outer - } - } - list = append(list, l) - } - - return list, nil -} - -// MatchReferencesByReferenceSelector applies all resource selector against the given resource object. -// -// Deprecated: use package selectors and its sub packages. -func MatchReferencesByReferenceSelector(obj ReferenceSelectionContext, resourceSelectors ...ReferenceSelector) (bool, error) { - for _, sel := range resourceSelectors { - ok, err := sel.MatchReference(obj) - if err != nil { - return false, err - } - if !ok { - return false, nil - } - } - return true, nil -} - -//////////////////////////////////////////////////////////////////////////////// - -type elementSelectionContext struct { - *ElementMeta - identity -} - -// Deprecated: not supported anymore. -type ElementSelectionContext = *elementSelectionContext - -// Deprecated: not supported anymore. -func NewElementSelectionContext(index int, elems ElementAccessor) ElementSelectionContext { - return &elementSelectionContext{ - ElementMeta: elems.Get(index).GetMeta(), - identity: identity{ - accessor: elems, - index: index, - }, - } -} - -// Deprecated: use package selectors and its sub packages. -type ElementSelector interface { - MatchElement(obj ElementSelectionContext) (bool, error) -} - -//////////////////////////////////////////////////////////////////////////////// - -// ReferenceSelectorFunc defines a function to filter a resource. -// -// Deprecated: use package selectors and its sub packages. -type ReferenceSelectorFunc func(obj ReferenceSelectionContext) (bool, error) - -var _ ReferenceSelector = ReferenceSelectorFunc(nil) - -func (s ReferenceSelectorFunc) MatchReference(obj ReferenceSelectionContext) (bool, error) { - return s(obj) -} - -type referenceSelectionContext struct { - *Reference - identity -} - -// Deprecated: use package selectors and its sub packages. -func NewReferenceSelectionContext(index int, refs References) ReferenceSelectionContext { - return &referenceSelectionContext{ - Reference: &refs[index], - identity: identity{ - accessor: refs, - index: index, - }, - } -} - -// ReferenceSelectionContext describes the selection context for a reference -// selector. It contains the reference and provides access to its -// identity in the context of its component descriptor. -// -// Deprecated: use package selectors and its sub packages. -type ReferenceSelectionContext = *referenceSelectionContext - -// ReferenceSelector defines a selector based on reference attributes. -// -// Deprecated: use package selectors and its sub packages. -type ReferenceSelector interface { - MatchReference(obj ReferenceSelectionContext) (bool, error) -} - -// AndC is an AND reference selector. -// -// Deprecated: use package selectors and its sub packages. -func AndC(sel ...ReferenceSelector) ReferenceSelector { - return ReferenceSelectorFunc(func(obj ReferenceSelectionContext) (bool, error) { - for _, s := range sel { - ok, err := s.MatchReference(obj) - if !ok || err != nil { - return ok, err - } - } - return true, nil - }) -} - -// OrC is an OR resource selector. -// -// Deprecated: use package selectors and its sub packages. -func OrC(sel ...ReferenceSelector) ReferenceSelector { - return ReferenceSelectorFunc(func(obj ReferenceSelectionContext) (bool, error) { - for _, s := range sel { - ok, err := s.MatchReference(obj) - if ok || err != nil { - return ok, err - } - } - return false, nil - }) -} - -// NotC is a negated resource selector. -// -// Deprecated: use package selectors and its sub packages. -func NotC(sel ReferenceSelector) ReferenceSelector { - return ReferenceSelectorFunc(func(obj ReferenceSelectionContext) (bool, error) { - ok, err := sel.MatchReference(obj) - if err != nil { - return false, err - } - return !ok, nil - }) -} - -// Deprecated: use package selectors and its sub packages. -func ByComponent(name string) ReferenceSelector { - return ReferenceSelectorFunc(func(obj ReferenceSelectionContext) (bool, error) { - return obj.ComponentName == name, nil - }) -} - -//////////////////////////////////////////////////////////////////////////////// - -type identity struct { - identity v1.Identity - accessor ElementAccessor - index int -} - -func (i *identity) Identity() v1.Identity { - if i.identity == nil { - i.identity = i.accessor.Get(i.index).GetMeta().GetIdentity(i.accessor) - } - return i.identity -} diff --git a/api/ocm/cpi/compose_test.go b/api/ocm/cpi/compose_test.go index 29c9161b97..4b8d9194e0 100644 --- a/api/ocm/cpi/compose_test.go +++ b/api/ocm/cpi/compose_test.go @@ -5,6 +5,7 @@ import ( . "github.com/mandelsoft/goutils/testutils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "ocm.software/ocm/api/ocm/selectors" . "ocm.software/ocm/api/ocm/testhelper" "github.com/mandelsoft/vfs/pkg/memoryfs" @@ -63,7 +64,7 @@ var _ = Describe("access method", func() { cv = Must(a.LookupComponentVersion(COMPONENT, VERSION)) final.Close(cv) - Expect(Must(cv.GetResourcesByName("text1"))[0].Meta().Digest).To(Equal(DS_TESTDATA)) + Expect(Must(cv.SelectResources(selectors.Name("text1")))[0].Meta().Digest).To(Equal(DS_TESTDATA)) }, Entry("direct", false), Entry("compose", true), @@ -104,7 +105,7 @@ var _ = Describe("access method", func() { cv = Must(a2.LookupComponentVersion(COMPONENT, VERSION)) final.Close(cv) - Expect(Must(cv.GetResourcesByName("text1"))[0].Meta().Digest).To(Equal(DS_TESTDATA)) + Expect(Must(cv.SelectResources(selectors.Name("text1")))[0].Meta().Digest).To(Equal(DS_TESTDATA)) }, Entry("direct", false), Entry("compose", true), diff --git a/api/ocm/cpi/dummy.go b/api/ocm/cpi/dummy.go index a5e73bf595..7181a35c05 100644 --- a/api/ocm/cpi/dummy.go +++ b/api/ocm/cpi/dummy.go @@ -89,11 +89,6 @@ func (d *DummyComponentVersionAccess) GetResourceByIndex(i int) (ResourceAccess, return nil, errors.ErrInvalid("resource index", strconv.Itoa(i)) } -// Deprecated: use GetResources. -func (d *DummyComponentVersionAccess) GetResourcesByName(name string, selectors ...compdesc.IdentitySelector) ([]ResourceAccess, error) { - return nil, errors.ErrInvalid("resource", name) -} - func (d *DummyComponentVersionAccess) SelectSources(sel ...srcsel.Selector) ([]SourceAccess, error) { return nil, nil } @@ -134,11 +129,6 @@ func (d *DummyComponentVersionAccess) GetReferenceByIndex(i int) (ComponentRefer return ComponentReference{}, errors.ErrInvalid("reference index", strconv.Itoa(i)) } -// Deprecated: use GetSources. -func (d *DummyComponentVersionAccess) GetSourcesByName(name string, selectors ...compdesc.IdentitySelector) ([]SourceAccess, error) { - return nil, errors.ErrInvalid("source", name) -} - func (d *DummyComponentVersionAccess) AccessMethod(spec AccessSpec) (AccessMethod, error) { if spec.IsLocal(d.Context) { return nil, errors.ErrNotSupported("local access method") @@ -200,28 +190,3 @@ func (d *DummyComponentVersionAccess) IsPersistent() bool { func (d *DummyComponentVersionAccess) UseDirectAccess() bool { return true } - -// Deprecated: use GetResources. -func (d *DummyComponentVersionAccess) GetResourcesByIdentitySelectors(selectors ...compdesc.IdentitySelector) ([]ResourceAccess, error) { - return nil, nil -} - -// Deprecated: use GetResources. -func (d *DummyComponentVersionAccess) GetResourcesByResourceSelectors(selectors ...compdesc.ResourceSelector) ([]ResourceAccess, error) { - return nil, nil -} - -// Deprecated: use GetReferences. -func (d *DummyComponentVersionAccess) GetReferencesByName(name string, selectors ...compdesc.IdentitySelector) (compdesc.References, error) { - return nil, nil -} - -// Deprecated: use GetReferences. -func (d *DummyComponentVersionAccess) GetReferencesByIdentitySelectors(selectors ...compdesc.IdentitySelector) (compdesc.References, error) { - return nil, nil -} - -// Deprecated: use GetReferences. -func (d *DummyComponentVersionAccess) GetReferencesByReferenceSelectors(selectors ...compdesc.ReferenceSelector) (compdesc.References, error) { - return nil, nil -} diff --git a/api/ocm/cpi/repocpi/view_cv.go b/api/ocm/cpi/repocpi/view_cv.go index 0320314fe7..c916c3b154 100644 --- a/api/ocm/cpi/repocpi/view_cv.go +++ b/api/ocm/cpi/repocpi/view_cv.go @@ -23,7 +23,6 @@ import ( common "ocm.software/ocm/api/utils/misc" "ocm.software/ocm/api/utils/refmgmt" "ocm.software/ocm/api/utils/refmgmt/resource" - "ocm.software/ocm/api/utils/selector" ) // View objects are the user facing generic implementations of the context interfaces. @@ -633,20 +632,6 @@ func (c *componentVersionAccessView) GetResourceByIndex(i int) (cpi.ResourceAcce return cpi.NewResourceAccess(c, r.Access, r.ResourceMeta), nil } -// Deprecated: use GetResources with appropriate selectors. -func (c *componentVersionAccessView) GetResourcesByName(name string, selectors ...compdesc.IdentitySelector) ([]cpi.ResourceAccess, error) { - resources, err := c.GetDescriptor().GetResourcesByName(name, selectors...) - if err != nil { - return nil, err - } - - result := []cpi.ResourceAccess{} - for _, resource := range resources { - result = append(result, cpi.NewResourceAccess(c, resource.Access, resource.ResourceMeta)) - } - return result, nil -} - func (c *componentVersionAccessView) SelectResources(sel ...rscsel.Selector) ([]cpi.ResourceAccess, error) { err := selectors.ValidateSelectors(sel...) if err != nil { @@ -677,56 +662,6 @@ func (c *componentVersionAccessView) GetResources() []cpi.ResourceAccess { return result } -// GetResourcesByIdentitySelectors returns resources that match the given identity selectors. -// -// Deprecated: use GetReferences. -func (c *componentVersionAccessView) GetResourcesByIdentitySelectors(selectors ...compdesc.IdentitySelector) ([]cpi.ResourceAccess, error) { - return c.GetResourcesBySelectors(selectors, nil) -} - -// GetResourcesByResourceSelectors returns resources that match the given resource selectors. -// -// Deprecated: use GetResources. -func (c *componentVersionAccessView) GetResourcesByResourceSelectors(selectors ...compdesc.ResourceSelector) ([]cpi.ResourceAccess, error) { - return c.GetResourcesBySelectors(nil, selectors) -} - -// GetResourcesBySelectors returns resources that match the given selector. -// -// Deprecated: use GetResources. -func (c *componentVersionAccessView) GetResourcesBySelectors(selectors []compdesc.IdentitySelector, resourceSelectors []compdesc.ResourceSelector) ([]cpi.ResourceAccess, error) { - resources := make([]cpi.ResourceAccess, 0) - rscs := c.GetDescriptor().Resources - for i := range rscs { - selctx := compdesc.NewResourceSelectionContext(i, rscs) - if len(selectors) > 0 { - ok, err := selector.MatchSelectors(selctx.Identity(), selectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - } - ok, err := compdesc.MatchResourceByResourceSelector(selctx, resourceSelectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - r, err := c.GetResourceByIndex(i) - if err != nil { - return nil, err - } - resources = append(resources, r) - } - if len(resources) == 0 { - return resources, compdesc.NotFound - } - return resources, nil -} - func (c *componentVersionAccessView) GetSource(id metav1.Identity) (cpi.SourceAccess, error) { r, err := c.GetDescriptor().GetSourceByIdentity(id) if err != nil { @@ -747,20 +682,6 @@ func (c *componentVersionAccessView) GetSourceByIndex(i int) (cpi.SourceAccess, return cpi.NewSourceAccess(c, r.Access, r.SourceMeta), nil } -// Deprecated: use GetSources with appropriate selectors. -func (c *componentVersionAccessView) GetSourcesByName(name string, selectors ...compdesc.IdentitySelector) ([]cpi.SourceAccess, error) { - sources, err := c.GetDescriptor().GetSourcesByName(name, selectors...) - if err != nil { - return nil, err - } - - result := []cpi.SourceAccess{} - for _, resource := range sources { - result = append(result, cpi.NewSourceAccess(c, resource.Access, resource.SourceMeta)) - } - return result, nil -} - func (c *componentVersionAccessView) SelectSources(sel ...srcsel.Selector) ([]cpi.SourceAccess, error) { err := selectors.ValidateSelectors(sel...) if err != nil { @@ -817,54 +738,3 @@ func (c *componentVersionAccessView) GetReferenceByIndex(i int) (cpi.ComponentRe } return c.GetDescriptor().References[i], nil } - -// Deprecated: use GetReferences. -func (c *componentVersionAccessView) GetReferencesByName(name string, selectors ...compdesc.IdentitySelector) (compdesc.References, error) { - return c.GetDescriptor().GetReferencesByName(name, selectors...) -} - -// GetReferencesByIdentitySelectors returns references that match the given identity selectors. -// -// Deprecated: use GetReferences. -func (c *componentVersionAccessView) GetReferencesByIdentitySelectors(selectors ...compdesc.IdentitySelector) (compdesc.References, error) { - return c.GetReferencesBySelectors(selectors, nil) -} - -// GetReferencesByReferenceSelectors returns references that match the given resource selectors. -// -// Deprecated: use GetReferences. -func (c *componentVersionAccessView) GetReferencesByReferenceSelectors(selectors ...compdesc.ReferenceSelector) (compdesc.References, error) { - return c.GetReferencesBySelectors(nil, selectors) -} - -// GetReferencesBySelectors returns references that match the given selector. -// -// Deprecated: use GetReferences. -func (c *componentVersionAccessView) GetReferencesBySelectors(selectors []compdesc.IdentitySelector, referenceSelectors []compdesc.ReferenceSelector) (compdesc.References, error) { - references := make(compdesc.References, 0) - refs := c.GetDescriptor().References - for i := range refs { - selctx := compdesc.NewReferenceSelectionContext(i, refs) - if len(selectors) > 0 { - ok, err := selector.MatchSelectors(selctx.Identity(), selectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - } - ok, err := compdesc.MatchReferencesByReferenceSelector(selctx, referenceSelectors...) - if err != nil { - return nil, fmt.Errorf("unable to match selector for resource %s: %w", selctx.Name, err) - } - if !ok { - continue - } - references = append(references, *selctx.Reference) - } - if len(references) == 0 { - return references, compdesc.NotFound - } - return references, nil -} diff --git a/api/ocm/extensions/repositories/comparch/comparch_test.go b/api/ocm/extensions/repositories/comparch/comparch_test.go index 5c64bd167a..2577f0f2dd 100644 --- a/api/ocm/extensions/repositories/comparch/comparch_test.go +++ b/api/ocm/extensions/repositories/comparch/comparch_test.go @@ -6,6 +6,7 @@ import ( . "github.com/mandelsoft/goutils/testutils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "ocm.software/ocm/api/ocm/selectors" . "ocm.software/ocm/api/ocm/testhelper" "github.com/mandelsoft/filepath/pkg/filepath" @@ -73,7 +74,7 @@ var _ = Describe("Repository", func() { defer Close(repo, "repo") cv := Must(repo.LookupComponentVersion(COMPONENT_NAME, COMPONENT_VERSION)) defer Close(cv, "compvers") - res := Must(cv.GetResourcesByName(RESOURCE_NAME)) + res := Must(cv.SelectResources(selectors.Name(RESOURCE_NAME))) acc := Must(res[0].AccessMethod()) defer Close(acc, "method") bytesA := Must(acc.Get()) @@ -89,7 +90,7 @@ var _ = Describe("Repository", func() { defer Close(repo) cv := Must(repo.LookupComponentVersion(COMPONENT_NAME, COMPONENT_VERSION)) defer Close(cv) - res := Must(cv.GetResourcesByName(RESOURCE_NAME)) + res := Must(cv.SelectResources(selectors.Name(RESOURCE_NAME))) acc := Must(res[0].AccessMethod()) defer Close(acc) data := Must(acc.Reader()) @@ -119,7 +120,7 @@ var _ = Describe("Repository", func() { MustBeSuccessful(arch.SetResourceBlob(compdesc.NewResourceMeta("blob", resourcetypes.PLAIN_TEXT, metav1.LocalRelation), blobaccess.ForString(mime.MIME_TEXT, S_TESTDATA), "", nil)) - res := Must(arch.GetResourcesByName("blob")) + res := Must(arch.SelectResources(selectors.Name("blob"))) Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{ HashAlgorithm: sha256.Algorithm, NormalisationAlgorithm: blob.GenericBlobDigestV1, @@ -135,7 +136,7 @@ var _ = Describe("Repository", func() { arch = Must(comparch.Open(octx, accessobj.ACC_WRITABLE, "test", 0o0700, accessio.PathFileSystem(memfs))) finalize.Close(arch, "comparch)") - res = Must(arch.GetResourcesByName("blob")) + res = Must(arch.SelectResources(selectors.Name("blob"))) Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{ HashAlgorithm: sha256.Algorithm, NormalisationAlgorithm: blob.GenericBlobDigestV1, @@ -159,7 +160,7 @@ var _ = Describe("Repository", func() { MustBeSuccessful(arch.SetResourceBlob(compdesc.NewResourceMeta("blob", resourcetypes.PLAIN_TEXT, metav1.LocalRelation), blobaccess.ForString(mime.MIME_TEXT, S_TESTDATA), "", nil)) - res := Must(arch.GetResourcesByName("blob")) + res := Must(arch.SelectResources(selectors.Name("blob"))) Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{ HashAlgorithm: sha256.Algorithm, NormalisationAlgorithm: blob.GenericBlobDigestV1, @@ -175,7 +176,7 @@ var _ = Describe("Repository", func() { arch = Must(comparch.Open(octx, accessobj.ACC_WRITABLE, "test", 0o0700, accessio.PathFileSystem(memfs))) finalize.Close(arch, "comparch)") - res = Must(arch.GetResourcesByName("blob")) + res = Must(arch.SelectResources(selectors.Name("blob"))) Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{ HashAlgorithm: sha256.Algorithm, NormalisationAlgorithm: blob.GenericBlobDigestV1, @@ -190,7 +191,7 @@ var _ = Describe("Repository", func() { defer Close(repo) cv := Must(repo.LookupComponentVersion(COMPONENT_NAME, COMPONENT_VERSION)) defer Close(cv) - res := Must(cv.GetResourcesByName(RESOURCE_NAME)) + res := Must(cv.SelectResources(selectors.Name("blob"))) acc := Must(res[0].AccessMethod()) defer Close(acc) }) @@ -245,7 +246,7 @@ var _ = Describe("Repository", func() { arch := Must(comparch.Open(octx, accessobj.ACC_READONLY, TAR_COMPARCH, 0o0700, accessio.PathFileSystem(env))) finalize.Close(arch, "comparch)") - res := Must(arch.GetResourcesByName("blob")) + res := Must(arch.SelectResources(selectors.Name("blob"))) Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{ HashAlgorithm: sha256.Algorithm, NormalisationAlgorithm: blob.GenericBlobDigestV1, @@ -279,7 +280,7 @@ var _ = Describe("Repository", func() { arch := Must(comparch.Open(octx, accessobj.ACC_READONLY, TAR_COMPARCH, 0o0700, accessio.PathFileSystem(env))) finalize.Close(arch, "comparch)") - res := Must(arch.GetResourcesByName("blob")) + res := Must(arch.SelectResources(selectors.Name("blob"))) Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{ HashAlgorithm: sha256.Algorithm, NormalisationAlgorithm: blob.GenericBlobDigestV1, diff --git a/api/ocm/extensions/repositories/composition/repository_test.go b/api/ocm/extensions/repositories/composition/repository_test.go index 4494947479..d34b7fd5ce 100644 --- a/api/ocm/extensions/repositories/composition/repository_test.go +++ b/api/ocm/extensions/repositories/composition/repository_test.go @@ -4,6 +4,7 @@ import ( . "github.com/mandelsoft/goutils/testutils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "ocm.software/ocm/api/ocm/selectors" "github.com/mandelsoft/goutils/finalizer" "github.com/mandelsoft/vfs/pkg/memoryfs" @@ -14,7 +15,7 @@ import ( metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes" me "ocm.software/ocm/api/ocm/extensions/repositories/composition" - ocmutils "ocm.software/ocm/api/ocm/ocmutils" + "ocm.software/ocm/api/ocm/ocmutils" "ocm.software/ocm/api/utils/accessio" "ocm.software/ocm/api/utils/accessobj" "ocm.software/ocm/api/utils/blobaccess/blobaccess" @@ -59,7 +60,7 @@ var _ = Describe("repository", func() { cv = Must(c.LookupVersion(VERSION)) finalize.Close(cv, "query") - rs := Must(cv.GetResourcesByName("test")) + rs := Must(cv.SelectResources(selectors.Name("test"))) Expect(len(rs)).To(Equal(1)) data := Must(ocmutils.GetResourceData(rs[0])) Expect(string(data)).To(Equal("testdata")) diff --git a/api/ocm/extensions/repositories/composition/version_test.go b/api/ocm/extensions/repositories/composition/version_test.go index 44b30fffd7..647e1b48e4 100644 --- a/api/ocm/extensions/repositories/composition/version_test.go +++ b/api/ocm/extensions/repositories/composition/version_test.go @@ -4,6 +4,7 @@ import ( . "github.com/mandelsoft/goutils/testutils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "ocm.software/ocm/api/ocm/selectors" "github.com/mandelsoft/goutils/finalizer" @@ -11,7 +12,7 @@ import ( metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes" me "ocm.software/ocm/api/ocm/extensions/repositories/composition" - ocmutils "ocm.software/ocm/api/ocm/ocmutils" + "ocm.software/ocm/api/ocm/ocmutils" "ocm.software/ocm/api/utils/blobaccess" "ocm.software/ocm/api/utils/blobaccess/bpi" "ocm.software/ocm/api/utils/mime" @@ -49,7 +50,7 @@ var _ = Describe("version", func() { cv = Must(c.LookupVersion(VERSION)) Expect(refmgmt.ReferenceCount(cv)).To(Equal(1)) nested.Close(cv, "query") - rs := Must(cv.GetResourcesByName("test")) + rs := Must(cv.SelectResources(selectors.Name("test"))) Expect(len(rs)).To(Equal(1)) data := Must(ocmutils.GetResourceData(rs[0])) Expect(string(data)).To(Equal("testdata")) @@ -65,7 +66,7 @@ var _ = Describe("version", func() { // check result cv = Must(repo2.LookupComponentVersion(COMPONENT, VERSION)) finalize.Close(cv, "query") - rs = Must(cv.GetResourcesByName("test")) + rs = Must(cv.SelectResources(selectors.Name("test"))) Expect(len(rs)).To(Equal(1)) data = Must(ocmutils.GetResourceData(rs[0])) Expect(string(data)).To(Equal("testdata")) diff --git a/api/ocm/internal/repository.go b/api/ocm/internal/repository.go index f79646c1bc..4aa4008afc 100644 --- a/api/ocm/internal/repository.go +++ b/api/ocm/internal/repository.go @@ -133,20 +133,6 @@ type ComponentVersionAccess interface { GetResources() []ResourceAccess SelectResources(sel ...rscsel.Selector) ([]ResourceAccess, error) - // - // Deprecated: use GetResources with selector arguments. - //nolint: staticcheck // deprecated - GetResourcesByName(name string, selectors ...compdesc.IdentitySelector) ([]ResourceAccess, error) - - // - // Deprecated: use GetResources with selector arguments. - //nolint: staticcheck // deprecated - GetResourcesByIdentitySelectors(selectors ...compdesc.IdentitySelector) ([]ResourceAccess, error) - - // - // Deprecated: use GetResources with selector arguments. - //nolint: staticcheck // deprecated - GetResourcesByResourceSelectors(selectors ...compdesc.ResourceSelector) ([]ResourceAccess, error) SetResource(*ResourceMeta, compdesc.AccessSpec, ...ModificationOption) error SetResourceByAccess(art ResourceAccess, modopts ...BlobModificationOption) error @@ -156,9 +142,6 @@ type ComponentVersionAccess interface { GetSources() []SourceAccess SelectSources(sel ...srcsel.Selector) ([]SourceAccess, error) - // Deprecated: use GetResources with appropriate selectors. - //nolint: staticcheck // deprecated - GetSourcesByName(name string, selectors ...compdesc.IdentitySelector) ([]SourceAccess, error) // SetSource updates or sets anew source. The options only use the // target options. All other options are ignored. SetSource(*SourceMeta, compdesc.AccessSpec, ...TargetOption) error @@ -172,17 +155,6 @@ type ComponentVersionAccess interface { GetReferences() []ComponentReference SelectReferences(sel ...refsel.Selector) ([]ComponentReference, error) - // Deprecated: use GetReferences with appropriate selectors. - //nolint: staticcheck // deprecated - GetReferencesByName(name string, selectors ...compdesc.IdentitySelector) (compdesc.References, error) - - // Deprecated: use GetReferences with appropriate selectors. - //nolint: staticcheck // deprecated - GetReferencesByIdentitySelectors(selectors ...compdesc.IdentitySelector) (compdesc.References, error) - - // Deprecated: use GetReferences with appropriate selectors. - //nolint: staticcheck // deprecated - GetReferencesByReferenceSelectors(selectors ...compdesc.ReferenceSelector) (compdesc.References, error) SetReference(ref *ComponentReference, opts ...TargetOption) error // AddBlob adds a local blob and returns an appropriate local access spec. diff --git a/api/utils/selector/selector.go b/api/ocm/selectors/json.go similarity index 93% rename from api/utils/selector/selector.go rename to api/ocm/selectors/json.go index 723ac33719..82a17b2057 100644 --- a/api/utils/selector/selector.go +++ b/api/ocm/selectors/json.go @@ -1,4 +1,4 @@ -package selector +package selectors import ( "encoding/json" @@ -7,6 +7,7 @@ import ( "github.com/ghodss/yaml" "github.com/xeipuuv/gojsonschema" + "ocm.software/ocm/api/ocm/selectors/accessors" ) // Interface defines a selector interface that @@ -223,3 +224,14 @@ func (j JSONSchemaSelector) Match(obj map[string]string) (bool, error) { } return res.Valid(), nil } + +// TODO 1: this might not work. +// TODO 2: what about accessor2? +func (j JSONSchemaSelector) MatchResource(accessor accessors.ElementListAccessor, accessor2 accessors.ResourceAccessor) bool { + documentLoader := gojsonschema.NewGoLoader(accessor) + res, err := j.Scheme.Validate(documentLoader) + if err != nil { + return false + } + return res.Valid() +} diff --git a/examples/lib/tour/03-working-with-credentials/02-basic-credential-management.go b/examples/lib/tour/03-working-with-credentials/02-basic-credential-management.go index 7d96b44974..adfda2d8dc 100644 --- a/examples/lib/tour/03-working-with-credentials/02-basic-credential-management.go +++ b/examples/lib/tour/03-working-with-credentials/02-basic-credential-management.go @@ -6,6 +6,7 @@ import ( "os" "github.com/mandelsoft/goutils/errors" + "ocm.software/ocm/api/ocm/selectors" "ocm.software/ocm/api/credentials" ociidentity "ocm.software/ocm/api/credentials/builtin/oci/identity" @@ -194,7 +195,7 @@ func UsingCredentialsB(cfg *helper.Config, create bool) error { // It is not longer a local blob. // --- begin examine cli --- - res, err := cv.GetResourcesByName("ocmcli") + res, err := cv.SelectResources(selectors.Name("ocmcli")) if err != nil { return errors.Wrapf(err, "accessing ocmcli resource") } diff --git a/examples/lib/tour/03-working-with-credentials/README.md b/examples/lib/tour/03-working-with-credentials/README.md index a7edf4f795..17cb2d5966 100644 --- a/examples/lib/tour/03-working-with-credentials/README.md +++ b/examples/lib/tour/03-working-with-credentials/README.md @@ -315,7 +315,7 @@ uploaded to the OCI registry as OCI artifact and the access method has be change to `ociArtifact`. It is not longer a local blob. ```go - res, err := cv.GetResourcesByName("ocmcli") + res, err := cv.SelectResources(selectors.Name("ocmcli")) if err != nil { return errors.Wrapf(err, "accessing ocmcli resource") }