Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wildcards in json:// and yaml:// so that values can be retrieved with slice #545

Merged
merged 7 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 57 additions & 23 deletions vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package runn
import (
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"text/template"

"github.com/bmatcuk/doublestar/v4"
"github.com/goccy/go-json"
"github.com/goccy/go-yaml"
)

const multiple = "*"

type evaluator struct {
scheme string
exts []string
Expand All @@ -30,45 +35,51 @@ var (
func evaluateSchema(value any, operationRoot string, store map[string]any) (any, error) {
switch v := value.(type) {
case string:
var targetEvaluator *evaluator
var e *evaluator
for _, evaluator := range evaluators {
if strings.HasPrefix(v, evaluator.scheme) {
targetEvaluator = evaluator
e = evaluator
}
}

if targetEvaluator == nil {
if e == nil {
return value, nil
}

p := v[len(targetEvaluator.scheme):]
if !hasExts(p, targetEvaluator.exts) && !hasTemplateSuffix(p, targetEvaluator.exts) {
p := v[len(e.scheme):]
if strings.Contains(p, "://") {
return value, fmt.Errorf("invalid path: %s", v)
}
if !hasExts(p, e.exts) && !hasTemplateSuffix(p, e.exts) {
return value, fmt.Errorf("unsupported file extension: %s", p)
}
if operationRoot != "" {
p = filepath.Join(operationRoot, p)
}
byteArray, err := readFile(p)
if err != nil {
return value, fmt.Errorf("read external files error: %w", err)
}
if store != nil && hasTemplateSuffix(p, targetEvaluator.exts) {
tmpl, err := template.New(p).Parse(string(byteArray))

if strings.Contains(p, multiple) {
base, pattern := doublestar.SplitPattern(p)
fsys := os.DirFS(base)
matches, err := doublestar.Glob(fsys, pattern)
if err != nil {
return value, fmt.Errorf("template parse error: %w", err)
return value, fmt.Errorf("glob error: %w", err)
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, store); err != nil {
return value, fmt.Errorf("template excute error: %w", err)
sort.Slice(matches, func(i, j int) bool { return matches[i] < matches[j] })
outs := []any{}
for _, m := range matches {
out, err := evalutateFile(filepath.Join(base, m), store, e)
if err != nil {
return value, fmt.Errorf("evaluate file error: %w", err)
}
outs = append(outs, out)
}
byteArray = buf.Bytes()
}
var evaluatedObj any
if err := targetEvaluator.unmarshal(byteArray, &evaluatedObj); err != nil {
return value, fmt.Errorf("unmarshal error: %w", err)
return outs, nil
} else {
out, err := evalutateFile(p, store, e)
if err != nil {
return value, fmt.Errorf("evaluate file error: %w", err)
}
return out, nil
}

return evaluatedObj, nil
}

return value, nil
Expand All @@ -91,3 +102,26 @@ func hasTemplateSuffix(p string, exts []string) bool {
}
return false
}

func evalutateFile(p string, store map[string]any, e *evaluator) (any, error) {
b, err := readFile(p)
if err != nil {
return nil, fmt.Errorf("read external files error: %w", err)
}
if store != nil && hasTemplateSuffix(p, e.exts) {
tmpl, err := template.New(p).Parse(string(b))
if err != nil {
return nil, fmt.Errorf("template parse error: %w", err)
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, store); err != nil {
return nil, fmt.Errorf("template excute error: %w", err)
}
b = buf.Bytes()
}
var out any
if err := e.unmarshal(b, &out); err != nil {
return nil, fmt.Errorf("unmarshal error: %w", err)
}
return out, nil
}
7 changes: 7 additions & 0 deletions vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func TestEvaluateSchema(t *testing.T) {
"",
true,
},
{"json://testdata/vars*.json", nil, []any{
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vars.json and vars_array.json

map[string]any{"foo": "test", "bar": float64(1)},
[]any{
map[string]any{"foo": "test1", "bar": float64(1)},
map[string]any{"foo": "test2", "bar": float64(2)},
},
}, false},
}
for i, tt := range tests {
tt := tt
Expand Down