Skip to content

Commit 7ba104c

Browse files
committed
internal/core/convert: do not roundtrip MarshalText to JSON
It produces text (a string) as a []byte, so there is no need to encode that string as JSON only to decode it again. We can directly treat the result like we do other strings. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Iab1106a0c89fce495fd2817124cd10f577246d42 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201804 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent b37ab01 commit 7ba104c

File tree

1 file changed

+7
-17
lines changed
  • internal/core/convert

1 file changed

+7
-17
lines changed

internal/core/convert/go.go

+7-17
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"cuelang.org/go/internal"
3737
"cuelang.org/go/internal/core/adt"
3838
"cuelang.org/go/internal/core/compile"
39-
internaljson "cuelang.org/go/internal/encoding/json"
4039
"cuelang.org/go/internal/types"
4140
)
4241

@@ -184,15 +183,6 @@ func isOmitEmpty(f *reflect.StructField) bool {
184183
return isOmitEmpty
185184
}
186185

187-
// parseJSON parses JSON into a CUE value. b must be valid JSON.
188-
func parseJSON(ctx *adt.OpContext, b []byte) adt.Value {
189-
expr, err := parser.ParseExpr("json", b)
190-
if err != nil {
191-
panic(err) // cannot happen
192-
}
193-
return compileExpr(ctx, expr)
194-
}
195-
196186
func GoValueToExpr(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Expr {
197187
e := convertRec(ctx, nilIsTop, x)
198188
if e == nil {
@@ -287,19 +277,19 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
287277
if err != nil {
288278
return ctx.AddErr(errors.Promote(err, "json.Marshaler"))
289279
}
290-
291-
return parseJSON(ctx, b)
280+
expr, err := parser.ParseExpr("json", b)
281+
if err != nil {
282+
panic(err) // cannot happen
283+
}
284+
return compileExpr(ctx, expr)
292285

293286
case encoding.TextMarshaler:
294287
b, err := v.MarshalText()
295288
if err != nil {
296289
return ctx.AddErr(errors.Promote(err, "encoding.TextMarshaler"))
297290
}
298-
b, err = internaljson.Marshal(string(b))
299-
if err != nil {
300-
return ctx.AddErr(errors.Promote(err, "json"))
301-
}
302-
return parseJSON(ctx, b)
291+
s, _ := unicode.UTF8.NewEncoder().String(string(b))
292+
return &adt.String{Src: ctx.Source(), Str: s}
303293

304294
case error:
305295
var errs errors.Error

0 commit comments

Comments
 (0)