Debian Codegraph @

internal/testutil/datatest/registry.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Type registry for test data type resolution.
5 // Maps type names to Go types for dynamic test data instantiation.
6
7 package datatest
8
9 import (
10 "fmt"
11 "reflect"
12 )
13
14 // TypeRegistry maps type names (strings) to Go types.
15 // This allows test data in YAML to reference types by name.
16 type TypeRegistry struct {
17 types map[string]reflect.Type
18 factories map[string]func() any
19 }
20
21 // NewTypeRegistry creates a new type registry.
22 func NewTypeRegistry() *TypeRegistry {
23 return &TypeRegistry{
24 types: make(map[string]reflect.Type),
25 factories: make(map[string]func() any),
26 }
27 }
28
29 // Register registers a type by providing an exemplar value.
30 // The type name can be used in YAML test data to instantiate values of this type.
31 //
32 // Example:
33 //
34 // registry.Register("string", "")
35 // registry.Register("int", 0)
36 // registry.Register("yaml.Node", yaml.Node{})
37 func (r *TypeRegistry) Register(name string, exemplar any) {
38 r.types[name] = reflect.TypeOf(exemplar)
39 }
40
41 // RegisterFactory registers a factory function for creating instances of a type.
42 // This is useful for parameterized types like maps and slices.
43 //
44 // Example:
45 //
46 // registry.RegisterFactory("map[string]any", func() interface{} {
47 // return make(map[string]interface{})
48 // })
49 func (r *TypeRegistry) RegisterFactory(name string, factory func() any) {
50 r.factories[name] = factory
51 // Also register the type by calling the factory once
52 if instance := factory(); instance != nil {
53 r.types[name] = reflect.TypeOf(instance)
54 }
55 }
56
57 // NewInstance creates a new zero-value instance of the registered type.
58 // Returns an error if the type is not registered.
59 func (r *TypeRegistry) NewInstance(name string) (any, error) {
60 // Check if there's a factory first
61 if factory, ok := r.factories[name]; ok {
62 return factory(), nil
63 }
64
65 // Otherwise, create zero value from type
66 typ, ok := r.types[name]
67 if !ok {
68 return nil, fmt.Errorf("type %q not registered", name)
69 }
70
71 // Create a new instance
72 return reflect.New(typ).Elem().Interface(), nil
73 }
74
75 // NewPointerInstance creates a new pointer to a zero-value instance of the registered type.
76 // This is useful for unmarshaling into struct pointers.
77 func (r *TypeRegistry) NewPointerInstance(name string) (any, error) {
78 // Check if there's a factory first
79 if factory, ok := r.factories[name]; ok {
80 instance := factory()
81 // Return pointer to the instance
82 ptr := reflect.New(reflect.TypeOf(instance))
83 ptr.Elem().Set(reflect.ValueOf(instance))
84 return ptr.Interface(), nil
85 }
86
87 // Otherwise, create pointer from type
88 typ, ok := r.types[name]
89 if !ok {
90 return nil, fmt.Errorf("type %q not registered", name)
91 }
92
93 // Create a new pointer instance
94 return reflect.New(typ).Interface(), nil
95 }
96
97 // GetType returns the [reflect.Type] for a registered type name.
98 func (r *TypeRegistry) GetType(name string) (reflect.Type, bool) {
99 typ, ok := r.types[name]
100 return typ, ok
101 }
102
103 // Has checks if a type is registered.
104 func (r *TypeRegistry) Has(name string) bool {
105 _, ok := r.types[name]
106 if !ok {
107 _, ok = r.factories[name]
108 }
109 return ok
110 }
111
112 // ListTypes returns a list of all registered type names.
113 func (r *TypeRegistry) ListTypes() []string {
114 types := make([]string, 0, len(r.types)+len(r.factories))
115 seen := make(map[string]bool)
116
117 for name := range r.types {
118 types = append(types, name)
119 seen[name] = true
120 }
121
122 for name := range r.factories {
123 if !seen[name] {
124 types = append(types, name)
125 }
126 }
127
128 return types
129 }
130
131 // ValueRegistry maps string names to arbitrary constant values for test data.
132 // This allows YAML test files to reference constants by name (e.g., "+Inf", "NaN", "MaxInt32").
133 // Unlike ConstantRegistry which only handles ints, this handles any type.
134 type ValueRegistry struct {
135 values map[string]any
136 }
137
138 // NewValueRegistry creates a new empty ValueRegistry.
139 func NewValueRegistry() *ValueRegistry {
140 return &ValueRegistry{
141 values: make(map[string]any),
142 }
143 }
144
145 // Register registers a constant value with a name.
146 func (r *ValueRegistry) Register(name string, value any) {
147 r.values[name] = value
148 }
149
150 // Get retrieves a constant value by name.
151 // Returns (value, true) if found, (nil, false) if not found.
152 func (r *ValueRegistry) Get(name string) (any, bool) {
153 value, ok := r.values[name]
154 return value, ok
155 }
156
157 // Resolve recursively resolves constant names in a value.
158 // If the value is a string that matches a registered constant name, it's replaced.
159 // If the value is a map or slice, it recursively resolves all elements.
160 func (r *ValueRegistry) Resolve(value any) any {
161 // Check if it's a string constant name
162 if str, ok := value.(string); ok {
163 if constVal, found := r.Get(str); found {
164 return constVal
165 }
166 return value
167 }
168
169 // Recursively resolve maps
170 if m, ok := value.(map[string]any); ok {
171 result := make(map[string]any, len(m))
172 for k, v := range m {
173 result[k] = r.Resolve(v)
174 }
175 return result
176 }
177
178 if m, ok := value.(map[any]any); ok {
179 result := make(map[any]any, len(m))
180 for k, v := range m {
181 result[r.Resolve(k)] = r.Resolve(v)
182 }
183 return result
184 }
185
186 // Recursively resolve slices
187 if s, ok := value.([]any); ok {
188 result := make([]any, len(s))
189 for i, v := range s {
190 result[i] = r.Resolve(v)
191 }
192 return result
193 }
194
195 return value
196 }