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

ReadJSON() now returns error #101

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion node/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module m { prefix ""; namespace ""; revision 0;
return nodeutil.ReflectChild(out), nil
},
})
in := nodeutil.ReadJSON(`{"name":"joe"}`)
in, _ := nodeutil.ReadJSON(`{"name":"joe"}`)
sel, err := b.Root().Find("sayHello")
fc.RequireEqual(t, nil, err)
out, err := sel.Action(in)
Expand Down
3 changes: 2 additions & 1 deletion node/check_when_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func TestWhen(t *testing.T) {
t.Fatal(err)
}
for _, d := range test.data {
b := node.NewBrowser(m, nodeutil.ReadJSON(d.in))
n, _ := nodeutil.ReadJSON(d.in)
b := node.NewBrowser(m, n)
actual, err := nodeutil.WriteJSON(b.Root())
if err != nil {
t.Error(err)
Expand Down
29 changes: 16 additions & 13 deletions node/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestChoiceInAction(t *testing.T) {
leaf y {
type string;
}
}
}
}
}
}`
Expand All @@ -44,7 +44,7 @@ func TestChoiceInAction(t *testing.T) {
}
root := node.NewBrowser(m, n).Root()
expected := `{"x":"hello"}`
in := nodeutil.ReadJSON(expected)
in, _ := nodeutil.ReadJSON(expected)
sel, err := root.Find("r")
fc.RequireEqual(t, nil, err)
_, err = sel.Action(in)
Expand Down Expand Up @@ -157,11 +157,12 @@ func TestChoiceLeafUpsert(t *testing.T) {
b := node.NewBrowser(m, nodeutil.ReflectChild(data))
sel, err := b.Root().Find("a")
fc.RequireEqual(t, nil, err)
err = sel.UpsertFrom(nodeutil.ReadJSON(`
n, _ := nodeutil.ReadJSON(`
{
"bb" : "y"
}
`))
`)
err = sel.UpsertFrom(n)
fc.RequireEqual(t, nil, err)
actual, err := nodeutil.WriteJSON(sel)
fc.RequireEqual(t, nil, err)
Expand Down Expand Up @@ -205,11 +206,12 @@ func TestChoiceContainerUpsert(t *testing.T) {
}
b := node.NewBrowser(m, nodeutil.ReflectChild(data))
sel := b.Root()
err = sel.UpsertFrom(nodeutil.ReadJSON(`
n, _ := nodeutil.ReadJSON(`
{
"b" : {"bb" : "y"}
}
`))
`)
err = sel.UpsertFrom(n)
fc.AssertEqual(t, nil, err)
actual, err := nodeutil.WriteJSON(sel)
fc.AssertEqual(t, nil, err)
Expand Down Expand Up @@ -380,7 +382,7 @@ func TestEditListItem(t *testing.T) {
}
root := testDataRoot()
bd := nodeutil.ReflectChild(root)
json := nodeutil.ReadJSON(`{"origin":{"country":"Canada"}}`)
json, _ := nodeutil.ReadJSON(`{"origin":{"country":"Canada"}}`)

// UPDATE
// Here we're testing editing a specific list item. With FindTarget walk controller
Expand Down Expand Up @@ -414,7 +416,7 @@ func TestEditListItem(t *testing.T) {
}
]
}`
json = nodeutil.ReadJSON(insertData)
json, _ = nodeutil.ReadJSON(insertData)
sel, err = rootSel.Find("fruits")
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, nil, sel.InsertFrom(json))
Expand All @@ -441,7 +443,7 @@ func TestEditChoiceInGroup(t *testing.T) {
}
}
}

uses g;
`,
data: `{"a":"hi"}`,
Expand All @@ -453,19 +455,19 @@ func TestEditChoiceInGroup(t *testing.T) {
leaf e {
type string;
}
choice q {
choice q {
case a {
container a {
leaf aa {
type string;
}
}
}
}
case b {
container b {
leaf bb {
type string;
}
}
}
}
}
Expand Down Expand Up @@ -495,7 +497,8 @@ func TestEditChoiceInGroup(t *testing.T) {
data := make(map[string]interface{})
n := nodeutil.ReflectChild(data)
b := node.NewBrowser(m, n)
err = b.Root().UpsertFromSetDefaults(nodeutil.ReadJSON(test.data))
in, _ := nodeutil.ReadJSON(test.data)
err = b.Root().UpsertFromSetDefaults(in)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion node/field_constraints_browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func TestFieldConstraintsRequests(t *testing.T) {
data := make(map[string]interface{})
b := node.NewBrowser(m, nodeutil.ReflectChild(data))
root := b.Root()
err = root.UpsertFrom(nodeutil.ReadJSON(test.JSON))
n, _ := nodeutil.ReadJSON(test.JSON)
err = root.UpsertFrom(n)
fc.AssertEqual(t, test.valid, err == nil, test.JSON)
}
}
4 changes: 2 additions & 2 deletions node/fields_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module x {
if err != nil {
t.Fatal(err)
}
n := nodeutil.ReadJSON(`
n, _ := nodeutil.ReadJSON(`
{
"a" : {
"a": "A",
Expand Down Expand Up @@ -58,7 +58,7 @@ module x {
if err != nil {
t.Fatal(err)
}
n := nodeutil.ReadJSON(`
n, _ := nodeutil.ReadJSON(`
{
"a" : [{
"id" : "1",
Expand Down
10 changes: 6 additions & 4 deletions node/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ func TestReplaceFrom(t *testing.T) {
// container
sel, err := root.Find("bird=robin/species")
fc.RequireEqual(t, nil, err)
err = sel.ReplaceFrom(nodeutil.ReadJSON(`
n, _ := nodeutil.ReadJSON(`
{"species":{"name":"dragon"}}
`))
`)
err = sel.ReplaceFrom(n)
fc.AssertEqual(t, nil, err)
sel, err = root.Find("bird=robin")
fc.RequireEqual(t, nil, err)
Expand All @@ -96,9 +97,10 @@ func TestReplaceFrom(t *testing.T) {
// list item
sel, err = root.Find("bird=robin")
fc.RequireEqual(t, nil, err)
sel.ReplaceFrom(nodeutil.ReadJSON(`
n, _ = nodeutil.ReadJSON(`
{"bird":[{"name": "robin", "wingspan":11}]}
`))
`)
sel.ReplaceFrom(n)
fc.AssertEqual(t, nil, err)
actual, err = js.JSON(root)
fc.AssertEqual(t, nil, err)
Expand Down
2 changes: 1 addition & 1 deletion node/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestWalkJson(t *testing.T) {
}`
ypath := source.Dir("../parser/testdata")
m := parser.RequireModule(ypath, "rtstone")
rdr := nodeutil.ReadJSON(config)
rdr, _ := nodeutil.ReadJSON(config)
sel := node.NewBrowser(m, rdr).Root()
if actual, err := nodeutil.WriteJSON(sel); err != nil {
t.Error(err)
Expand Down
11 changes: 6 additions & 5 deletions node/xpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestXFind(t *testing.T) {
//fc.DebugLog(true)
mstr := ` module m { namespace ""; prefix ""; revision 0;
mstr := ` module m { namespace ""; prefix ""; revision 0;
container a {
leaf b {
type int32;
Expand All @@ -27,24 +27,25 @@ func TestXFind(t *testing.T) {
leaf bbb {
type boolean;
}
}
}
list list {
leaf leaf {
type int32;
}
}
}
}
`
m, err := parser.LoadModuleFromString(nil, mstr)
if err != nil {
t.Fatal(err)
}
b := node.NewBrowser(m, nodeutil.ReadJSON(`{
n, _ := nodeutil.ReadJSON(`{
"a":{"b":10},
"aa":{"bb":"hello"},
"aaa":{"bbb":true},
"list":[{"leaf":99},{"leaf":100}]
}`))
}`)
b := node.NewBrowser(m, n)
tests := []struct {
xpath string
expected string
Expand Down
6 changes: 3 additions & 3 deletions nodeutil/config_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ func TestConfigProxy(t *testing.T) {
})

t.Run("editContainer", func(t *testing.T) {
edit := nodeutil.ReadJSON(`{"class":"thrush"}`)
edit, _ := nodeutil.ReadJSON(`{"class":"thrush"}`)
sel, err := proxy.Root().Find("bird=robin/species")
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, nil, sel.InsertFrom(edit))
fc.AssertEqual(t, "thrush", localData["robin"].Species.Class)
})

t.Run("editList", func(t *testing.T) {
edit := nodeutil.ReadJSON(`{"wingspan":10}`)
edit, _ := nodeutil.ReadJSON(`{"wingspan":10}`)
sel, err := proxy.Root().Find("bird=robin")
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, nil, sel.UpsertFrom(edit))
fc.AssertEqual(t, 10, localData["robin"].Wingspan)
})

t.Run("addListItem", func(t *testing.T) {
edit := nodeutil.ReadJSON(`{"bird":[{"name":"owl"}]}`)
edit, _ := nodeutil.ReadJSON(`{"bird":[{"name":"owl"}]}`)
sel, err := proxy.Root().Find("bird")
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, nil, sel.InsertFrom(edit))
Expand Down
3 changes: 2 additions & 1 deletion nodeutil/copy_on_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func TestCopyOnWrite(t *testing.T) {
sel, err = sel.Find(test.sel)
fc.RequireEqual(t, nil, err)
}
fc.RequireEqual(t, nil, sel.UpsertFrom(nodeutil.ReadJSON(test.change)))
n, _ := nodeutil.ReadJSON(test.change)
fc.RequireEqual(t, nil, sel.UpsertFrom(n))
fc.AssertEqual(t, 1, len(aBirds))
actual, _ := nodeutil.WritePrettyJSON(b.Root())
fc.Gold(t, *updateFlag, []byte(actual), test.gold)
Expand Down
4 changes: 2 additions & 2 deletions nodeutil/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module m {
}

// new
a := ReadJSON(`{
a, _ := ReadJSON(`{
"movie" : {
"mame" : "StarWars",
"character" : {
Expand All @@ -60,7 +60,7 @@ module m {
`)

// old
b := ReadJSON(`{
b, _ := ReadJSON(`{
"movie" : {
"mame" : "StarWars",
"character" : {
Expand Down
22 changes: 13 additions & 9 deletions nodeutil/example_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
// that confirms to the model.
func ExampleBasic_onField() {
model := `
leaf foo {
type string;
leaf foo {
type string;
}`

data := &nodeutil.Basic{
Expand All @@ -43,7 +43,8 @@ func ExampleBasic_onField() {

examplePrint(sel)

sel.UpsertFrom(nodeutil.ReadJSON(`{"foo":"WRITE"}`))
n, _ := nodeutil.ReadJSON(`{"foo":"WRITE"}`)
sel.UpsertFrom(n)
// Output:
// {"foo":"READ"}
// WRITE
Expand All @@ -58,8 +59,8 @@ type foo struct {
func ExampleBasic_onChild() {
model := `
container foo {
leaf bar {
type string;
leaf bar {
type string;
}
}
`
Expand Down Expand Up @@ -100,7 +101,8 @@ func ExampleBasic_onChild() {
examplePrint(sel)

fmt.Println("Creating")
sel.InsertFrom(nodeutil.ReadJSON(`{"foo":{"bar":"y"}}`))
n, _ := nodeutil.ReadJSON(`{"foo":{"bar":"y"}}`)
sel.InsertFrom(n)
examplePrint(sel)

// Output:
Expand All @@ -122,7 +124,7 @@ func ExampleBasic_onNext() {
model := `
list foo {
key "bar";
leaf bar {
leaf bar {
type string;
}
}`
Expand Down Expand Up @@ -192,7 +194,8 @@ func ExampleBasic_onNext() {
examplePrint(sel)

fmt.Println("Creating")
err = sel.UpsertFrom(nodeutil.ReadJSON(`{"foo":[{"bar":"b"}]}`))
n, _ := nodeutil.ReadJSON(`{"foo":[{"bar":"b"}]}`)
err = sel.UpsertFrom(n)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -256,7 +259,8 @@ func ExampleBasic_onAction() {

// JSON is a useful format to use as input, but this can come from any node
// that would return "a" and "b" leaves.
result, err := sel.Action(nodeutil.ReadJSON(`{"a":10,"b":32}`))
n, _ := nodeutil.ReadJSON(`{"a":10,"b":32}`)
result, err := sel.Action(n)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions nodeutil/example_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ExampleReadJSON() {

myApp := make(map[string]interface{})
sel := exampleSelection(model, nodeutil.ReflectChild(myApp))
data := `{
n, _ := nodeutil.ReadJSON(`{
"bird" : [{
"name": "swallow",
"wingSpan" : 10
Expand All @@ -48,8 +48,8 @@ func ExampleReadJSON() {
"continent" : "africa"
}
}
`
if err := sel.InsertFrom(nodeutil.ReadJSON(data)); err != nil {
`)
if err := sel.InsertFrom(n); err != nil {
fmt.Print(err.Error())
}
out, _ := nodeutil.WriteJSON(sel)
Expand Down
Loading