Skip to content

Commit c65dfa3

Browse files
committed
cmd/cue: verify the JSON for equivalence in get_go_json_compat
We took the JSON from `cue export` and unmarshalled it into the Go type to verify it is still compatible, but we did not actually check that the values are unchanged. This might not be the case if we, for example, dropped a key or changed one of the key values. This also means that we don't need to do a Go json.Unmarshal check; if Go's json.Marshal results in exactly the same bytes as `cue export`, then it's safe to assume that json.Unmarshal would work too. While here, make sure we use `cue vet` in concrete mode. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Id488eddfba3120d5d8f443a10e008805954a0d9d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1211575 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent f567a0c commit c65dfa3

File tree

1 file changed

+14
-41
lines changed

1 file changed

+14
-41
lines changed

cmd/cue/cmd/testdata/script/get_go_json_compat.txtar

+14-41
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ cmp decls/p_go_gen.cue decls/p_go_gen.cue.golden
66

77
# Marshal via Go's encoding/json and validate with the generated CUE schema.
88
exec go run ./marshal
9-
cp stdout go_marshal.json
10-
cmp go_marshal.json go_marshal.json.golden
11-
exec cue vet go_marshal.json schema.cue
9+
cmp stdout go_marshal.json
10+
exec cue vet -c schema.cue go_marshal.json
1211

13-
# Export from CUE and Unmarshal via Go's encoding/json.
12+
# Export from CUE to verify that it results in the same JSON as Go did.
13+
# This helps catch if any of the keys have been dropped or their values changed.
1414
exec cue export ./decls
15-
cmp stdout cue_export.json.golden
16-
stdin stdout
17-
exec go run ./unmarshal
15+
cmp stdout go_marshal.json
1816

1917
-- schema.cue --
2018
package schema
@@ -41,16 +39,6 @@ package decls
4139
#ResetColor,
4240
]
4341
}
44-
-- go_marshal.json.golden --
45-
{"Duration":3000000000,"Strings":["\u001b[31mError:\u001b[0m","\u001b[0m"]}
46-
-- cue_export.json.golden --
47-
{
48-
"Duration": 3000000000,
49-
"Strings": [
50-
"\u001b[31mError:\u001b[0m",
51-
"\u001b[0m"
52-
]
53-
}
5442
-- decls/p.go --
5543
package decls
5644

@@ -102,33 +90,18 @@ func main() {
10290
decls.ResetColor,
10391
},
10492
}
105-
data, err := json.Marshal(r)
93+
// Mimic the formatting of `cue export` to be able to compare bytes.
94+
data, err := json.MarshalIndent(r, "", " ")
10695
if err != nil {
10796
log.Fatal(err)
10897
}
10998
fmt.Printf("%s\n", data)
11099
}
111-
-- unmarshal/main.go --
112-
package main
113-
114-
import (
115-
"encoding/json"
116-
"io"
117-
"os"
118-
"fmt"
119-
"log"
120-
121-
"mod.test/decls"
122-
)
123-
124-
func main() {
125-
data, err := io.ReadAll(os.Stdin)
126-
if err != nil {
127-
log.Fatal(err)
128-
}
129-
var r decls.Root
130-
if err := json.Unmarshal(data, &r); err != nil {
131-
log.Fatal(err)
132-
}
133-
fmt.Printf("%s\n", data)
100+
-- go_marshal.json --
101+
{
102+
"Duration": 3000000000,
103+
"Strings": [
104+
"\u001b[31mError:\u001b[0m",
105+
"\u001b[0m"
106+
]
134107
}

0 commit comments

Comments
 (0)