Skip to content

Commit

Permalink
encoing/json: made independent of pkg/encoding/json
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Jan 31, 2022
1 parent 802a852 commit 880863a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
33 changes: 23 additions & 10 deletions encoding/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package json

import (
gojson "encoding/json"
"encoding/json"
"fmt"
"io"
"strings"

Expand All @@ -27,19 +28,31 @@ import (
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/pkg/encoding/json"
"cuelang.org/go/internal/value"
)

// Valid reports whether data is a valid JSON encoding.
func Valid(b []byte) bool {
return gojson.Valid(b)
return json.Valid(b)
}

// Validate validates JSON and confirms it matches the constraints
// specified by v.
func Validate(b []byte, v cue.Value) error {
_, err := json.Validate(b, v)
return err
if !json.Valid(b) {
return fmt.Errorf("json: invalid JSON")
}
r := value.ConvertToRuntime(v.Context())
inst, err := r.Compile("json.Validate", b)
if err != nil {
return err
}

v = v.Unify(inst.Value())
if v.Err() != nil {
return v.Err()
}
return nil
}

// Extract parses JSON-encoded data to a CUE expression, using path for
Expand Down Expand Up @@ -67,13 +80,13 @@ func Decode(r *cue.Runtime, path string, data []byte) (*cue.Instance, error) {

func extract(path string, b []byte) (ast.Expr, error) {
expr, err := parser.ParseExpr(path, b)
if err != nil || !gojson.Valid(b) {
if err != nil || !json.Valid(b) {
p := token.NoPos
if pos := errors.Positions(err); len(pos) > 0 {
p = pos[0]
}
var x interface{}
err := gojson.Unmarshal(b, &x)
err := json.Unmarshal(b, &x)
return nil, errors.Wrapf(err, p, "invalid JSON for file %q", path)
}
return expr, nil
Expand All @@ -88,7 +101,7 @@ func NewDecoder(r *cue.Runtime, path string, src io.Reader) *Decoder {
return &Decoder{
r: r,
path: path,
dec: gojson.NewDecoder(src),
dec: json.NewDecoder(src),
offset: 1,
}
}
Expand All @@ -97,7 +110,7 @@ func NewDecoder(r *cue.Runtime, path string, src io.Reader) *Decoder {
type Decoder struct {
r *cue.Runtime
path string
dec *gojson.Decoder
dec *json.Decoder
offset int
}

Expand All @@ -113,7 +126,7 @@ func (d *Decoder) Extract() (ast.Expr, error) {
}

func (d *Decoder) extract() (ast.Expr, error) {
var raw gojson.RawMessage
var raw json.RawMessage
err := d.dec.Decode(&raw)
if err == io.EOF {
return nil, err
Expand Down
13 changes: 2 additions & 11 deletions pkg/encoding/json/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/value"
cuejson "cuelang.org/go/encoding/json"
)

// Compact generates the JSON-encoded src with insignificant space characters
Expand Down Expand Up @@ -108,18 +108,9 @@ func Unmarshal(b []byte) (ast.Expr, error) {
// Validate validates JSON and confirms it matches the constraints
// specified by v.
func Validate(b []byte, v cue.Value) (bool, error) {
if !json.Valid(b) {
return false, fmt.Errorf("json: invalid JSON")
}
r := value.ConvertToRuntime(v.Context())
inst, err := r.Compile("json.Validate", b)
err := cuejson.Validate(b, v)
if err != nil {
return false, err
}

v = v.Unify(inst.Value())
if v.Err() != nil {
return false, v.Err()
}
return true, nil
}

0 comments on commit 880863a

Please sign in to comment.