Skip to content

Commit f965f9e

Browse files
authored
For compatability, emit the \n within the library, not the cmd (google#137)
* For compatability, emit the \n within the library, not the commandline tool * Also add manifestString to library
1 parent 3eaf189 commit f965f9e

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

builtins.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package jsonnet
1818

1919
import (
20+
"bytes"
2021
"crypto/md5"
2122
"encoding/hex"
2223
"fmt"
@@ -240,11 +241,12 @@ func builtinToString(e *evaluator, xp potentialValue) (value, error) {
240241
case *valueString:
241242
return x, nil
242243
}
243-
s, err := e.i.manifestAndSerializeJSON(e.trace, x, false, "")
244+
var buf bytes.Buffer
245+
err = e.i.manifestAndSerializeJSON(&buf, e.trace, x, false, "")
244246
if err != nil {
245247
return nil, err
246248
}
247-
return makeValueString(s), nil
249+
return makeValueString(buf.String()), nil
248250
}
249251

250252
func builtinMakeArray(e *evaluator, szp potentialValue, funcp potentialValue) (value, error) {

interpreter.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -723,16 +723,28 @@ func serializeJSON(v interface{}, multiline bool, indent string, buf *bytes.Buff
723723
}
724724
}
725725

726-
func (i *interpreter) manifestAndSerializeJSON(trace *TraceElement, v value, multiline bool, indent string) (string, error) {
727-
var buf bytes.Buffer
726+
func (i *interpreter) manifestAndSerializeJSON(
727+
buf *bytes.Buffer, trace *TraceElement, v value, multiline bool, indent string) error {
728728
manifested, err := i.manifestJSON(trace, v)
729729
if err != nil {
730-
return "", err
730+
return err
731731
}
732-
serializeJSON(manifested, multiline, indent, &buf)
733-
return buf.String(), nil
732+
serializeJSON(manifested, multiline, indent, buf)
733+
return nil
734734
}
735735

736+
// manifestString expects the value to be a string and returns it.
737+
func (i *interpreter) manifestString(buf *bytes.Buffer, trace *TraceElement, v value) error {
738+
switch v := v.(type) {
739+
case *valueString:
740+
buf.WriteString(v.getString())
741+
return nil
742+
default:
743+
return makeRuntimeError(fmt.Sprint("Expected string result, got: " + v.getType().name), i.getCurrentStackTrace(trace))
744+
}
745+
}
746+
747+
736748
func jsonToValue(e *evaluator, v interface{}) (value, error) {
737749
switch v := v.(type) {
738750
case nil:
@@ -879,7 +891,8 @@ func makeInitialEnv(filename string, baseStd valueObject) environment {
879891
}
880892

881893
// TODO(sbarzowski) this function takes far too many arguments - build interpreter in vm instead
882-
func evaluate(node ast.Node, ext vmExtMap, tla vmExtMap, nativeFuncs map[string]*NativeFunction, maxStack int, importer Importer) (string, error) {
894+
func evaluate(node ast.Node, ext vmExtMap, tla vmExtMap, nativeFuncs map[string]*NativeFunction,
895+
maxStack int, importer Importer, stringOutput bool) (string, error) {
883896
i, err := buildInterpreter(ext, nativeFuncs, maxStack, importer)
884897
if err != nil {
885898
return "", err
@@ -915,9 +928,15 @@ func evaluate(node ast.Node, ext vmExtMap, tla vmExtMap, nativeFuncs map[string]
915928
manifestationTrace := &TraceElement{
916929
loc: &manifestationLoc,
917930
}
918-
s, err := i.manifestAndSerializeJSON(manifestationTrace, result, true, "")
931+
var buf bytes.Buffer
932+
if stringOutput {
933+
err = i.manifestString(&buf, manifestationTrace, result)
934+
} else {
935+
err = i.manifestAndSerializeJSON(&buf, manifestationTrace, result, true, "")
936+
}
919937
if err != nil {
920938
return "", err
921939
}
922-
return s, nil
940+
buf.WriteString("\n")
941+
return buf.String(), nil
923942
}

jsonnet/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,5 @@ func main() {
114114
fmt.Fprintf(os.Stderr, "%v\n", err.Error())
115115
os.Exit(2)
116116
}
117-
fmt.Println(json)
117+
fmt.Print(json)
118118
}

main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func TestMain(t *testing.T) {
127127
// TODO(sbarzowski) perhaps somehow mark that we are processing
128128
// an error. But for now we can treat them the same.
129129
output = errFormatter.format(err)
130+
output += "\n"
130131
}
131-
output += "\n"
132132
if *update {
133133
err := ioutil.WriteFile(test.golden, []byte(output), 0666)
134134
if err != nil {
@@ -260,13 +260,13 @@ func TestCustomImporter(t *testing.T) {
260260
},
261261
})
262262
input := `[import "a.jsonnet", importstr "b.jsonnet"]`
263-
expected := `[ 4, "3 + 3" ]`
263+
expected := `[ 4, "3 + 3" ] `
264264
actual, err := vm.EvaluateSnippet("custom_import.jsonnet", input)
265265
if err != nil {
266266
t.Errorf("Unexpected error: %v", err)
267267
}
268268
actual = removeExcessiveWhitespace(actual)
269269
if actual != expected {
270-
t.Errorf("Expected %v, but got %v", expected, actual)
270+
t.Errorf("Expected %q, but got %q", expected, actual)
271271
}
272272
}

vm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type VM struct {
3838
nativeFuncs map[string]*NativeFunction
3939
importer Importer
4040
ef ErrorFormatter
41+
StringOutput bool
4142
}
4243

4344
// External variable or top level argument provided before execution
@@ -98,7 +99,7 @@ func (vm *VM) evaluateSnippet(filename string, snippet string) (output string, e
9899
if err != nil {
99100
return "", err
100101
}
101-
output, err = evaluate(node, vm.ext, vm.tla, vm.nativeFuncs, vm.MaxStack, vm.importer)
102+
output, err = evaluate(node, vm.ext, vm.tla, vm.nativeFuncs, vm.MaxStack, vm.importer, vm.StringOutput)
102103
if err != nil {
103104
return "", err
104105
}

0 commit comments

Comments
 (0)