-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kinda like the Python JSON loads function. Takes in a string and tries to parse it as json, converting it recursively into native RSL types. For example, if you have a valid JSON dictionary as a string, and pass that in, it will return an RslMap with the constituent fields (all the way through) likewise converted to native RSL types. This should be useful for operations where you get a JSON string returns (e.g. via a shell command or a rad request) and you want to inspect/interact with the json blob as it is.
- Loading branch information
Showing
10 changed files
with
186 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,5 @@ const ( | |
UNIQUE = "unique" | ||
SORT_FUNC = "sort" | ||
CONFIRM = "confirm" | ||
PARSE_JSON = "parse_json" | ||
) |
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,26 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func runParseJson(i *MainInterpreter, function Token, args []interface{}) interface{} { | ||
if len(args) != 1 { | ||
i.error(function, PARSE_JSON+fmt.Sprintf("() takes exactly one argument, got %d", len(args))) | ||
} | ||
|
||
switch coerced := args[0].(type) { | ||
case string: | ||
var m interface{} | ||
err := json.Unmarshal([]byte(coerced), &m) | ||
if err != nil { | ||
i.error(function, fmt.Sprintf("Error parsing JSON: %v", err)) | ||
} | ||
return ConvertToNativeTypes(i, function, m) | ||
default: | ||
// maybe a bit harsh, should allow just passthrough of e.g. int64? | ||
i.error(function, PARSE_JSON+fmt.Sprintf("() expects string, got %s", TypeAsString(args[0]))) | ||
panic(UNREACHABLE) | ||
} | ||
} |
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,99 @@ | ||
package testing | ||
|
||
import "testing" | ||
|
||
func TestParseJson_Int(t *testing.T) { | ||
rsl := ` | ||
a = parse_json("2") | ||
print(a + 1) | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertOnlyOutput(t, stdOutBuffer, "3\n") | ||
assertNoErrors(t) | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_Float(t *testing.T) { | ||
rsl := ` | ||
a = parse_json("2.1") | ||
print(a + 1) | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertOnlyOutput(t, stdOutBuffer, "3.1\n") | ||
assertNoErrors(t) | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_Bool(t *testing.T) { | ||
rsl := ` | ||
a = parse_json("true") | ||
print(a or false) | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertOnlyOutput(t, stdOutBuffer, "true\n") | ||
assertNoErrors(t) | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_String(t *testing.T) { | ||
rsl := ` | ||
a = parse_json('"alice"') | ||
print(a + "e") | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertOnlyOutput(t, stdOutBuffer, "alicee\n") | ||
assertNoErrors(t) | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_Map(t *testing.T) { | ||
rsl := ` | ||
a = parse_json('\{"name": "alice", "age": 20, "height": 5.5, "is_student": true, "cars": ["audi", "bmw"], "friends": \{"bob": 1, "charlie": 2}}') | ||
print(a["name"] + "e") | ||
print(a["age"] + 1) | ||
print(a["height"] + 1.1) | ||
print(a["is_student"] or false) | ||
print(a["cars"][0] + "e") | ||
print(a["friends"]["bob"] + 1) | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertOnlyOutput(t, stdOutBuffer, "alicee\n21\n6.6\ntrue\naudie\n2\n") | ||
assertNoErrors(t) | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_ErrorsOnInvalidJson(t *testing.T) { | ||
rsl := ` | ||
parse_json('\{asd asd}') | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertError(t, 1, "RslError at L2/10 on 'parse_json': Error parsing JSON: invalid character 'a' looking for beginning of object key string\n") | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_ErrorsOnInvalidType(t *testing.T) { | ||
rsl := ` | ||
parse_json(10) | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertError(t, 1, "RslError at L2/10 on 'parse_json': parse_json() expects string, got int\n") | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_ErrorsOnNoArgs(t *testing.T) { | ||
rsl := ` | ||
parse_json() | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertError(t, 1, "RslError at L2/10 on 'parse_json': parse_json() takes exactly one argument, got 0\n") | ||
resetTestState() | ||
} | ||
|
||
func TestParseJson_ErrorsOnTooManyArgs(t *testing.T) { | ||
rsl := ` | ||
parse_json("1", "2") | ||
` | ||
setupAndRunCode(t, rsl) | ||
assertError(t, 1, "RslError at L2/10 on 'parse_json': parse_json() takes exactly one argument, got 2\n") | ||
resetTestState() | ||
} |
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