this repo has no description
0
fork

Configure Feed

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

encoing/json: made independent of pkg/encoding/json

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

Change-Id: I1589d17f0de5f93b66709cdefa70aca16fe13c0c
Signed-off-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/532279
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
880863af 802a8520

+25 -21
+23 -10
encoding/json/json.go
··· 16 16 package json 17 17 18 18 import ( 19 - gojson "encoding/json" 19 + "encoding/json" 20 + "fmt" 20 21 "io" 21 22 "strings" 22 23 ··· 27 28 "cuelang.org/go/cue/literal" 28 29 "cuelang.org/go/cue/parser" 29 30 "cuelang.org/go/cue/token" 30 - "cuelang.org/go/pkg/encoding/json" 31 + "cuelang.org/go/internal/value" 31 32 ) 32 33 33 34 // Valid reports whether data is a valid JSON encoding. 34 35 func Valid(b []byte) bool { 35 - return gojson.Valid(b) 36 + return json.Valid(b) 36 37 } 37 38 38 39 // Validate validates JSON and confirms it matches the constraints 39 40 // specified by v. 40 41 func Validate(b []byte, v cue.Value) error { 41 - _, err := json.Validate(b, v) 42 - return err 42 + if !json.Valid(b) { 43 + return fmt.Errorf("json: invalid JSON") 44 + } 45 + r := value.ConvertToRuntime(v.Context()) 46 + inst, err := r.Compile("json.Validate", b) 47 + if err != nil { 48 + return err 49 + } 50 + 51 + v = v.Unify(inst.Value()) 52 + if v.Err() != nil { 53 + return v.Err() 54 + } 55 + return nil 43 56 } 44 57 45 58 // Extract parses JSON-encoded data to a CUE expression, using path for ··· 67 80 68 81 func extract(path string, b []byte) (ast.Expr, error) { 69 82 expr, err := parser.ParseExpr(path, b) 70 - if err != nil || !gojson.Valid(b) { 83 + if err != nil || !json.Valid(b) { 71 84 p := token.NoPos 72 85 if pos := errors.Positions(err); len(pos) > 0 { 73 86 p = pos[0] 74 87 } 75 88 var x interface{} 76 - err := gojson.Unmarshal(b, &x) 89 + err := json.Unmarshal(b, &x) 77 90 return nil, errors.Wrapf(err, p, "invalid JSON for file %q", path) 78 91 } 79 92 return expr, nil ··· 88 101 return &Decoder{ 89 102 r: r, 90 103 path: path, 91 - dec: gojson.NewDecoder(src), 104 + dec: json.NewDecoder(src), 92 105 offset: 1, 93 106 } 94 107 } ··· 97 110 type Decoder struct { 98 111 r *cue.Runtime 99 112 path string 100 - dec *gojson.Decoder 113 + dec *json.Decoder 101 114 offset int 102 115 } 103 116 ··· 113 126 } 114 127 115 128 func (d *Decoder) extract() (ast.Expr, error) { 116 - var raw gojson.RawMessage 129 + var raw json.RawMessage 117 130 err := d.dec.Decode(&raw) 118 131 if err == io.EOF { 119 132 return nil, err
+2 -11
pkg/encoding/json/manual.go
··· 24 24 "cuelang.org/go/cue/errors" 25 25 "cuelang.org/go/cue/parser" 26 26 "cuelang.org/go/cue/token" 27 - "cuelang.org/go/internal/value" 27 + cuejson "cuelang.org/go/encoding/json" 28 28 ) 29 29 30 30 // Compact generates the JSON-encoded src with insignificant space characters ··· 108 108 // Validate validates JSON and confirms it matches the constraints 109 109 // specified by v. 110 110 func Validate(b []byte, v cue.Value) (bool, error) { 111 - if !json.Valid(b) { 112 - return false, fmt.Errorf("json: invalid JSON") 113 - } 114 - r := value.ConvertToRuntime(v.Context()) 115 - inst, err := r.Compile("json.Validate", b) 111 + err := cuejson.Validate(b, v) 116 112 if err != nil { 117 113 return false, err 118 - } 119 - 120 - v = v.Unify(inst.Value()) 121 - if v.Err() != nil { 122 - return false, v.Err() 123 114 } 124 115 return true, nil 125 116 }