|
| 1 | +package regresql |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +type JSONGenerator struct { |
| 6 | + registry *GeneratorRegistry |
| 7 | +} |
| 8 | + |
| 9 | +func NewJSONGenerator(registry *GeneratorRegistry) *JSONGenerator { |
| 10 | + return &JSONGenerator{registry: registry} |
| 11 | +} |
| 12 | + |
| 13 | +func (g *JSONGenerator) Name() string { return "json" } |
| 14 | + |
| 15 | +func (g *JSONGenerator) Validate(params map[string]any, column *ColumnInfo) error { |
| 16 | + _, hasSchema := params["schema"] |
| 17 | + _, hasValue := params["value"] |
| 18 | + |
| 19 | + if !hasSchema && !hasValue { |
| 20 | + return fmt.Errorf("json generator requires either 'schema' or 'value' parameter") |
| 21 | + } |
| 22 | + if hasSchema && hasValue { |
| 23 | + return fmt.Errorf("json generator cannot have both 'schema' and 'value'") |
| 24 | + } |
| 25 | + |
| 26 | + if hasSchema { |
| 27 | + schema, ok := params["schema"].(map[string]any) |
| 28 | + if !ok { |
| 29 | + return fmt.Errorf("'schema' must be an object") |
| 30 | + } |
| 31 | + return g.validateSchema(schema) |
| 32 | + } |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +func (g *JSONGenerator) validateSchema(schema map[string]any) error { |
| 37 | + for field, spec := range schema { |
| 38 | + specMap, ok := spec.(map[string]any) |
| 39 | + if !ok { |
| 40 | + continue // static value |
| 41 | + } |
| 42 | + |
| 43 | + genName, ok := specMap["generator"].(string) |
| 44 | + if !ok { |
| 45 | + continue // static value (object without generator key) |
| 46 | + } |
| 47 | + |
| 48 | + gen, err := g.registry.Get(genName) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("field '%s': %w", field, err) |
| 51 | + } |
| 52 | + |
| 53 | + if err := gen.Validate(specMap, nil); err != nil { |
| 54 | + return fmt.Errorf("field '%s': %w", field, err) |
| 55 | + } |
| 56 | + } |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func (g *JSONGenerator) Generate(params map[string]any, column *ColumnInfo) (any, error) { |
| 61 | + // Static value mode |
| 62 | + if value, ok := params["value"]; ok { |
| 63 | + return value, nil |
| 64 | + } |
| 65 | + |
| 66 | + // Schema mode |
| 67 | + schema := params["schema"].(map[string]any) |
| 68 | + result := make(map[string]any) |
| 69 | + |
| 70 | + for field, spec := range schema { |
| 71 | + specMap, ok := spec.(map[string]any) |
| 72 | + if !ok { |
| 73 | + result[field] = spec |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + genName, ok := specMap["generator"].(string) |
| 78 | + if !ok { |
| 79 | + result[field] = spec |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + gen, err := g.registry.Get(genName) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("field '%s': %w", field, err) |
| 86 | + } |
| 87 | + |
| 88 | + value, err := gen.Generate(specMap, nil) |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("field '%s': %w", field, err) |
| 91 | + } |
| 92 | + result[field] = value |
| 93 | + } |
| 94 | + |
| 95 | + return result, nil |
| 96 | +} |
0 commit comments