Skip to content

Commit

Permalink
descriptive + flagsetscheme
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Oct 31, 2024
1 parent a10e0c5 commit 7c08f60
Show file tree
Hide file tree
Showing 19 changed files with 449 additions and 286 deletions.
6 changes: 3 additions & 3 deletions api/credentials/cpi/repotypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func RegisterRepositoryTypeVersions(s RepositoryTypeVersionScheme) {
////////////////////////////////////////////////////////////////////////////////

func NewRepositoryType[I RepositorySpec](name string, opts ...RepositoryOption) RepositoryType {
return descriptivetype.NewTypedObjectTypeObject(runtime.NewVersionedTypedObjectType[RepositorySpec, I](name), opts...)
return descriptivetype.NewVersionedTypedObjectTypeObject(runtime.NewVersionedTypedObjectType[RepositorySpec, I](name), opts...)
}

func NewRepositoryTypeByConverter[I RepositorySpec, V runtime.TypedObject](name string, converter runtime.Converter[I, V], opts ...RepositoryOption) RepositoryType {
return descriptivetype.NewTypedObjectTypeObject(runtime.NewVersionedTypedObjectTypeByConverter[RepositorySpec, I](name, converter), opts...)
return descriptivetype.NewVersionedTypedObjectTypeObject(runtime.NewVersionedTypedObjectTypeByConverter[RepositorySpec, I](name, converter), opts...)
}

func NewRepositoryTypeByFormatVersion(name string, fmt runtime.FormatVersion[RepositorySpec], opts ...RepositoryOption) RepositoryType {
return descriptivetype.NewTypedObjectTypeObject(runtime.NewVersionedTypedObjectTypeByFormatVersion[RepositorySpec](name, fmt), opts...)
return descriptivetype.NewVersionedTypedObjectTypeObject(runtime.NewVersionedTypedObjectTypeByFormatVersion[RepositorySpec](name, fmt), opts...)
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
10 changes: 5 additions & 5 deletions api/credentials/internal/repotypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type RepositoryType interface {
descriptivetype.TypedObjectType[RepositorySpec]
descriptivetype.VersionedTypedObjectType[RepositorySpec]
}

type RepositorySpec interface {
Expand All @@ -28,22 +28,22 @@ type (
)

type RepositoryTypeScheme interface {
descriptivetype.TypeScheme[RepositorySpec, RepositoryType]
descriptivetype.VersionedTypeScheme[RepositorySpec, RepositoryType]
}

type _Scheme = descriptivetype.TypeScheme[RepositorySpec, RepositoryType]
type _Scheme = descriptivetype.VersionedTypeScheme[RepositorySpec, RepositoryType]

type repositoryTypeScheme struct {
_Scheme
}

func NewRepositoryTypeScheme(defaultDecoder RepositorySpecDecoder, base ...RepositoryTypeScheme) RepositoryTypeScheme {
scheme := descriptivetype.MustNewDefaultTypeScheme[RepositorySpec, RepositoryType, RepositoryTypeScheme]("Credential provider", nil, &UnknownRepositorySpec{}, true, defaultDecoder, utils.Optional(base...))
scheme := descriptivetype.MustNewDefaultVersionedTypeScheme[RepositorySpec, RepositoryType, RepositoryTypeScheme]("Credential provider", nil, &UnknownRepositorySpec{}, true, defaultDecoder, utils.Optional(base...))
return &repositoryTypeScheme{scheme}
}

func NewStrictRepositoryTypeScheme(base ...RepositoryTypeScheme) runtime.VersionedTypeRegistry[RepositorySpec, RepositoryType] {
scheme := descriptivetype.MustNewDefaultTypeScheme[RepositorySpec, RepositoryType, RepositoryTypeScheme]("Credential provider", nil, nil, false, nil, utils.Optional(base...))
scheme := descriptivetype.MustNewDefaultVersionedTypeScheme[RepositorySpec, RepositoryType, RepositoryTypeScheme]("Credential provider", nil, nil, false, nil, utils.Optional(base...))
return &repositoryTypeScheme{scheme}
}

Expand Down
6 changes: 3 additions & 3 deletions api/ocm/cpi/accspeccpi/accesstypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func MustNewAccessSpecMultiFormatVersion(kind string, formats AccessSpecFormatVe
}

func NewAccessSpecType[I AccessSpec](name string, opts ...AccessSpecTypeOption) AccessType {
return flagsetscheme.NewTypedObjectTypeObject[AccessSpec](runtime.NewVersionedTypedObjectType[AccessSpec, I](name), opts...)
return flagsetscheme.NewVersionedTypedObjectTypeObject[AccessSpec](runtime.NewVersionedTypedObjectType[AccessSpec, I](name), opts...)
}

func NewAccessSpecTypeByConverter[I AccessSpec, V runtime.VersionedTypedObject](name string, converter runtime.Converter[I, V], opts ...AccessSpecTypeOption) AccessType {
return flagsetscheme.NewTypedObjectTypeObject[AccessSpec](runtime.NewVersionedTypedObjectTypeByConverter[AccessSpec, I, V](name, converter), opts...)
return flagsetscheme.NewVersionedTypedObjectTypeObject[AccessSpec](runtime.NewVersionedTypedObjectTypeByConverter[AccessSpec, I, V](name, converter), opts...)
}

func NewAccessSpecTypeByFormatVersion(name string, fmt runtime.FormatVersion[AccessSpec], opts ...AccessSpecTypeOption) AccessType {
return flagsetscheme.NewTypedObjectTypeObject[AccessSpec](runtime.NewVersionedTypedObjectTypeByFormatVersion[AccessSpec](name, fmt), opts...)
return flagsetscheme.NewVersionedTypedObjectTypeObject[AccessSpec](runtime.NewVersionedTypedObjectTypeByFormatVersion[AccessSpec](name, fmt), opts...)
}
8 changes: 4 additions & 4 deletions api/ocm/extensions/labels/routingslip/internal/entrytypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ type (

////////////////////////////////////////////////////////////////////////////////

type EntryTypeScheme = flagsetscheme.ExtendedTypeScheme[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider]
type EntryTypeScheme = flagsetscheme.ExtendedProviderTypeScheme[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider]

func unwrapTypeScheme(s EntryTypeScheme) flagsetscheme.TypeScheme[Entry, EntryType] {
func unwrapTypeScheme(s EntryTypeScheme) flagsetscheme.VersionedTypeScheme[Entry, EntryType] {
return s.Unwrap()
}

func NewEntryTypeScheme(base ...EntryTypeScheme) EntryTypeScheme {
return flagsetscheme.NewTypeSchemeWrapper[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider](flagsetscheme.NewTypeScheme[Entry, EntryType, flagsetscheme.TypeScheme[Entry, EntryType]]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", &UnknownEntry{}, true, sliceutils.Transform(base, unwrapTypeScheme)...))
return flagsetscheme.NewVersionedTypeSchemeWrapper[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider](flagsetscheme.NewVersionedTypeScheme[Entry, EntryType, flagsetscheme.VersionedTypeScheme[Entry, EntryType]]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", &UnknownEntry{}, true, sliceutils.Transform(base, unwrapTypeScheme)...))
}

func NewStrictEntryTypeScheme(base ...EntryTypeScheme) EntryTypeScheme {
return flagsetscheme.NewTypeSchemeWrapper[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider](flagsetscheme.NewTypeScheme[Entry, EntryType, flagsetscheme.TypeScheme[Entry, EntryType]]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", nil, false, sliceutils.Transform(base, unwrapTypeScheme)...))
return flagsetscheme.NewVersionedTypeSchemeWrapper[Entry, EntryType, flagsets.ExplicitlyTypedConfigTypeOptionSetConfigProvider](flagsetscheme.NewVersionedTypeScheme[Entry, EntryType, flagsetscheme.VersionedTypeScheme[Entry, EntryType]]("Entry type", "entry", "", "routing slip entry specification", "Entry Specification Options", nil, false, sliceutils.Transform(base, unwrapTypeScheme)...))
}

func CreateEntry(t runtime.TypedObject) (Entry, error) {
Expand Down
6 changes: 3 additions & 3 deletions api/ocm/extensions/labels/routingslip/spi/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func MustNewEntryMultiFormatVersion(kind string, formats EntryFormatVersionRegis
////////////////////////////////////////////////////////////////////////////////

func NewEntryType[I Entry](name string, opts ...EntryTypeOption) EntryType {
return flagsetscheme.NewTypedObjectTypeObject[Entry](runtime.NewVersionedTypedObjectType[Entry, I](name), opts...)
return flagsetscheme.NewVersionedTypedObjectTypeObject[Entry](runtime.NewVersionedTypedObjectType[Entry, I](name), opts...)
}

func NewEntryTypeByConverter[I Entry, V runtime.VersionedTypedObject](name string, converter runtime.Converter[I, V], opts ...EntryTypeOption) EntryType {
return flagsetscheme.NewTypedObjectTypeObject[Entry](runtime.NewVersionedTypedObjectTypeByConverter[Entry, I, V](name, converter), opts...)
return flagsetscheme.NewVersionedTypedObjectTypeObject[Entry](runtime.NewVersionedTypedObjectTypeByConverter[Entry, I, V](name, converter), opts...)
}

func NewEntryTypeByFormatVersion(name string, fmt runtime.FormatVersion[Entry], opts ...EntryTypeOption) EntryType {
return flagsetscheme.NewTypedObjectTypeObject[Entry](runtime.NewVersionedTypedObjectTypeByFormatVersion[Entry](name, fmt), opts...)
return flagsetscheme.NewVersionedTypedObjectTypeObject[Entry](runtime.NewVersionedTypedObjectTypeByFormatVersion[Entry](name, fmt), opts...)
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 5 additions & 9 deletions api/ocm/extensions/pubsub/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/general"
"github.com/mandelsoft/goutils/generics"
"github.com/mandelsoft/goutils/optionutils"
"github.com/modern-go/reflect2"

"ocm.software/ocm/api/ocm/cpi"
Expand All @@ -33,7 +32,7 @@ func WithDesciption(desc string) Option {

////////////////////////////////////////////////////////////////////////////////

type PubSubType descriptivetype.TypedObjectType[PubSubSpec]
type PubSubType descriptivetype.VersionedTypedObjectType[PubSubSpec]

// PubSubSpec is the interface publish/subscribe specifications
// must fulfill. The main task is to map the specification
Expand Down Expand Up @@ -61,14 +60,14 @@ type PubSubMethod interface {
// PubSub types. A PubSub type is finally able to
// provide an implementation for notifying a dedicated
// PubSub instance.
type TypeScheme descriptivetype.TypeScheme[PubSubSpec, PubSubType]
type TypeScheme descriptivetype.VersionedTypeScheme[PubSubSpec, PubSubType]

func NewTypeScheme(base ...TypeScheme) TypeScheme {
return descriptivetype.NewTypeScheme[PubSubSpec, PubSubType, TypeScheme]("PubSub type", nil, &UnknownPubSubSpec{}, false, base...)
return descriptivetype.NewVersionedTypeScheme[PubSubSpec, PubSubType, TypeScheme]("PubSub type", nil, &UnknownPubSubSpec{}, false, base...)
}

func NewStrictTypeScheme(base ...TypeScheme) runtime.VersionedTypeRegistry[PubSubSpec, PubSubType] {
return descriptivetype.NewTypeScheme[PubSubSpec, PubSubType, TypeScheme]("PubSub type", nil, &UnknownPubSubSpec{}, false, base...)
return descriptivetype.NewVersionedTypeScheme[PubSubSpec, PubSubType, TypeScheme]("PubSub type", nil, &UnknownPubSubSpec{}, false, base...)
}

// DefaultTypeScheme contains all globally known PubSub serializers.
Expand All @@ -83,10 +82,7 @@ func CreatePubSubSpec(t runtime.TypedObject) (PubSubSpec, error) {
}

func NewPubSubType[I PubSubSpec](name string, opts ...Option) PubSubType {
t := descriptivetype.NewTypedObjectTypeObject[PubSubSpec](runtime.NewVersionedTypedObjectType[PubSubSpec, I](name))
ta := descriptivetype.NewTypeObjectTarget[PubSubSpec](t)
optionutils.ApplyOptions[descriptivetype.OptionTarget](ta, opts...)
return t
return descriptivetype.NewVersionedTypedObjectTypeObject[PubSubSpec](runtime.NewVersionedTypedObjectType[PubSubSpec, I](name), opts...)
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions api/ocm/internal/accesstypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ type AccessMethod interface {
AsBlobAccess() BlobAccess
}

type AccessTypeScheme flagsetscheme.TypeScheme[AccessSpec, AccessType]
type AccessTypeScheme flagsetscheme.VersionedTypeScheme[AccessSpec, AccessType]

func NewAccessTypeScheme(base ...AccessTypeScheme) AccessTypeScheme {
return flagsetscheme.NewTypeScheme[AccessSpec, AccessType, AccessTypeScheme]("Access type", "access", "accessType", "blob access specification", "Access Specification Options", &UnknownAccessSpec{}, true, base...)
return flagsetscheme.NewVersionedTypeScheme[AccessSpec, AccessType, AccessTypeScheme]("Access type", "access", "accessType", "blob access specification", "Access Specification Options", &UnknownAccessSpec{}, true, base...)
}

func NewStrictAccessTypeScheme(base ...AccessTypeScheme) runtime.VersionedTypeRegistry[AccessSpec, AccessType] {
return flagsetscheme.NewTypeScheme[AccessSpec, AccessType, AccessTypeScheme]("Access type", "access", "accessType", "blob access specification", "Access Specification Options", nil, false, base...)
return flagsetscheme.NewVersionedTypeScheme[AccessSpec, AccessType, AccessTypeScheme]("Access type", "access", "accessType", "blob access specification", "Access Specification Options", nil, false, base...)
}

// DefaultAccessTypeScheme contains all globally known access serializer.
Expand Down
19 changes: 19 additions & 0 deletions api/utils/cobrautils/flagsets/flagsetscheme/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package flagsetscheme

import (
"ocm.software/ocm/api/utils/cobrautils/flagsets"
)

type TypeInfo interface {
ConfigOptionTypeSetHandler() flagsets.ConfigOptionTypeSetHandler
Description() string
Format() string
}

type typeInfoImpl struct {
handler flagsets.ConfigOptionTypeSetHandler
}

func (i *typeInfoImpl) ConfigOptionTypeSetHandler() flagsets.ConfigOptionTypeSetHandler {
return i.handler
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ import (
"ocm.software/ocm/api/utils/runtime/descriptivetype"
)

// OptionTargetWrapper is used as target for option functions, it provides
// setters for fields, which should not be modifiable for a final type object.
type OptionTargetWrapper[T any] struct {
target T
info *typeInfoImpl
}

func NewOptionTargetWrapper[T any](target T, info *typeInfoImpl) *OptionTargetWrapper[T] {
return &OptionTargetWrapper[T]{
target: target,
info: info,
}
}

func (t OptionTargetWrapper[E]) SetConfigHandler(value flagsets.ConfigOptionTypeSetHandler) {
t.info.handler = value
}

////////////////////////////////////////////////////////////////////////////////
// Access Type Options

type OptionTarget interface {
descriptivetype.OptionTarget
SetConfigHandler(flagsets.ConfigOptionTypeSetHandler)
}

Expand Down
62 changes: 39 additions & 23 deletions api/utils/cobrautils/flagsets/flagsetscheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,23 @@ import (
"ocm.software/ocm/api/utils/runtime/descriptivetype"
)

// VersionTypedObjectType is the appropriately extended type interface
// based on runtime.VersionTypedObjectType.
type VersionTypedObjectType[T runtime.VersionedTypedObject] interface {
descriptivetype.TypedObjectType[T]

ConfigOptionTypeSetHandler() flagsets.ConfigOptionTypeSetHandler
}

////////////////////////////////////////////////////////////////////////////////

// ExtendedTypeScheme is the appropriately extended scheme interface based on
// ExtendedProviderTypeScheme is the appropriately extended scheme interface based on
// runtime.TypeScheme supporting an extended config provider interface.
type ExtendedTypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider] interface {
type ExtendedProviderTypeScheme[T runtime.TypedObject, R TypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider] interface {
descriptivetype.TypeScheme[T, R]

CreateConfigTypeSetConfigProvider() P

Unwrap() TypeScheme[T, R]
}

type _TypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]] interface {
type _TypeScheme[T runtime.TypedObject, R TypedObjectType[T]] interface {
TypeScheme[T, R]
}

type typeSchemeWrapper[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider] struct {
type typeSchemeWrapper[T runtime.TypedObject, R TypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider] struct {
_TypeScheme[T, R]
}

Expand All @@ -44,41 +36,65 @@ func (s *typeSchemeWrapper[T, R, P]) Unwrap() TypeScheme[T, R] {
return s._TypeScheme
}

// NewVersionedTypeSchemeWrapper wraps a [VersionedTypeScheme] into a scheme returning a specialized config provider
// by casting the result. The type scheme constructor provides different implementations based on its
// arguments. This method here can be used to provide a type scheme returning the correct type.
func NewVersionedTypeSchemeWrapper[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider](s VersionedTypeScheme[T, R]) ExtendedProviderTypeScheme[T, R, P] {
return &typeSchemeWrapper[T, R, P]{s}
}

// NewTypeSchemeWrapper wraps a [TypeScheme] into a scheme returning a specialized config provider
// by casting the result. The type scheme constructor provides different implementations based on its
// arguments. This method here can be used to provide a type scheme returning the correct type.
func NewTypeSchemeWrapper[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider](s TypeScheme[T, R]) ExtendedTypeScheme[T, R, P] {
func NewTypeSchemeWrapper[T runtime.TypedObject, R TypedObjectType[T], P flagsets.ConfigTypeOptionSetConfigProvider](s TypeScheme[T, R]) ExtendedProviderTypeScheme[T, R, P] {
return &typeSchemeWrapper[T, R, P]{s}
}

// TypeScheme is the appropriately extended scheme interface based on
// VersionedTypeScheme is the appropriately extended scheme interface based on
// runtime.TypeScheme.
type TypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]] interface {
ExtendedTypeScheme[T, R, flagsets.ConfigTypeOptionSetConfigProvider]
type VersionedTypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]] interface {
ExtendedProviderTypeScheme[T, R, flagsets.ConfigTypeOptionSetConfigProvider]
}

type TypeScheme[T runtime.TypedObject, R TypedObjectType[T]] interface {
ExtendedProviderTypeScheme[T, R, flagsets.ConfigTypeOptionSetConfigProvider]
}

type _typeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]] interface {
type _typeScheme[T runtime.TypedObject, R TypedObjectType[T]] interface {
descriptivetype.TypeScheme[T, R]
}

type typeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], S TypeScheme[T, R]] struct {
type typeScheme[T runtime.TypedObject, R TypedObjectType[T], S TypeScheme[T, R]] struct {
cfgname string
description string
group string
typeOption string
_typeScheme[T, R]
}

func flagExtender[T runtime.VersionedTypedObject, R VersionTypedObjectType[T]](ty R) string {
func flagExtender[T runtime.TypedObject, R TypedObjectType[T]](ty R) string {
if h := ty.ConfigOptionTypeSetHandler(); h != nil {
return utils.IndentLines(flagsets.FormatConfigOptions(h), " ")
}
return ""
}

// NewTypeScheme provides an TypeScheme implementation based on the interfaces
// NewVersionedTypeScheme provides an VersionedTypeScheme implementation based on the interfaces
// and the default runtime.TypeScheme implementation.
func NewVersionedTypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], S TypeScheme[T, R]](kindname string, cfgname, typeOption, desc, group string, unknown runtime.Unstructured, acceptUnknown bool, base ...S) VersionedTypeScheme[T, R] {
scheme := descriptivetype.NewVersionedTypeScheme[T, R](kindname, flagExtender[T, R], unknown, acceptUnknown, utils.Optional(base...))
return &typeScheme[T, R, S]{
cfgname: cfgname,
description: desc,
group: group,
typeOption: typeOption,
_typeScheme: scheme,
}
}

// NewTypeScheme provides an VersionedTypeScheme implementation based on the interfaces
// and the default runtime.TypeScheme implementation.
func NewTypeScheme[T runtime.VersionedTypedObject, R VersionTypedObjectType[T], S TypeScheme[T, R]](kindname string, cfgname, typeOption, desc, group string, unknown runtime.Unstructured, acceptUnknown bool, base ...S) TypeScheme[T, R] {
func NewTypeScheme[T runtime.TypedObject, R TypedObjectType[T], S TypeScheme[T, R]](kindname string, cfgname, typeOption, desc, group string, unknown runtime.Unstructured, acceptUnknown bool, base ...S) TypeScheme[T, R] {
scheme := descriptivetype.NewTypeScheme[T, R](kindname, flagExtender[T, R], unknown, acceptUnknown, utils.Optional(base...))
return &typeScheme[T, R, S]{
cfgname: cfgname,
Expand Down Expand Up @@ -110,8 +126,8 @@ func (t *typeScheme[T, R, S]) CreateConfigTypeSetConfigProvider() flagsets.Confi
}
}
if t.BaseScheme() != nil {
base := t.BaseScheme()
for _, s := range base.(S).CreateConfigTypeSetConfigProvider().OptionTypeSets() {
base := t.BaseScheme().(S)
for _, s := range base.CreateConfigTypeSetConfigProvider().OptionTypeSets() {
if prov.GetTypeSet(s.GetName()) == nil {
err := prov.AddTypeSet(s)
if err != nil {
Expand Down
Loading

0 comments on commit 7c08f60

Please sign in to comment.