-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.go
More file actions
511 lines (432 loc) · 13.4 KB
/
reflection.go
File metadata and controls
511 lines (432 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package goenum
import (
"fmt"
"reflect"
"strings"
)
// EnumMetadata contains reflection-based metadata about an enum type
type EnumMetadata struct {
// Type information
Type reflect.Type
// Field information
Fields []EnumField
// Tag information
Tags map[string]string
// Value type information
ValueType reflect.Type
// IsComposite indicates if this is a composite enum
IsComposite bool
}
// EnumField represents a field in an enum type
type EnumField struct {
Name string
Type reflect.Type
Value interface{}
Tags map[string]string
IsExported bool
}
// GetEnumMetadata returns reflection-based metadata about an enum type
func GetEnumMetadata[T Enum](enum T) (*EnumMetadata, error) {
if reflect.ValueOf(enum).IsNil() {
return nil, fmt.Errorf("cannot get metadata for nil enum")
}
// Get the type of the enum
t := reflect.TypeOf(enum)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
metadata := &EnumMetadata{
Type: t,
Fields: make([]EnumField, 0),
Tags: make(map[string]string),
ValueType: reflect.TypeOf(enum.Value()),
IsComposite: isCompositeEnum(enum),
}
// Extract fields
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldValue := reflect.ValueOf(enum).Field(i).Interface()
enumField := EnumField{
Name: field.Name,
Type: field.Type,
Value: fieldValue,
Tags: make(map[string]string),
IsExported: field.IsExported(),
}
// Extract tags
for _, tag := range []string{"json", "yaml", "xml", "enum"} {
if tagValue := field.Tag.Get(tag); tagValue != "" {
enumField.Tags[tag] = tagValue
}
}
metadata.Fields = append(metadata.Fields, enumField)
}
// Extract type-level tags
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
// Skip tag extraction for methods as they don't have tags
metadata.Tags[method.Name] = method.Name
}
return metadata, nil
}
// isCompositeEnum checks if an enum is a composite enum
func isCompositeEnum(enum Enum) bool {
_, ok := enum.(CompositeEnum)
return ok
}
// GetEnumValueType returns the type of an enum's value
func GetEnumValueType[T Enum](enum T) reflect.Type {
if reflect.ValueOf(enum).IsNil() {
return nil
}
return reflect.TypeOf(enum.Value())
}
// GetEnumFieldValue returns the value of a specific field in an enum
func GetEnumFieldValue[T Enum](enum T, fieldName string) (interface{}, error) {
if reflect.ValueOf(enum).IsNil() {
return nil, fmt.Errorf("cannot get field value from nil enum")
}
v := reflect.ValueOf(enum)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
field := v.FieldByName(fieldName)
if !field.IsValid() {
return nil, fmt.Errorf("field %s not found in enum", fieldName)
}
return field.Interface(), nil
}
// GetEnumTagValue returns the value of a specific tag on an enum field
func GetEnumTagValue[T Enum](enum T, fieldName, tagName string) (string, error) {
if reflect.ValueOf(enum).IsNil() {
return "", fmt.Errorf("cannot get tag value from nil enum")
}
t := reflect.TypeOf(enum)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
field, ok := t.FieldByName(fieldName)
if !ok {
return "", fmt.Errorf("field %s not found in enum", fieldName)
}
return field.Tag.Get(tagName), nil
}
// GetEnumFields returns all fields of an enum type
func GetEnumFields[T Enum](enum T) ([]EnumField, error) {
if reflect.ValueOf(enum).IsNil() {
return nil, fmt.Errorf("cannot get fields from nil enum")
}
t := reflect.TypeOf(enum)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
fields := make([]EnumField, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldValue := reflect.ValueOf(enum).Field(i).Interface()
enumField := EnumField{
Name: field.Name,
Type: field.Type,
Value: fieldValue,
Tags: make(map[string]string),
IsExported: field.IsExported(),
}
// Extract tags
for _, tag := range []string{"json", "yaml", "xml", "enum"} {
if tagValue := field.Tag.Get(tag); tagValue != "" {
enumField.Tags[tag] = tagValue
}
}
fields = append(fields, enumField)
}
return fields, nil
}
// GetEnumMethods returns all methods of an enum type
func GetEnumMethods[T Enum](enum T) ([]string, error) {
if reflect.ValueOf(enum).IsNil() {
return nil, fmt.Errorf("cannot get methods from nil enum")
}
t := reflect.TypeOf(enum)
methods := make([]string, 0, t.NumMethod())
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
methods = append(methods, method.Name)
}
return methods, nil
}
// IsEnumType checks if a type implements the Enum interface
func IsEnumType(t reflect.Type) bool {
return t.Implements(reflect.TypeOf((*Enum)(nil)).Elem())
}
// IsCompositeEnumType checks if a type implements the CompositeEnum interface
func IsCompositeEnumType(t reflect.Type) bool {
return t.Implements(reflect.TypeOf((*CompositeEnum)(nil)).Elem())
}
// GetEnumTypeInfo returns information about an enum type
func GetEnumTypeInfo[T Enum](enum T) (map[string]interface{}, error) {
if reflect.ValueOf(enum).IsNil() {
return nil, fmt.Errorf("cannot get type info from nil enum")
}
t := reflect.TypeOf(enum)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
info := map[string]interface{}{
"name": t.Name(),
"package": t.PkgPath(),
"kind": t.Kind().String(),
"is_enum": IsEnumType(t),
"is_composite": IsCompositeEnumType(t),
"num_fields": t.NumField(),
"num_methods": t.NumMethod(),
"value_type": GetEnumValueType(enum).String(),
"implements": make([]string, 0),
}
// Check implemented interfaces
if IsEnumType(t) {
info["implements"] = append(info["implements"].([]string), "Enum")
}
if IsCompositeEnumType(t) {
info["implements"] = append(info["implements"].([]string), "CompositeEnum")
}
return info, nil
}
// EnumReflection provides reflection-based utilities for working with enums
type EnumReflection struct {
// Type is the reflect.Type of the enum struct
Type reflect.Type
// EnumSet is the reflect.Value of the enum set
EnumSet reflect.Value
}
// NewEnumReflection creates a new EnumReflection instance for the given enum type
func NewEnumReflection[T Enum](enumSet *EnumSet[T]) *EnumReflection {
var zero T
return &EnumReflection{
Type: reflect.TypeOf(zero),
EnumSet: reflect.ValueOf(enumSet),
}
}
// GetEnumFields returns all enum fields from a struct type
func (r *EnumReflection) GetEnumFields() ([]reflect.StructField, error) {
if r.Type.Kind() != reflect.Struct {
return nil, fmt.Errorf("type %v is not a struct", r.Type)
}
var fields []reflect.StructField
for i := 0; i < r.Type.NumField(); i++ {
field := r.Type.Field(i)
if field.Type.Implements(reflect.TypeOf((*Enum)(nil)).Elem()) {
fields = append(fields, field)
}
}
return fields, nil
}
// GetEnumValues returns all enum values from a struct type
func (r *EnumReflection) GetEnumValues() ([]Enum, error) {
if r.Type.Kind() != reflect.Struct {
return nil, fmt.Errorf("type %v is not a struct", r.Type)
}
var values []Enum
for i := 0; i < r.Type.NumField(); i++ {
field := r.Type.Field(i)
if field.Type.Implements(reflect.TypeOf((*Enum)(nil)).Elem()) {
value := reflect.New(field.Type).Elem()
if enum, ok := value.Interface().(Enum); ok {
values = append(values, enum)
}
}
}
return values, nil
}
// GetEnumSet returns the enum set for a given enum type
func (r *EnumReflection) GetEnumSet() (*EnumSet[Enum], error) {
if !r.EnumSet.IsValid() {
return nil, fmt.Errorf("enum set is nil")
}
if r.EnumSet.IsNil() {
return nil, fmt.Errorf("enum set is nil")
}
// Get the underlying value
enumSetValue := r.EnumSet
if !enumSetValue.IsValid() {
return nil, fmt.Errorf("invalid enum set value")
}
// Create a new EnumSet
enumSet := NewEnumSet[Enum]()
// Get the Values() method
valuesMethod := enumSetValue.MethodByName("Values")
if !valuesMethod.IsValid() {
return nil, fmt.Errorf("invalid enum set structure: Values method not found")
}
// Call the Values() method to get all enums
valuesResult := valuesMethod.Call(nil)
if len(valuesResult) == 0 {
return nil, fmt.Errorf("invalid enum set structure: Values method returned no results")
}
// Get the slice of values
values := valuesResult[0]
if values.Kind() != reflect.Slice {
return nil, fmt.Errorf("invalid enum set structure: Values method did not return a slice")
}
// Register each enum in the new set
for i := 0; i < values.Len(); i++ {
value := values.Index(i)
if enum, ok := value.Interface().(Enum); ok {
enumSet.Register(enum)
}
}
return enumSet, nil
}
// GetEnumByName uses reflection to find an enum by name
func (r *EnumReflection) GetEnumByName(name string) (Enum, error) {
if !r.EnumSet.IsValid() {
return nil, fmt.Errorf("enum set is nil")
}
if r.EnumSet.IsNil() {
return nil, fmt.Errorf("enum set is nil")
}
// Get the underlying value
enumSetValue := r.EnumSet
if !enumSetValue.IsValid() {
return nil, fmt.Errorf("invalid enum set value")
}
// Get the GetByName method
getByNameMethod := enumSetValue.MethodByName("GetByName")
if !getByNameMethod.IsValid() {
return nil, fmt.Errorf("invalid enum set structure: GetByName method not found")
}
// Call GetByName with the name parameter
result := getByNameMethod.Call([]reflect.Value{reflect.ValueOf(name)})
if len(result) != 2 {
return nil, fmt.Errorf("invalid enum set structure: GetByName method returned unexpected results")
}
// Check if the enum was found
if !result[1].Bool() {
return nil, fmt.Errorf("enum with name %s not found", name)
}
// Convert the result to Enum
if enum, ok := result[0].Interface().(Enum); ok {
return enum, nil
}
return nil, fmt.Errorf("invalid enum type returned by GetByName")
}
// GetEnumByValue uses reflection to find an enum by value
func (r *EnumReflection) GetEnumByValue(value interface{}) (Enum, error) {
if !r.EnumSet.IsValid() {
return nil, fmt.Errorf("enum set is nil")
}
if r.EnumSet.IsNil() {
return nil, fmt.Errorf("enum set is nil")
}
// Get the underlying value
enumSetValue := r.EnumSet
if !enumSetValue.IsValid() {
return nil, fmt.Errorf("invalid enum set value")
}
// Get the GetByValue method
getByValueMethod := enumSetValue.MethodByName("GetByValue")
if !getByValueMethod.IsValid() {
return nil, fmt.Errorf("invalid enum set structure: GetByValue method not found")
}
// Call GetByValue with the value parameter
result := getByValueMethod.Call([]reflect.Value{reflect.ValueOf(value)})
if len(result) != 2 {
return nil, fmt.Errorf("invalid enum set structure: GetByValue method returned unexpected results")
}
// Check if the enum was found
if !result[1].Bool() {
return nil, fmt.Errorf("enum with value %v not found", value)
}
// Convert the result to Enum
if enum, ok := result[0].Interface().(Enum); ok {
return enum, nil
}
return nil, fmt.Errorf("invalid enum type returned by GetByValue")
}
// GetEnumTags returns all tags for a given enum field
func (r *EnumReflection) GetEnumTags(fieldName string) (map[string]string, error) {
field, ok := r.Type.FieldByName(fieldName)
if !ok {
return nil, fmt.Errorf("field %s not found", fieldName)
}
tags := make(map[string]string)
for _, tag := range strings.Split(string(field.Tag), " ") {
if tag == "" {
continue
}
parts := strings.Split(tag, ":")
if len(parts) == 2 {
tags[parts[0]] = strings.Trim(parts[1], "\"")
}
}
return tags, nil
}
// GetEnumMetadata returns metadata about an enum type
func (r *EnumReflection) GetEnumMetadata() (*EnumMetadata, error) {
fields, err := r.GetEnumFields()
if err != nil {
return nil, err
}
metadata := &EnumMetadata{
Type: r.Type,
Fields: make([]EnumField, len(fields)),
Tags: make(map[string]string),
ValueType: nil, // Will be set if we have any enum fields
IsComposite: false,
}
for i, field := range fields {
metadata.Fields[i] = EnumField{
Name: field.Name,
Type: field.Type,
Value: nil, // Will be set when we have an instance
Tags: make(map[string]string),
IsExported: field.IsExported(),
}
// Extract tags
for _, tag := range []string{"json", "yaml", "xml", "enum"} {
if tagValue := field.Tag.Get(tag); tagValue != "" {
metadata.Fields[i].Tags[tag] = tagValue
}
}
}
return metadata, nil
}
// GetEnumMethods returns all methods available for an enum type
func (r *EnumReflection) GetEnumMethods() []reflect.Method {
var methods []reflect.Method
for i := 0; i < r.Type.NumMethod(); i++ {
method := r.Type.Method(i)
methods = append(methods, method)
}
return methods
}
// GetEnumInterfaces returns all interfaces implemented by the enum type
func (r *EnumReflection) GetEnumInterfaces() []reflect.Type {
var interfaces []reflect.Type
for i := 0; i < r.Type.NumMethod(); i++ {
method := r.Type.Method(i)
if method.Type.NumIn() > 0 {
receiver := method.Type.In(0)
if receiver.Implements(reflect.TypeOf((*Enum)(nil)).Elem()) {
interfaces = append(interfaces, receiver)
}
}
}
return interfaces
}
// GetEnumConstants returns all constant values defined for the enum type
func (r *EnumReflection) GetEnumConstants() (map[string]interface{}, error) {
if r.Type.Kind() != reflect.Struct {
return nil, fmt.Errorf("type %v is not a struct", r.Type)
}
constants := make(map[string]interface{})
for i := 0; i < r.Type.NumField(); i++ {
field := r.Type.Field(i)
if field.Type.Implements(reflect.TypeOf((*Enum)(nil)).Elem()) {
value := reflect.New(field.Type).Elem()
if enum, ok := value.Interface().(Enum); ok {
constants[field.Name] = enum.Value()
}
}
}
return constants, nil
}