-
Notifications
You must be signed in to change notification settings - Fork 15
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 #82 from nofun97/json-func
JSON to arrai object
- Loading branch information
Showing
6 changed files
with
100 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ func stdScope() rel.Scope { | |
stdStr(), | ||
stdEval(), | ||
stdOs(), | ||
stdJSON(), | ||
)) | ||
}) | ||
return stdScopeVar | ||
|
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,26 @@ | ||
package syntax | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/arr-ai/arrai/rel" | ||
"github.com/arr-ai/arrai/translate" | ||
) | ||
|
||
func stdJSON() rel.Attr { | ||
return rel.NewTupleAttr( | ||
"json", | ||
rel.NewNativeFunctionAttr("decode", func(v rel.Value) rel.Value { | ||
s := mustAsString(v) | ||
var data interface{} | ||
var err error | ||
if err = json.Unmarshal([]byte(s), &data); err == nil { | ||
var d rel.Value | ||
if d, err = translate.JSONToArrai(data); err == nil { | ||
return d | ||
} | ||
} | ||
panic(err) | ||
}), | ||
) | ||
} |
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,42 @@ | ||
package syntax | ||
|
||
import "testing" | ||
|
||
func TestJSONDecode(t *testing.T) { | ||
t.Parallel() | ||
AssertCodePanics(t, `//.json.decode(123)`) | ||
AssertCodesEvalToSameValue(t, | ||
`{ | ||
"a": (s: "string"), | ||
"b": 123, | ||
"c": 123.321, | ||
"d": (a: [1, (s: "string again"), (a: []), {}]), | ||
"e": { | ||
"f": { | ||
"g": (s: "321") | ||
}, | ||
"h": (a: []) | ||
}, | ||
"i": (null: {}), | ||
"j": (a: [(b: {()}), (b: {})]), | ||
"k": (s: {}) | ||
}`, | ||
`//.json.decode( | ||
'{ | ||
"a": "string", | ||
"b": 123, | ||
"c": 123.321, | ||
"d": [1, "string again", [], {}], | ||
"e": { | ||
"f": { | ||
"g": "321" | ||
}, | ||
"h": [] | ||
}, | ||
"i": null, | ||
"j": [true, false], | ||
"k": "" | ||
}' | ||
)`, | ||
) | ||
} |
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