| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package datatest |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
type TypeRegistry struct { |
| 17 |
types map[string]reflect.Type |
| 18 |
factories map[string]func() any |
| 19 |
} |
| 20 |
|
| 21 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func (r *TypeRegistry) Register(name string, exemplar any) { |
| 38 |
r.types[name] = reflect.TypeOf(exemplar) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
func (r *TypeRegistry) RegisterFactory(name string, factory func() any) { |
| 50 |
r.factories[name] = factory |
| 51 |
|
| 52 |
if instance := factory(); instance != nil { |
| 53 |
r.types[name] = reflect.TypeOf(instance) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
func (r *TypeRegistry) NewInstance(name string) (any, error) { |
| 60 |
|
| 61 |
if factory, ok := r.factories[name]; ok { |
| 62 |
return factory(), nil |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
typ, ok := r.types[name] |
| 67 |
if !ok { |
| 68 |
return nil, fmt.Errorf("type %q not registered", name) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
return reflect.New(typ).Elem().Interface(), nil |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (r *TypeRegistry) NewPointerInstance(name string) (any, error) { |
| 78 |
|
| 79 |
if factory, ok := r.factories[name]; ok { |
| 80 |
instance := factory() |
| 81 |
|
| 82 |
ptr := reflect.New(reflect.TypeOf(instance)) |
| 83 |
ptr.Elem().Set(reflect.ValueOf(instance)) |
| 84 |
return ptr.Interface(), nil |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
typ, ok := r.types[name] |
| 89 |
if !ok { |
| 90 |
return nil, fmt.Errorf("type %q not registered", name) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
return reflect.New(typ).Interface(), nil |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func (r *TypeRegistry) GetType(name string) (reflect.Type, bool) { |
| 99 |
typ, ok := r.types[name] |
| 100 |
return typ, ok |
| 101 |
} |
| 102 |
|
| 103 |
|
| 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 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
type ValueRegistry struct { |
| 135 |
values map[string]any |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func NewValueRegistry() *ValueRegistry { |
| 140 |
return &ValueRegistry{ |
| 141 |
values: make(map[string]any), |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
func (r *ValueRegistry) Register(name string, value any) { |
| 147 |
r.values[name] = value |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func (r *ValueRegistry) Get(name string) (any, bool) { |
| 153 |
value, ok := r.values[name] |
| 154 |
return value, ok |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
func (r *ValueRegistry) Resolve(value any) any { |
| 161 |
|
| 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 |
|
| 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 |
|
| 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 |
} |