-
Notifications
You must be signed in to change notification settings - Fork 603
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 #32474 from vespa-engine/havardpe/slime-json-decoder
json encoding and decoding
- Loading branch information
Showing
7 changed files
with
1,080 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
|
||
package slime | ||
|
||
import "errors" | ||
|
||
var ( | ||
Invalid Value = ErrorMsg("invalid value") | ||
) | ||
|
||
type errorValue struct { | ||
emptyValue | ||
err error | ||
} | ||
|
||
func ErrorMsg(msg string) Value { | ||
return &errorValue{err: errors.New(msg)} | ||
} | ||
|
||
func Error(err error) Value { | ||
return &errorValue{err: err} | ||
} | ||
|
||
func AsError(value Value) error { | ||
ev, ok := value.(*errorValue) | ||
if ok { | ||
return ev.err | ||
} | ||
return nil | ||
} | ||
|
||
func (*errorValue) Valid() bool { return false } |
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 @@ | ||
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
|
||
package slime | ||
|
||
type Inserter interface { | ||
Insert(value Value) Value | ||
} | ||
|
||
type myInserter func(value Value) Value | ||
|
||
func (f myInserter) Insert(value Value) Value { | ||
return f(value) | ||
} | ||
|
||
func InsertRoot(root *Value) Inserter { | ||
return myInserter(func(value Value) Value { | ||
*root = value | ||
return value | ||
}) | ||
} | ||
|
||
func InsertEntry(arr Value) Inserter { | ||
return myInserter(func(value Value) Value { | ||
return arr.Add(value) | ||
}) | ||
} | ||
|
||
func InsertField(obj Value, name string) Inserter { | ||
return myInserter(func(value Value) Value { | ||
return obj.Set(name, value) | ||
}) | ||
} |
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 @@ | ||
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
|
||
package slime | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestInsertRoot(t *testing.T) { | ||
var root Value | ||
in := InsertRoot(&root) | ||
res := in.Insert(Long(5)) | ||
assert.Equal(t, int64(5), root.AsLong()) | ||
assert.Equal(t, int64(5), res.AsLong()) | ||
} | ||
|
||
func TestInsertEntry(t *testing.T) { | ||
arr := Array() | ||
in := InsertEntry(arr) | ||
res := in.Insert(Long(5)) | ||
assert.Equal(t, int64(5), arr.Entry(0).AsLong()) | ||
assert.Equal(t, int64(5), res.AsLong()) | ||
} | ||
|
||
func TestInsertField(t *testing.T) { | ||
obj := Object() | ||
in := InsertField(obj, "foo") | ||
res := in.Insert(Long(5)) | ||
assert.Equal(t, int64(5), obj.Field("foo").AsLong()) | ||
assert.Equal(t, int64(5), res.AsLong()) | ||
} |
Oops, something went wrong.