-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from k2tzumi/feature/support-for-read-json-in-…
…var-section Support for read json in var section
- Loading branch information
Showing
7 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
desc: external vars | ||
vars: | ||
external: json://testdata/vars.json | ||
nonEvaluate: | ||
value: json://testdata/vars.json | ||
tabular: json://testdata/vars_array.json | ||
override: | ||
foo: "test2" | ||
bar: 2 | ||
steps: | ||
evaluate: | ||
test: | | ||
vars.external.foo == "test" && | ||
vars.external.bar == 1 | ||
nonEvaluate: | ||
test: | | ||
vars.nonEvaluate.value == "json://testdata/vars.json" | ||
tabular: | ||
test: | | ||
vars.tabular[1].bar == 2 | ||
override: | ||
test: | | ||
vars.override == vars.external |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"foo": "test", | ||
"bar": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"foo": "test1", | ||
"bar": 1 | ||
}, | ||
{ | ||
"foo": "test2", | ||
"bar": 2 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package runn | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/goccy/go-json" | ||
) | ||
|
||
const varsSupportScheme string = "json://" | ||
|
||
func evaluateSchema(value interface{}) (interface{}, error) { | ||
switch v := value.(type) { | ||
case string: | ||
if strings.HasPrefix(v, varsSupportScheme) { | ||
byteArray, err := os.ReadFile(v[len(varsSupportScheme):]) | ||
if err != nil { | ||
return value, fmt.Errorf("read external files error: %w", err) | ||
|
||
} | ||
var jsonObj interface{} | ||
if err := json.Unmarshal(byteArray, &jsonObj); err != nil { | ||
return value, fmt.Errorf("unmarshal error: %w", err) | ||
} | ||
|
||
return jsonObj, nil | ||
} | ||
} | ||
|
||
return value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package runn | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestEvaluateSchema(t *testing.T) { | ||
brokenJson, _ := os.CreateTemp("", "broken_json") | ||
defer os.Remove(brokenJson.Name()) | ||
|
||
tests := []struct { | ||
value interface{} | ||
want interface{} | ||
error bool | ||
}{ | ||
{1, 1, false}, | ||
{[]string{"1"}, []string{"1"}, false}, | ||
{"string", "string", false}, | ||
{"json://testdata/vars.json", map[string]interface{}{"foo": "test", "bar": float64(1)}, false}, | ||
{"json://not_exists.json", "json://not_exists.json", true}, | ||
{"json://" + brokenJson.Name(), "json://" + brokenJson.Name(), true}, | ||
} | ||
for _, tt := range tests { | ||
got, err := evaluateSchema(tt.value) | ||
if diff := cmp.Diff(got, tt.want, nil); diff != "" { | ||
t.Errorf("%s", diff) | ||
} | ||
if (tt.error && err == nil) || (!tt.error && err != nil) { | ||
t.Errorf("no much error") | ||
} | ||
} | ||
} |