Skip to content

Commit 38743e0

Browse files
authored
fix CreateTemplateByType regression (#1899)
Signed-off-by: grokspawn <jordan@nimblewidget.com>
1 parent fa08e28 commit 38743e0

6 files changed

Lines changed: 79 additions & 19 deletions

File tree

alpha/template/api/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"context"
55
"io"
6+
"strings"
67

78
"github.com/operator-framework/operator-registry/alpha/declcfg"
89
)
@@ -25,6 +26,8 @@ type Template interface {
2526
Render(ctx context.Context, reader io.Reader) (*declcfg.DeclarativeConfig, error)
2627
// Schema returns the schema identifier for this template type
2728
Schema() string
29+
// Type returns the registration type for this template (typically the last segment of the schema)
30+
Type() string
2831
}
2932

3033
// Factory creates template instances based on schema
@@ -33,4 +36,12 @@ type Factory interface {
3336
CreateTemplate(renderBundle BundleRenderer) Template
3437
// Schema returns the schema identifier this factory handles
3538
Schema() string
39+
// Type returns the registration type for this factory (typically the last segment of the schema)
40+
Type() string
41+
}
42+
43+
// TypeFromSchema extracts the type name from a schema identifier
44+
// (e.g., "olm.semver" -> "semver")
45+
func TypeFromSchema(schema string) string {
46+
return schema[strings.LastIndex(schema, ".")+1:]
3647
}

alpha/template/basic/basic.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func (t *basicTemplate) Schema() string {
7373
return schema
7474
}
7575

76+
// Type returns the registration type for this template
77+
func (t *basicTemplate) Type() string {
78+
return api.TypeFromSchema(schema)
79+
}
80+
7681
// Helper functions
7782

7883
func parseSpec(reader io.Reader) (*BasicTemplateData, error) {
@@ -152,3 +157,8 @@ func (f *Factory) CreateTemplate(renderBundle api.BundleRenderer) api.Template {
152157
func (f *Factory) Schema() string {
153158
return schema
154159
}
160+
161+
// Type returns the registration type for this template
162+
func (f *Factory) Type() string {
163+
return api.TypeFromSchema(schema)
164+
}

alpha/template/registry.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ func (r *registry) CreateTemplateBySchema(reader io.Reader, renderBundle BundleR
8989

9090
func (r *registry) CreateTemplateByType(templateType string, renderBundle BundleRenderer) (Template, error) {
9191
r.mu.RLock()
92-
factory, exists := r.factories[templateType]
9392
defer r.mu.RUnlock()
94-
if !exists {
95-
return nil, &UnknownSchemaError{Schema: templateType}
96-
}
9793

98-
return factory.CreateTemplate(renderBundle), nil
94+
for _, f := range r.factories {
95+
if f.Type() == templateType {
96+
return f.CreateTemplate(renderBundle), nil
97+
}
98+
}
99+
return nil, &UnknownTypeError{Type: templateType}
99100
}
100101

101102
// GetSupportedSchemas returns all supported schema identifiers
@@ -117,8 +118,8 @@ func (r *registry) GetSupportedTypes() []string {
117118
r.mu.RLock()
118119
defer r.mu.RUnlock()
119120
types := make([]string, 0, len(r.factories))
120-
for schema := range r.factories {
121-
types = append(types, schema[strings.LastIndex(schema, ".")+1:])
121+
for _, f := range r.factories {
122+
types = append(types, f.Type())
122123
}
123124
slices.Sort(types)
124125
return types
@@ -144,3 +145,12 @@ type UnknownSchemaError struct {
144145
func (e *UnknownSchemaError) Error() string {
145146
return "unknown template schema: " + e.Schema
146147
}
148+
149+
// UnknownTypeError is returned when a template type is not recognized
150+
type UnknownTypeError struct {
151+
Type string
152+
}
153+
154+
func (e *UnknownTypeError) Error() string {
155+
return "unknown template type: " + e.Type
156+
}

alpha/template/registry_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stretchr/testify/require"
1010

1111
"github.com/operator-framework/operator-registry/alpha/declcfg"
12+
"github.com/operator-framework/operator-registry/alpha/template/api"
1213
)
1314

1415
// mockTemplate is a test implementation of the Template interface
@@ -32,6 +33,10 @@ func (m *mockTemplate) Schema() string {
3233
return m.schema
3334
}
3435

36+
func (m *mockTemplate) Type() string {
37+
return api.TypeFromSchema(m.schema)
38+
}
39+
3540
// mockFactory is a test implementation of the TemplateFactory interface
3641
type mockFactory struct {
3742
schema string
@@ -48,6 +53,10 @@ func (f *mockFactory) Schema() string {
4853
return f.schema
4954
}
5055

56+
func (f *mockFactory) Type() string {
57+
return api.TypeFromSchema(f.schema)
58+
}
59+
5160
// newEmptyTemplateRegistry creates an empty template registry for testing purposes.
5261
// Unlike newEmptyTemplateRegistry(), this does not register any built-in factories.
5362
func newEmptyTemplateRegistry() *registry {
@@ -118,28 +127,28 @@ func TestTemplateRegistry_CreateTemplateByType(t *testing.T) {
118127
{
119128
name: "create template for registered type",
120129
setupSchemas: []string{"olm.semver"},
121-
requestType: "olm.semver",
130+
requestType: "semver",
122131
expectError: false,
123132
},
124133
{
125134
name: "create template for multiple registered types",
126-
setupSchemas: []string{"olm.semver", "olm.basic", "olm.composite"},
127-
requestType: "olm.basic",
135+
setupSchemas: []string{"olm.semver", "olm.basic", "olm.subtitutes"},
136+
requestType: "basic",
128137
expectError: false,
129138
},
130139
{
131140
name: "error on unregistered type",
132141
setupSchemas: []string{"olm.semver"},
133-
requestType: "olm.unknown",
142+
requestType: "unknown",
134143
expectError: true,
135-
expectedErr: "unknown template schema: olm.unknown",
144+
expectedErr: "unknown template type: unknown",
136145
},
137146
{
138147
name: "error on empty registry",
139148
setupSchemas: []string{},
140-
requestType: "olm.semver",
149+
requestType: "semver",
141150
expectError: true,
142-
expectedErr: "unknown template schema: olm.semver",
151+
expectedErr: "unknown template type: semver",
143152
},
144153
}
145154

@@ -157,13 +166,13 @@ func TestTemplateRegistry_CreateTemplateByType(t *testing.T) {
157166
require.Nil(t, template)
158167
require.Contains(t, err.Error(), tt.expectedErr)
159168

160-
var unknownSchemaErr *UnknownSchemaError
161-
require.ErrorAs(t, err, &unknownSchemaErr)
162-
require.Equal(t, tt.requestType, unknownSchemaErr.Schema)
169+
var unknownTypeErr *UnknownTypeError
170+
require.ErrorAs(t, err, &unknownTypeErr)
171+
require.Equal(t, tt.requestType, unknownTypeErr.Type)
163172
} else {
164173
require.NoError(t, err)
165174
require.NotNil(t, template)
166-
require.Equal(t, tt.requestType, template.Schema())
175+
require.Equal(t, tt.requestType, template.Type())
167176
}
168177
})
169178
}
@@ -464,7 +473,7 @@ func TestTemplateRegistry_RenderBundlePropagation(t *testing.T) {
464473
var err error
465474

466475
if tt.method == "byType" {
467-
template, err = registry.CreateTemplateByType("olm.semver", mockRenderBundle)
476+
template, err = registry.CreateTemplateByType("semver", mockRenderBundle)
468477
require.NoError(t, err)
469478
} else {
470479
input := `schema: olm.semver`

alpha/template/semver/semver.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ func (t *semverTemplate) Schema() string {
123123
return schema
124124
}
125125

126+
// Type returns the registration type for this template
127+
func (t *semverTemplate) Type() string {
128+
return api.TypeFromSchema(schema)
129+
}
130+
126131
// Helper functions
127132

128133
// channel "archetypes", restricted in this iteration to just these
@@ -621,3 +626,8 @@ func (f *Factory) CreateTemplate(renderBundle api.BundleRenderer) api.Template {
621626
func (f *Factory) Schema() string {
622627
return schema
623628
}
629+
630+
// Type returns the registration type for this template
631+
func (f *Factory) Type() string {
632+
return api.TypeFromSchema(schema)
633+
}

alpha/template/substitutes/substitutes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func (t *template) Schema() string {
5252
return schema
5353
}
5454

55+
// Type returns the registration type for this template
56+
func (t *template) Type() string {
57+
return api.TypeFromSchema(schema)
58+
}
59+
5560
func (t *template) RenderBundle(ctx context.Context, bundleRef string) (*declcfg.DeclarativeConfig, error) {
5661
return t.renderBundle(ctx, bundleRef)
5762
}
@@ -285,3 +290,8 @@ func (f *Factory) CreateTemplate(renderBundle api.BundleRenderer) api.Template {
285290
func (f *Factory) Schema() string {
286291
return schema
287292
}
293+
294+
// Type returns the registration type for this template
295+
func (f *Factory) Type() string {
296+
return api.TypeFromSchema(schema)
297+
}

0 commit comments

Comments
 (0)