Skip to content

Commit 3bf3dbd

Browse files
committed
encoding/gocode: don't require output to be stable
gocode generates a Go variable cuegenInstanceData whose value is a string containing CUE values encoded via Runtime.Marshal. Said Marshal method encodes data via encoding/gob and then compresses the encoded gob with compress/gzip. This is perfectly fine, but the result isn't guaranteed to be stable byte-by-byte between Go versions, since the gob format evolves and gzip may get better at compressing data over time. In particular, in Go 1.21, https://go.dev/cl/460543 changed the encoding slightly by having the first user type be registered in encoding/gob with type id 64 rather than 65. Given that encoding/gob had had a constant `const firstUserId = 64` for a long time, we assume that user types starting with type ID 65 was a minor mistake. That upstream change shouldn't break any reasonable gob user. However, we were expecting encoding/gob's output to be perfectly stable, which is certainly not reasonable to do. The tests in gen_test.go already import generated Go code in testdata, so we do cover whether the generated code does what we expect it to. We thus don't really need to check the output is stable, we just need a way to update the output files in testdata as needed. For that purpose, `go generate` is perfectly fine. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I24b85c8c648a573ccca3c670f320f7e91278046e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167483 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 99e8578 commit 3bf3dbd

File tree

4 files changed

+81
-93
lines changed

4 files changed

+81
-93
lines changed

encoding/gocode/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
//go:generate go run testdata/gen.go
16+
1517
// Package gocode defines functions for extracting CUE definitions from Go code
1618
// and generating Go code from CUE values.
1719
//

encoding/gocode/gen_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package gocode
1818

1919
import (
20+
"bytes"
21+
"regexp"
2022
"strings"
2123
"testing"
2224

25+
"cuelang.org/go/cue/errors"
2326
"cuelang.org/go/encoding/gocode/testdata/pkg1"
2427
"cuelang.org/go/encoding/gocode/testdata/pkg2"
2528
)
@@ -107,3 +110,13 @@ O.P: invalid value 4 (out of bound >5):
107110
})
108111
}
109112
}
113+
114+
func errStr(err error) string {
115+
if err == nil {
116+
return "nil"
117+
}
118+
buf := &bytes.Buffer{}
119+
errors.Print(buf, err, nil)
120+
r := regexp.MustCompile(`.cue:\d+:\d+`)
121+
return r.ReplaceAllString(buf.String(), ".cue:x:x")
122+
}

encoding/gocode/generator_test.go

-93
This file was deleted.

encoding/gocode/testdata/gen.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2019 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"io/ioutil"
19+
"log"
20+
"os"
21+
"path/filepath"
22+
23+
"cuelang.org/go/cue"
24+
"cuelang.org/go/cue/load"
25+
"cuelang.org/go/encoding/gocode"
26+
_ "cuelang.org/go/pkg"
27+
)
28+
29+
func main() {
30+
dirs, err := ioutil.ReadDir("testdata")
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
35+
cwd, err := os.Getwd()
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
40+
for _, d := range dirs {
41+
if !d.IsDir() {
42+
continue
43+
}
44+
dir := filepath.Join(cwd, "testdata")
45+
pkg := "." + string(filepath.Separator) + d.Name()
46+
inst := cue.Build(load.Instances([]string{pkg}, &load.Config{
47+
Dir: dir,
48+
ModuleRoot: dir,
49+
Module: "cuelang.org/go/encoding/gocode/testdata",
50+
}))[0]
51+
if err := inst.Err; err != nil {
52+
log.Fatal(err)
53+
}
54+
55+
goPkg := "./testdata/" + d.Name()
56+
b, err := gocode.Generate(goPkg, inst, nil)
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
goFile := filepath.Join("testdata", d.Name(), "cue_gen.go")
62+
if err := os.WriteFile(goFile, b, 0644); err != nil {
63+
log.Fatal(err)
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)