Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mega fix for new selector handling + accessor interface alignment + tests #918

Merged
merged 4 commits into from
Sep 11, 2024
Merged
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
20 changes: 8 additions & 12 deletions api/ocm/compdesc/accessors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package compdesc

import (
"ocm.software/ocm/api/ocm/compdesc/equivalent"
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/selectors/accessors"
)
Expand Down Expand Up @@ -38,19 +37,16 @@ type ObjectMetaAccessor interface {
}

// ElementMetaAccessor provides generic access an elements meta information.
type ElementMetaAccessor interface {
ElementMetaProvider
Equivalent(ElementMetaAccessor) equivalent.EqualState
}
// Deprecated: use Element.
type ElementMetaAccessor = accessors.Element

// ElementAccessor provides generic access to list of elements.
type ElementAccessor interface {
Len() int
Get(i int) ElementMetaAccessor
}
type Element = accessors.Element

// ElementListAccessor provides generic access to list of elements.
type ElementListAccessor = accessors.ElementListAccessor

type ElementMetaProvider interface {
GetMeta() *ElementMeta
GetMeta() accessors.ElementMeta
}

// ElementArtifactAccessor provides access to generic artifact information of an element.
Expand All @@ -69,7 +65,7 @@ type ElementDigestAccessor interface {
// ArtifactAccessor provides generic access to list of artifacts.
// There are resources or sources.
type ArtifactAccessor interface {
ElementAccessor
ElementListAccessor
GetArtifact(i int) ElementArtifactAccessor
}

Expand Down
46 changes: 11 additions & 35 deletions api/ocm/compdesc/componentdescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const InternalSchemaVersion = "internal"

// Deprecated: as result of the new select function an empty list is returned instead of an error.
var NotFound = errors.ErrNotFound()

const (
Expand Down Expand Up @@ -127,7 +128,7 @@ const (
type ElementMetaAccess interface {
GetName() string
GetVersion() string
GetIdentity(accessor ElementAccessor) metav1.Identity
GetIdentity(accessor ElementListAccessor) metav1.Identity
GetLabels() metav1.Labels
}

Expand Down Expand Up @@ -166,7 +167,7 @@ func (o *ElementMeta) GetName() string {
}

// GetMeta returns the element meta.
func (r *ElementMeta) GetMeta() *ElementMeta {
func (r *ElementMeta) GetMeta() accessors.ElementMeta {
return r
}

Expand Down Expand Up @@ -231,7 +232,7 @@ func (o *ElementMeta) AddExtraIdentity(identity metav1.Identity) {
}

// GetIdentity returns the identity of the object.
func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity {
func (o *ElementMeta) GetIdentity(accessor ElementListAccessor) metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
Expand All @@ -242,7 +243,7 @@ func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity {
l := accessor.Len()
for i := 0; i < l; i++ {
m := accessor.Get(i).GetMeta()
if m.Name == o.Name && m.ExtraIdentity.Equals(o.ExtraIdentity) {
if m.GetName() == o.Name && m.GetExtraIdentity().Equals(o.ExtraIdentity) {
if found {
identity[SystemIdentityVersion] = o.Version

Expand All @@ -255,32 +256,7 @@ func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity {
return identity
}

// GetIdentityForContext returns the identity of the object.
func (o *ElementMeta) GetIdentityForContext(accessor accessors.ElementListAccessor) metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
}
identity[SystemIdentityName] = o.Name
if accessor != nil {
found := false
l := accessor.Len()
for i := 0; i < l; i++ {
m := accessor.Get(i).GetMeta()
if m.GetName() == o.GetName() && m.GetExtraIdentity().Equals(o.ExtraIdentity) {
if found {
identity[SystemIdentityVersion] = o.Version

break
}
found = true
}
}
}
return identity
}

// GetRawIdentity returns the identity plus version.
// GetRawIdentity returns the identity plus version, if set.
func (o *ElementMeta) GetRawIdentity() metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
Expand All @@ -306,7 +282,7 @@ func (o *ElementMeta) GetMatchBaseIdentity() metav1.Identity {
}

// GetIdentityDigest returns the digest of the object's identity.
func (o *ElementMeta) GetIdentityDigest(accessor ElementAccessor) []byte {
func (o *ElementMeta) GetIdentityDigest(accessor ElementListAccessor) []byte {
return o.GetIdentity(accessor).Digest()
}

Expand Down Expand Up @@ -337,7 +313,7 @@ func (o *ElementMeta) Equivalent(a *ElementMeta) equivalent.EqualState {
return state.Apply(o.Labels.Equivalent(a.Labels))
}

func GetByIdentity(a ElementAccessor, id metav1.Identity) ElementMetaAccessor {
func GetByIdentity(a ElementListAccessor, id metav1.Identity) ElementMetaAccessor {
l := a.Len()
for i := 0; i < l; i++ {
e := a.Get(i)
Expand All @@ -348,7 +324,7 @@ func GetByIdentity(a ElementAccessor, id metav1.Identity) ElementMetaAccessor {
return nil
}

func GetIndexByIdentity(a ElementAccessor, id metav1.Identity) int {
func GetIndexByIdentity(a ElementListAccessor, id metav1.Identity) int {
l := a.Len()
for i := 0; i < l; i++ {
e := a.Get(i)
Expand All @@ -375,7 +351,7 @@ func GenericAccessSpec(un *runtime.UnstructuredTypedObject) AccessSpec {
// Sources describes a set of source specifications.
type Sources []Source

var _ ElementAccessor = Sources{}
var _ ElementListAccessor = Sources{}

func SourceArtifacts(cd *ComponentDescriptor) ArtifactAccessor {
return cd.Sources
Expand Down Expand Up @@ -538,7 +514,7 @@ func (r SourceRefs) Copy() SourceRefs {
// Resources describes a set of resource specifications.
type Resources []Resource

var _ ElementAccessor = Resources{}
var _ ElementListAccessor = Resources{}

func ResourceArtifacts(cd *ComponentDescriptor) ArtifactAccessor {
return cd.Resources
Expand Down
2 changes: 1 addition & 1 deletion api/ocm/compdesc/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (cd *ComponentDescriptor) GetComponentReferences(selectors ...IdentitySelec
// If the index is not found -1 is returned.
// Deprecated: use GetReferenceIndex.
func (cd *ComponentDescriptor) GetComponentReferenceIndex(ref Reference) int {
return cd.GetReferenceIndex(ref.GetMeta())
return cd.GetReferenceIndex(&ref)
}

// GetReferenceAccessByIdentity returns a pointer to the reference that matches the given identity.
Expand Down
2 changes: 1 addition & 1 deletion api/ocm/compdesc/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (cd *ComponentDescriptor) Equivalent(o *ComponentDescriptor) equivalent.Equ
)
}

func EquivalentElems(a ElementAccessor, b ElementAccessor) equivalent.EqualState {
func EquivalentElems(a ElementListAccessor, b ElementListAccessor) equivalent.EqualState {
state := equivalent.StateEquivalent()

// Equivaluent of elements handles nil to provide state accoding to it
Expand Down
46 changes: 19 additions & 27 deletions api/ocm/compdesc/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/mandelsoft/goutils/errors"
"golang.org/x/exp/slices"

v1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/selectors"
Expand Down Expand Up @@ -40,20 +41,21 @@ func (cd *ComponentDescriptor) AddRepositoryContext(repoCtx runtime.TypedObject)
return nil
}

func (cd *ComponentDescriptor) SelectResources(sel ...rscsel.Selector) ([]Resource, error) {
func (cd *ComponentDescriptor) SelectResources(sel ...rscsel.Selector) (Resources, error) {
err := selectors.ValidateSelectors(sel...)
if err != nil {
return nil, err
}

list := MapToSelectorElementList(cd.Resources)
result := []Resource{}
result := Resources{}
outer:
for _, r := range cd.Resources {
if len(sel) > 0 {
mr := MapToSelectorResource(&r)
for _, s := range sel {
if !s.MatchResource(list, mr) {
continue
continue outer
}
}
}
Expand All @@ -62,12 +64,8 @@ func (cd *ComponentDescriptor) SelectResources(sel ...rscsel.Selector) ([]Resour
return result, nil
}

func (cd *ComponentDescriptor) GetResources() []Resource {
result := []Resource{}
for _, r := range cd.Resources {
result = append(result, r)
}
return result
func (cd *ComponentDescriptor) GetResources() Resources {
return slices.Clone(cd.Resources)
}

// GetResourceByIdentity returns resource that matches the given identity.
Expand Down Expand Up @@ -116,20 +114,21 @@ func (cd *ComponentDescriptor) GetResourceIndex(res *ResourceMeta) int {
return ElementIndex(cd.Resources, res)
}

func (cd *ComponentDescriptor) SelectSources(sel ...srcsel.Selector) ([]Source, error) {
func (cd *ComponentDescriptor) SelectSources(sel ...srcsel.Selector) (Sources, error) {
err := selectors.ValidateSelectors(sel...)
if err != nil {
return nil, err
}

list := MapToSelectorElementList(cd.Sources)
result := []Source{}
result := Sources{}
outer:
for _, r := range cd.Sources {
if len(sel) > 0 {
mr := MapToSelectorSource(&r)
for _, s := range sel {
if !s.MatchSource(list, mr) {
continue
continue outer
}
}
}
Expand All @@ -138,12 +137,8 @@ func (cd *ComponentDescriptor) SelectSources(sel ...srcsel.Selector) ([]Source,
return result, nil
}

func (cd *ComponentDescriptor) GetSources() []Source {
result := []Source{}
for _, r := range cd.Sources {
result = append(result, r)
}
return result
func (cd *ComponentDescriptor) GetSources() Sources {
return slices.Clone(cd.Sources)
}

// GetSourceByIdentity returns source that match the given identity.
Expand Down Expand Up @@ -185,20 +180,21 @@ func (cd *ComponentDescriptor) GetReferenceByIdentity(id v1.Identity) (Reference
return Reference{}, errors.ErrNotFound(KIND_REFERENCE, id.String())
}

func (cd *ComponentDescriptor) SelectReferences(sel ...refsel.Selector) ([]Reference, error) {
func (cd *ComponentDescriptor) SelectReferences(sel ...refsel.Selector) (References, error) {
err := selectors.ValidateSelectors(sel...)
if err != nil {
return nil, err
}

list := MapToSelectorElementList(cd.References)
result := []Reference{}
result := References{}
outer:
for _, r := range cd.References {
if len(sel) > 0 {
mr := MapToSelectorReference(&r)
for _, s := range sel {
if !s.MatchReference(list, mr) {
continue
continue outer
}
}
}
Expand All @@ -207,12 +203,8 @@ func (cd *ComponentDescriptor) SelectReferences(sel ...refsel.Selector) ([]Refer
return result, nil
}

func (cd *ComponentDescriptor) GetReferences() []Reference {
result := []Reference{}
for _, r := range cd.References {
result = append(result, r)
}
return result
func (cd *ComponentDescriptor) GetReferences() References {
return slices.Clone(cd.References)
}

// GetReferenceIndexByIdentity returns the index of the reference that matches the given identity.
Expand Down
9 changes: 5 additions & 4 deletions api/ocm/compdesc/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
)

type elemList struct {
ElementAccessor
ElementListAccessor
}

func (e *elemList) Get(i int) accessors.ElementMetaAccessor {
return generics.Cast[accessors.ElementMetaAccessor](e.ElementAccessor.Get(i))
func (e *elemList) Get(i int) accessors.Element {
elem := e.ElementListAccessor.Get(i)
return generics.Cast[accessors.Element](elem)
}

func MapToSelectorElementList(accessor ElementAccessor) accessors.ElementListAccessor {
func MapToSelectorElementList(accessor ElementListAccessor) accessors.ElementListAccessor {
return &elemList{accessor}
}

Expand Down
Loading