|
| 1 | +package module |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/GoCodeAlone/modular" |
| 9 | +) |
| 10 | + |
| 11 | +// JSONParseStep parses a JSON string value from the pipeline context into a |
| 12 | +// structured Go value (map, slice, etc.) and stores the result as step output. |
| 13 | +// |
| 14 | +// This is useful when a pipeline step (e.g. step.db_query against a legacy |
| 15 | +// driver, or step.http_call) returns a JSON column/field as a raw string rather |
| 16 | +// than as a pre-parsed Go type. It is the explicit counterpart to the automatic |
| 17 | +// json/jsonb detection that step.db_query performs for the pgx driver. |
| 18 | +// |
| 19 | +// Configuration: |
| 20 | +// |
| 21 | +// source: "steps.fetch.row.json_column" # dot-path to the JSON string value (required) |
| 22 | +// target: "parsed_data" # output key name (optional, defaults to "value") |
| 23 | +type JSONParseStep struct { |
| 24 | + name string |
| 25 | + source string |
| 26 | + target string |
| 27 | +} |
| 28 | + |
| 29 | +// NewJSONParseStepFactory returns a StepFactory that creates JSONParseStep instances. |
| 30 | +func NewJSONParseStepFactory() StepFactory { |
| 31 | + return func(name string, config map[string]any, _ modular.Application) (PipelineStep, error) { |
| 32 | + source, _ := config["source"].(string) |
| 33 | + if source == "" { |
| 34 | + return nil, fmt.Errorf("json_parse step %q: 'source' is required", name) |
| 35 | + } |
| 36 | + |
| 37 | + target, _ := config["target"].(string) |
| 38 | + if target == "" { |
| 39 | + target = "value" |
| 40 | + } |
| 41 | + |
| 42 | + return &JSONParseStep{ |
| 43 | + name: name, |
| 44 | + source: source, |
| 45 | + target: target, |
| 46 | + }, nil |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Name returns the step name. |
| 51 | +func (s *JSONParseStep) Name() string { return s.name } |
| 52 | + |
| 53 | +// Execute resolves the source path, parses the value as JSON if it is a string, |
| 54 | +// and stores the result under the configured target key. |
| 55 | +func (s *JSONParseStep) Execute(_ context.Context, pc *PipelineContext) (*StepResult, error) { |
| 56 | + raw := resolveBodyFrom(s.source, pc) |
| 57 | + if raw == nil { |
| 58 | + return nil, fmt.Errorf("json_parse step %q: source %q not found or resolved to nil", s.name, s.source) |
| 59 | + } |
| 60 | + |
| 61 | + var parsed any |
| 62 | + switch v := raw.(type) { |
| 63 | + case string: |
| 64 | + if err := json.Unmarshal([]byte(v), &parsed); err != nil { |
| 65 | + return nil, fmt.Errorf("json_parse step %q: failed to parse JSON from %q: %w", s.name, s.source, err) |
| 66 | + } |
| 67 | + case []byte: |
| 68 | + if err := json.Unmarshal(v, &parsed); err != nil { |
| 69 | + return nil, fmt.Errorf("json_parse step %q: failed to parse JSON bytes from %q: %w", s.name, s.source, err) |
| 70 | + } |
| 71 | + default: |
| 72 | + // Value is already a structured type (map, slice, number, bool, nil). |
| 73 | + // Pass it through unchanged so that pipelines are idempotent when the |
| 74 | + // upstream step already returns a parsed value (e.g. after the db_query |
| 75 | + // fix lands, json_parse is a no-op for json/jsonb columns). |
| 76 | + parsed = raw |
| 77 | + } |
| 78 | + |
| 79 | + return &StepResult{Output: map[string]any{ |
| 80 | + s.target: parsed, |
| 81 | + }}, nil |
| 82 | +} |
0 commit comments