-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic.go
More file actions
287 lines (249 loc) · 7.81 KB
/
dynamic.go
File metadata and controls
287 lines (249 loc) · 7.81 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
package goenum
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
)
// DuplicateHandling defines how to handle duplicate enums during loading
type DuplicateHandling int
const (
// DuplicateError will return an error when duplicates are found
DuplicateError DuplicateHandling = iota
// DuplicateSkip will skip duplicate entries
DuplicateSkip
// DuplicateOverride will override existing entries with new ones
DuplicateOverride
)
// ValidationOptions defines options for enum validation
type ValidationOptions struct {
// DuplicateHandling specifies how to handle duplicate enums
DuplicateHandling DuplicateHandling
// ValueType specifies the expected type for enum values (e.g., reflect.TypeOf(0) for int)
ValueType reflect.Type
// AllowEmptyNames allows enums with empty names
AllowEmptyNames bool
// AllowEmptyValues allows enums with nil values
AllowEmptyValues bool
}
// DefaultValidationOptions returns the default validation options
func DefaultValidationOptions() *ValidationOptions {
return &ValidationOptions{
DuplicateHandling: DuplicateError,
ValueType: nil, // No type restriction by default
AllowEmptyNames: false,
AllowEmptyValues: false,
}
}
// EnumDefinition represents the structure for loading enum data
type EnumDefinition struct {
Name string `json:"name"`
Value interface{} `json:"value"`
Description string `json:"description"`
Aliases []string `json:"aliases,omitempty"`
}
// DynamicEnumLoader provides functionality to load enums from various sources
type DynamicEnumLoader struct {
enumSet *EnumSet[Enum]
options *ValidationOptions
}
// NewDynamicEnumLoader creates a new DynamicEnumLoader instance
func NewDynamicEnumLoader(options *ValidationOptions) *DynamicEnumLoader {
if options == nil {
options = DefaultValidationOptions()
}
return &DynamicEnumLoader{
enumSet: NewEnumSet[Enum](),
options: options,
}
}
// validateEnumDefinition validates an enum definition according to the options
func (l *DynamicEnumLoader) validateEnumDefinition(def EnumDefinition) error {
// Check for empty name
if !l.options.AllowEmptyNames && def.Name == "" {
return fmt.Errorf("enum name cannot be empty")
}
// Check for empty value
if !l.options.AllowEmptyValues && def.Value == nil {
return fmt.Errorf("enum value cannot be nil")
}
// Check value type if specified
if l.options.ValueType != nil && def.Value != nil {
valueType := reflect.TypeOf(def.Value)
if !valueType.AssignableTo(l.options.ValueType) {
return fmt.Errorf("enum value type %v is not assignable to expected type %v",
valueType, l.options.ValueType)
}
}
return nil
}
// handleDuplicate handles duplicate enum according to the options
func (l *DynamicEnumLoader) handleDuplicate(name string, value interface{}) error {
switch l.options.DuplicateHandling {
case DuplicateError:
return fmt.Errorf("duplicate enum found: name=%s, value=%v", name, value)
case DuplicateSkip:
return nil // Skip this enum
case DuplicateOverride:
// Remove existing enum before adding new one
if _, exists := l.enumSet.GetByName(name); exists {
// Create a new set and copy all enums except the one to override
newSet := NewEnumSet[Enum]()
for _, enum := range l.enumSet.Values() {
if enum.String() != name {
newSet.Register(enum)
}
}
l.enumSet = newSet
}
}
return nil
}
// LoadFromJSON loads enum definitions from a JSON file
func (l *DynamicEnumLoader) LoadFromJSON(filename string) error {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
return l.LoadFromReader(file)
}
// LoadFromReader loads enum definitions from an io.Reader
func (l *DynamicEnumLoader) LoadFromReader(reader io.Reader) error {
var definitions []EnumDefinition
if err := json.NewDecoder(reader).Decode(&definitions); err != nil {
return fmt.Errorf("failed to decode JSON: %w", err)
}
for _, def := range definitions {
// Validate the enum definition
if err := l.validateEnumDefinition(def); err != nil {
return fmt.Errorf("invalid enum definition: %w", err)
}
// Handle duplicates
if err := l.handleDuplicate(def.Name, def.Value); err != nil {
if l.options.DuplicateHandling == DuplicateError {
return err
}
continue // Skip this enum for DuplicateSkip
}
// Convert float64 to int if necessary
if f, ok := def.Value.(float64); ok {
def.Value = int(f)
}
enum := &EnumBase{
name: def.Name,
value: def.Value,
description: def.Description,
aliases: def.Aliases,
jsonConfig: DefaultJSONConfig(),
}
l.enumSet.Register(enum)
}
return nil
}
// LoadFromDirectory loads all JSON files from a directory
func (l *DynamicEnumLoader) LoadFromDirectory(dir string) error {
// Check if directory exists
if _, err := os.Stat(dir); os.IsNotExist(err) {
return fmt.Errorf("directory does not exist: %s", dir)
}
files, err := filepath.Glob(filepath.Join(dir, "*.json"))
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
if len(files) == 0 {
return fmt.Errorf("no JSON files found in directory: %s", dir)
}
for _, file := range files {
if err := l.LoadFromJSON(file); err != nil {
return fmt.Errorf("failed to load file %s: %w", file, err)
}
}
return nil
}
// GetEnumSet returns the loaded enum set
func (l *DynamicEnumLoader) GetEnumSet() *EnumSet[Enum] {
return l.enumSet
}
// LoadFromMap loads enum definitions from a map
func (l *DynamicEnumLoader) LoadFromMap(definitions map[string]EnumDefinition) error {
for _, def := range definitions {
// Validate the enum definition
if err := l.validateEnumDefinition(def); err != nil {
return fmt.Errorf("invalid enum definition: %w", err)
}
// Handle duplicates
if err := l.handleDuplicate(def.Name, def.Value); err != nil {
if l.options.DuplicateHandling == DuplicateError {
return err
}
continue // Skip this enum for DuplicateSkip
}
enum := &EnumBase{
name: def.Name,
value: def.Value,
description: def.Description,
aliases: def.Aliases,
jsonConfig: DefaultJSONConfig(),
}
l.enumSet.Register(enum)
}
return nil
}
// LoadFromSlice loads enum definitions from a slice
func (l *DynamicEnumLoader) LoadFromSlice(definitions []EnumDefinition) error {
for _, def := range definitions {
// Validate the enum definition
if err := l.validateEnumDefinition(def); err != nil {
return fmt.Errorf("invalid enum definition: %w", err)
}
// Handle duplicates
if err := l.handleDuplicate(def.Name, def.Value); err != nil {
if l.options.DuplicateHandling == DuplicateError {
return err
}
continue // Skip this enum for DuplicateSkip
}
// Create a new enum set if we need to override
if l.options.DuplicateHandling == DuplicateOverride {
newSet := NewEnumSet[Enum]()
for _, enum := range l.enumSet.Values() {
if enum.String() != def.Name {
newSet.Register(enum)
}
}
l.enumSet = newSet
}
enum := &EnumBase{
name: def.Name,
value: def.Value,
description: def.Description,
aliases: def.Aliases,
jsonConfig: DefaultJSONConfig(),
}
// Only register if we're not skipping
if l.options.DuplicateHandling != DuplicateSkip || !l.enumSet.Contains(enum) {
l.enumSet.Register(enum)
}
}
return nil
}
// ExportToJSON exports the current enum set to a JSON file
func (l *DynamicEnumLoader) ExportToJSON(filename string) error {
definitions := make([]EnumDefinition, 0)
for _, enum := range l.enumSet.Values() {
definitions = append(definitions, EnumDefinition{
Name: enum.String(),
Value: enum.Value(),
Description: enum.Description(),
Aliases: enum.Aliases(),
})
}
data, err := json.MarshalIndent(definitions, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal enums: %w", err)
}
return os.WriteFile(filename, data, 0644)
}