Skip to content

Commit

Permalink
Merge pull request #32474 from vespa-engine/havardpe/slime-json-decoder
Browse files Browse the repository at this point in the history
json encoding and decoding
  • Loading branch information
havardpe authored Sep 26, 2024
2 parents 4719404 + cde7d2e commit 42859f0
Show file tree
Hide file tree
Showing 7 changed files with 1,080 additions and 2 deletions.
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

0 comments on commit 42859f0

Please sign in to comment.