Merge pull request #36 from sbarzowski/toString

Add toString builtin
This commit is contained in:
Stanisław Barzowski 2017-09-06 18:39:19 -04:00 committed by Dave Cunningham
parent 77b8b9e335
commit c9e23d4ff3
20 changed files with 34 additions and 1 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package jsonnet
import (
"bytes"
"math"
"sort"
@ -152,6 +153,19 @@ func builtinLength(e *evaluator, xp potentialValue) (value, error) {
return makeValueNumber(float64(num)), nil
}
func builtinToString(e *evaluator, xp potentialValue) (value, error) {
x, err := e.evaluate(xp)
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = e.i.manifestJSON(e.trace, x, false, "", &buf)
if err != nil {
return nil, err
}
return makeValueString(buf.String()), nil
}
func builtinMakeArray(e *evaluator, szp potentialValue, funcp potentialValue) (value, error) {
sz, err := e.evaluateNumber(szp)
if err != nil {
@ -432,6 +446,7 @@ var uopBuiltins = []*UnaryBuiltin{
// TODO(sbarzowski) eliminate duplication in function names (e.g. build map from array or constants)
var funcBuiltins = map[string]evalCallable{
"length": &UnaryBuiltin{name: "length", function: builtinLength, parameters: ast.Identifiers{"x"}},
"toString": &UnaryBuiltin{name: "toString", function: builtinToString, parameters: ast.Identifiers{"x"}},
"makeArray": &BinaryBuiltin{name: "makeArray", function: builtinMakeArray, parameters: ast.Identifiers{"sz", "func"}},
"primitiveEquals": &BinaryBuiltin{name: "primitiveEquals", function: primitiveEquals, parameters: ast.Identifiers{"sz", "func"}},
"objectFieldsEx": &BinaryBuiltin{name: "objectFields", function: builtinObjectFieldsEx, parameters: ast.Identifiers{"obj", "hidden"}},

View File

@ -460,7 +460,7 @@ func unparseString(v string) string {
for _, c := range v {
switch c {
case '"':
buf.WriteString("\n")
buf.WriteString("\\\"")
case '\\':
buf.WriteString("\\\\")
case '\b':

1
testdata/std.toString.golden vendored Normal file
View File

@ -0,0 +1 @@
"\"xxx\""

1
testdata/std.toString.input vendored Normal file
View File

@ -0,0 +1 @@
std.toString("xxx")

1
testdata/std.toString2.golden vendored Normal file
View File

@ -0,0 +1 @@
"2"

1
testdata/std.toString2.input vendored Normal file
View File

@ -0,0 +1 @@
std.toString(2)

1
testdata/std.toString3.golden vendored Normal file
View File

@ -0,0 +1 @@
"{ }"

1
testdata/std.toString3.input vendored Normal file
View File

@ -0,0 +1 @@
std.toString({})

1
testdata/std.toString4.golden vendored Normal file
View File

@ -0,0 +1 @@
"[ ]"

1
testdata/std.toString4.input vendored Normal file
View File

@ -0,0 +1 @@
std.toString([])

1
testdata/std.toString5.golden vendored Normal file
View File

@ -0,0 +1 @@
RUNTIME ERROR: x

1
testdata/std.toString5.input vendored Normal file
View File

@ -0,0 +1 @@
std.toString(error "x")

1
testdata/std.toString6.golden vendored Normal file
View File

@ -0,0 +1 @@
"{\"x\": [{\"y\": [ ]}]}"

1
testdata/std.toString6.input vendored Normal file
View File

@ -0,0 +1 @@
std.toString({x: [{y: []}]})

1
testdata/std.toString7.golden vendored Normal file
View File

@ -0,0 +1 @@
"[{\"a\": [42, 42, { }], \"b\": 1337}, 42, \"text\", 42]"

1
testdata/std.toString7.input vendored Normal file
View File

@ -0,0 +1 @@
std.toString([{a: [42, 42, {}], ["b"]: 1337}, 42, "text", 42])

1
testdata/string.golden vendored Normal file
View File

@ -0,0 +1 @@
"xxx"

1
testdata/string.input vendored Normal file
View File

@ -0,0 +1 @@
"xxx"

1
testdata/string2.golden vendored Normal file
View File

@ -0,0 +1 @@
"\"xxx\""

1
testdata/string2.input vendored Normal file
View File

@ -0,0 +1 @@
"\"xxx\""