encoding/json: belated fixes of review
See https://cue-review.googlesource.com/c/cue/+/3380/
Change-Id: I2341fce5474fc685be4c95fe23c48ea9b9fb4922
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3441
Reviewed-by: roger peppe <rogpeppe@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/json/json.go b/encoding/json/json.go
index 4551370..e273686 100644
--- a/encoding/json/json.go
+++ b/encoding/json/json.go
@@ -12,11 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-// Package yaml converts JSON to and from CUE.
+// Package json converts JSON to and from CUE.
package json
import (
- "bytes"
gojson "encoding/json"
"io"
"strconv"
@@ -29,7 +28,6 @@
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
- "cuelang.org/go/internal/source"
"cuelang.org/go/pkg/encoding/json"
)
@@ -45,14 +43,14 @@
return err
}
-// Extract parses the YAML to a CUE expression.
+// Extract parses the JSON to a CUE expression.
//
// If src != nil, Extract parses the source from src and the path is for
// position information. The type of the argument for the src parameter must be
// string, []byte, or io.Reader. If src == nil, ParseFile parses the file
// specified by filename.
-func Extract(path string, src interface{}) (ast.Expr, error) {
- expr, err := extract(path, src)
+func Extract(path string, b []byte) (ast.Expr, error) {
+ expr, err := extract(path, b)
if err != nil {
return nil, err
}
@@ -60,28 +58,23 @@
return expr, nil
}
-// Decode converts JSON file to a CUE value.
+// Decode converts a JSON file to a CUE value.
//
// If src != nil, Extract parses the source from src and the path is for
// position information. The type of the argument for the src parameter must be
// string, []byte, or io.Reader. If src == nil, ParseFile parses the file
// specified by filename.
-func Decode(r *cue.Runtime, path string, src interface{}) (*cue.Instance, error) {
- expr, err := extract(path, src)
+func Decode(r *cue.Runtime, path string, b []byte) (*cue.Instance, error) {
+ expr, err := extract(path, b)
if err != nil {
return nil, err
}
return r.CompileExpr(expr)
}
-func extract(path string, src interface{}) (ast.Expr, error) {
- b, err := source.Read(path, src)
- if err != nil {
- return nil, err
- }
+func extract(path string, b []byte) (ast.Expr, error) {
expr, err := parser.ParseExpr(path, b)
if err != nil || !gojson.Valid(b) {
- // Get JSON-specific error, but
p := token.NoPos
if pos := errors.Positions(err); len(pos) > 0 {
p = pos[0]
@@ -264,7 +257,7 @@
}
func quoteMulti(a []string, indent int) string {
- b := bytes.Buffer{}
+ b := strings.Builder{}
prefix := "\n" + strings.Repeat("\t", indent)
b.WriteString(`"""`)
for _, s := range a {
diff --git a/encoding/json/json_test.go b/encoding/json/json_test.go
index 26cafd2..6c33dc2 100644
--- a/encoding/json/json_test.go
+++ b/encoding/json/json_test.go
@@ -108,7 +108,7 @@
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := &bytes.Buffer{}
- e, err := Extract(tc.name, tc.in)
+ e, err := Extract(tc.name, []byte(tc.in))
toString(out, e, err)
assert.Equal(t, tc.out, out.String())