this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg/encoding/json: implement UnmarshalStream

Signed-off-by: Marcel van Lohuizen <mpvl@golang.org>

Change-Id: Id8484437779dba22c9b01220a23356fdd639f8a0
Signed-off-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/532281
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>

authored by

Marcel van Lohuizen and committed by
Marcel van Lohuizen
9aeaf704 480b28b1

+52
+21
pkg/encoding/json/manual.go
··· 18 18 "bytes" 19 19 "encoding/json" 20 20 "fmt" 21 + "io" 21 22 22 23 "cuelang.org/go/cue" 23 24 "cuelang.org/go/cue/ast" ··· 90 91 buf.WriteByte('\n') 91 92 } 92 93 return buf.String(), nil 94 + } 95 + 96 + // UnmarshalStream parses the JSON to a CUE instance. 97 + func UnmarshalStream(data []byte) (ast.Expr, error) { 98 + var r cue.Runtime 99 + d := cuejson.NewDecoder(&r, "", bytes.NewReader(data)) 100 + 101 + a := []ast.Expr{} 102 + for { 103 + x, err := d.Extract() 104 + if err == io.EOF { 105 + break 106 + } 107 + if err != nil { 108 + return nil, err 109 + } 110 + a = append(a, x) 111 + } 112 + 113 + return ast.NewList(a...), nil 93 114 } 94 115 95 116 // Unmarshal parses the JSON-encoded data.
+12
pkg/encoding/json/pkg.go
··· 92 92 } 93 93 }, 94 94 }, { 95 + Name: "UnmarshalStream", 96 + Params: []internal.Param{ 97 + {Kind: adt.BytesKind | adt.StringKind}, 98 + }, 99 + Result: adt.TopKind, 100 + Func: func(c *internal.CallCtxt) { 101 + data := c.Bytes(0) 102 + if c.Do() { 103 + c.Ret, c.Err = UnmarshalStream(data) 104 + } 105 + }, 106 + }, { 95 107 Name: "Unmarshal", 96 108 Params: []internal.Param{ 97 109 {Kind: adt.BytesKind | adt.StringKind},
+19
pkg/encoding/json/testdata/gen.txtar
··· 15 15 y: json.Marshal({a: x}) 16 16 } 17 17 t9: json.MarshalStream([{a: 1}, {b: int | *2}]) 18 + 19 + unmarshalStream: { 20 + t1: json.UnmarshalStream(#"{"a": 1}{"b": 2}"#) 21 + t1: json.UnmarshalStream(#'{"a": 1}{"b": 2}'#) 22 + empty: json.UnmarshalStream('') 23 + empty: json.UnmarshalStream("") 24 + nums: json.UnmarshalStream('1 2') 25 + nums: json.UnmarshalStream("1 2") 26 + } 27 + 18 28 -- out/json -- 19 29 Errors: 20 30 a: error in call to encoding/json.Validate: invalid value 10 (out of bound <3): ··· 52 62 {"b":2} 53 63 54 64 """ 65 + unmarshalStream: { 66 + t1: [{ 67 + a: 1 68 + }, { 69 + b: 2 70 + }] 71 + empty: [] 72 + nums: [1, 2] 73 + } 55 74