|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// StepResult is the MVP step result shape (design doc §13.2). |
| 13 | +type StepResult struct { |
| 14 | + Output any |
| 15 | + Meta map[string]any |
| 16 | +} |
| 17 | + |
| 18 | +// Context holds values for ${input.*} and ${steps.*} interpolation (§13.1). |
| 19 | +type Context struct { |
| 20 | + Input map[string]any |
| 21 | + Steps map[string]StepResult |
| 22 | +} |
| 23 | + |
| 24 | +var tokenRE = regexp.MustCompile(`\$\{([^}]*)\}`) |
| 25 | + |
| 26 | +// InterpolateString replaces every ${...} token in s using dot-path lookup only (§13.1 MVP). |
| 27 | +// Resolved values are embedded as strings: scalars and JSON for objects/arrays. |
| 28 | +func InterpolateString(s string, ctx Context) (string, error) { |
| 29 | + var errs []error |
| 30 | + out := tokenRE.ReplaceAllStringFunc(s, func(full string) string { |
| 31 | + m := tokenRE.FindStringSubmatch(full) |
| 32 | + if m == nil { |
| 33 | + errs = append(errs, fmt.Errorf("interpolation: malformed token %q", full)) |
| 34 | + return full |
| 35 | + } |
| 36 | + path := strings.TrimSpace(m[1]) |
| 37 | + if path == "" { |
| 38 | + errs = append(errs, errors.New("interpolation: empty placeholder")) |
| 39 | + return full |
| 40 | + } |
| 41 | + val, err := resolvePath(ctx, path) |
| 42 | + if err != nil { |
| 43 | + errs = append(errs, err) |
| 44 | + return full |
| 45 | + } |
| 46 | + str, err := valueToString(val) |
| 47 | + if err != nil { |
| 48 | + errs = append(errs, err) |
| 49 | + return full |
| 50 | + } |
| 51 | + return str |
| 52 | + }) |
| 53 | + if len(errs) > 0 { |
| 54 | + return out, errors.Join(errs...) |
| 55 | + } |
| 56 | + return out, nil |
| 57 | +} |
| 58 | + |
| 59 | +// InterpolateWalk walks v recursively: it interpolates string leaves and descends into |
| 60 | +// map[string]any and []any. Other JSON-like types are left unchanged. |
| 61 | +func InterpolateWalk(v any, ctx Context) (any, error) { |
| 62 | + switch t := v.(type) { |
| 63 | + case string: |
| 64 | + return InterpolateString(t, ctx) |
| 65 | + case map[string]any: |
| 66 | + out := make(map[string]any, len(t)) |
| 67 | + for k, val := range t { |
| 68 | + iv, err := InterpolateWalk(val, ctx) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + out[k] = iv |
| 73 | + } |
| 74 | + return out, nil |
| 75 | + case []any: |
| 76 | + out := make([]any, len(t)) |
| 77 | + for i := range t { |
| 78 | + iv, err := InterpolateWalk(t[i], ctx) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + out[i] = iv |
| 83 | + } |
| 84 | + return out, nil |
| 85 | + default: |
| 86 | + return v, nil |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func resolvePath(ctx Context, path string) (any, error) { |
| 91 | + parts := splitPath(path) |
| 92 | + if len(parts) < 2 { |
| 93 | + return nil, fmt.Errorf("interpolation: path %q must use input.<field>... or steps.<id>.output|meta...", path) |
| 94 | + } |
| 95 | + switch parts[0] { |
| 96 | + case "input": |
| 97 | + if ctx.Input == nil { |
| 98 | + return nil, fmt.Errorf("interpolation: no input in context for path %q", path) |
| 99 | + } |
| 100 | + return walkAny(ctx.Input, parts[1:], path) |
| 101 | + case "steps": |
| 102 | + if len(parts) < 3 { |
| 103 | + return nil, fmt.Errorf("interpolation: path %q must be steps.<step_id>.output|meta...", path) |
| 104 | + } |
| 105 | + stepID := parts[1] |
| 106 | + if ctx.Steps == nil { |
| 107 | + return nil, fmt.Errorf("interpolation: unknown step %q", stepID) |
| 108 | + } |
| 109 | + sr, ok := ctx.Steps[stepID] |
| 110 | + if !ok { |
| 111 | + return nil, fmt.Errorf("interpolation: unknown step %q", stepID) |
| 112 | + } |
| 113 | + switch parts[2] { |
| 114 | + case "output": |
| 115 | + return walkAny(sr.Output, parts[3:], path) |
| 116 | + case "meta": |
| 117 | + if sr.Meta == nil { |
| 118 | + return nil, fmt.Errorf("interpolation: step %q has no meta", stepID) |
| 119 | + } |
| 120 | + return walkAny(sr.Meta, parts[3:], path) |
| 121 | + default: |
| 122 | + return nil, fmt.Errorf("interpolation: steps.%s must use .output or .meta, not %q", stepID, parts[2]) |
| 123 | + } |
| 124 | + default: |
| 125 | + return nil, fmt.Errorf("interpolation: path must start with input or steps, not %q", parts[0]) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func splitPath(path string) []string { |
| 130 | + var parts []string |
| 131 | + for _, p := range strings.Split(path, ".") { |
| 132 | + p = strings.TrimSpace(p) |
| 133 | + if p != "" { |
| 134 | + parts = append(parts, p) |
| 135 | + } |
| 136 | + } |
| 137 | + return parts |
| 138 | +} |
| 139 | + |
| 140 | +func walkAny(v any, parts []string, fullPath string) (any, error) { |
| 141 | + if len(parts) == 0 { |
| 142 | + return v, nil |
| 143 | + } |
| 144 | + m, ok := v.(map[string]any) |
| 145 | + if !ok { |
| 146 | + return nil, fmt.Errorf("interpolation: cannot resolve %q: need map at %q, got %T", fullPath, parts[0], v) |
| 147 | + } |
| 148 | + next, ok := m[parts[0]] |
| 149 | + if !ok { |
| 150 | + return nil, fmt.Errorf("interpolation: undefined path %q (missing %q)", fullPath, parts[0]) |
| 151 | + } |
| 152 | + return walkAny(next, parts[1:], fullPath) |
| 153 | +} |
| 154 | + |
| 155 | +func valueToString(v any) (string, error) { |
| 156 | + if v == nil { |
| 157 | + return "", nil |
| 158 | + } |
| 159 | + switch x := v.(type) { |
| 160 | + case string: |
| 161 | + return x, nil |
| 162 | + case bool: |
| 163 | + return strconv.FormatBool(x), nil |
| 164 | + case int: |
| 165 | + return strconv.Itoa(x), nil |
| 166 | + case int64: |
| 167 | + return strconv.FormatInt(x, 10), nil |
| 168 | + case float64: |
| 169 | + return strconv.FormatFloat(x, 'g', -1, 64), nil |
| 170 | + case json.Number: |
| 171 | + return x.String(), nil |
| 172 | + default: |
| 173 | + b, err := json.Marshal(x) |
| 174 | + if err != nil { |
| 175 | + return "", fmt.Errorf("interpolation: encode value: %w", err) |
| 176 | + } |
| 177 | + return string(b), nil |
| 178 | + } |
| 179 | +} |
0 commit comments