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

json encoding and decoding #32474

Merged
merged 3 commits into from
Sep 26, 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
32 changes: 32 additions & 0 deletions client/go/internal/vespa/slime/error.go
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 }
32 changes: 32 additions & 0 deletions client/go/internal/vespa/slime/inserter.go
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)
})
}
32 changes: 32 additions & 0 deletions client/go/internal/vespa/slime/inserter_test.go
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())
}
Loading
Loading