Skip to content

Commit af83dad

Browse files
committed
all: make use of some more Go 1.22 std APIs
I was reminded that I had written two funcs similar to cmp.Or, and there was one in encoding/gocode as well. While here, use the new slices package in a few more places. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ifd058cf54bba140c91bc317ba46ee7e83ba47e9e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198292 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Paul Jolly <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 1aaf802 commit af83dad

File tree

4 files changed

+13
-23
lines changed

4 files changed

+13
-23
lines changed

cue/load/loader_common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (s *importStack) Pop() {
9494
}
9595

9696
func (s *importStack) Copy() []string {
97-
return append([]string{}, *s...)
97+
return slices.Clone(*s)
9898
}
9999

100100
type fileProcessor struct {

encoding/gocode/generator.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package gocode
1616

1717
import (
1818
"bytes"
19+
"cmp"
1920
"fmt"
2021
"go/ast"
2122
"go/format"
@@ -159,7 +160,7 @@ func Generate(pkgPath string, inst cue.InstanceOrValue, c *Config) (b []byte, er
159160

160161
g.exec(loadCode, map[string]string{
161162
"runtime": g.RuntimeVar,
162-
"prefix": strValue(g.Prefix, defaultPrefix),
163+
"prefix": cmp.Or(g.Prefix, defaultPrefix),
163164
"data": string(b),
164165
})
165166

@@ -244,14 +245,14 @@ func (g *generator) decl(name string, v cue.Value) {
244245
}
245246

246247
g.exec(stubCode, map[string]interface{}{
247-
"prefix": strValue(g.Prefix, defaultPrefix),
248+
"prefix": cmp.Or(g.Prefix, defaultPrefix),
248249
"cueName": name, // the field name of the CUE type
249250
"goType": goType, // the receiver or argument type
250251
"zero": zero, // the zero value of the underlying type
251252

252253
// @go attribute options
253254
"func": isFunc,
254-
"validate": lookupName(attr, "validate", strValue(g.ValidateName, "Validate")),
255+
"validate": lookupName(attr, "validate", cmp.Or(g.ValidateName, "Validate")),
255256
"complete": lookupName(attr, "complete", g.CompleteName),
256257
})
257258
}
@@ -267,13 +268,6 @@ func lookupName(attr cue.Attribute, option, config string) string {
267268
return name
268269
}
269270

270-
func strValue(have, fallback string) string {
271-
if have == "" {
272-
return fallback
273-
}
274-
return have
275-
}
276-
277271
func mappedGoTypes(s string) bool {
278272
switch s {
279273
case "bool", "float32", "float64",

internal/_e2e/script_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package e2e_test
1616

1717
import (
1818
"bytes"
19+
"cmp"
20+
"context"
1921
cryptorand "crypto/rand"
2022
"fmt"
2123
"os"
@@ -74,17 +76,17 @@ var (
7476
// githubPublicRepo is a GitHub public repository
7577
// with the "cue.works authz" GitHub App installed.
7678
// The repository can be entirely empty, as it's only needed for authz.
77-
githubPublicRepo = envOr("GITHUB_PUBLIC_REPO", "github.com/cue-labs-modules-testing/e2e-public")
79+
githubPublicRepo = cmp.Or(os.Getenv("GITHUB_PUBLIC_REPO"), "github.com/cue-labs-modules-testing/e2e-public")
7880

7981
// githubPublicRepo is a GitHub private repository
8082
// with the "cue.works authz" GitHub App installed.
8183
// The repository can be entirely empty, as it's only needed for authz.
82-
githubPrivateRepo = envOr("GITHUB_PRIVATE_REPO", "github.com/cue-labs-modules-testing/e2e-private")
84+
githubPrivateRepo = cmp.Or(os.Getenv("GITHUB_PRIVATE_REPO"), "github.com/cue-labs-modules-testing/e2e-private")
8385

8486
// gcloudRegistry is an existing Google Cloud Artifact Registry repository
8587
// to publish module versions to via "cue mod publish",
8688
// and authenticated via gcloud's configuration in the host environment.
87-
gcloudRegistry = envOr("GCLOUD_REGISTRY", "europe-west1-docker.pkg.dev/project-unity-377819/modules-e2e-registry")
89+
gcloudRegistry = cmp.Or(os.Getenv("GCLOUD_REGISTRY"), "europe-west1-docker.pkg.dev/project-unity-377819/modules-e2e-registry")
8890
)
8991

9092
func TestScript(t *testing.T) {
@@ -180,13 +182,6 @@ func TestScript(t *testing.T) {
180182

181183
func addr[T any](t T) *T { return &t }
182184

183-
func envOr(name, fallback string) string {
184-
if s := os.Getenv(name); s != "" {
185-
return s
186-
}
187-
return fallback
188-
}
189-
190185
func envMust(t *testing.T, name string) string {
191186
if s := os.Getenv(name); s != "" {
192187
return s

pkg/path/path_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"reflect"
2424
"runtime"
25+
"slices"
2526
"testing"
2627
)
2728

@@ -111,7 +112,7 @@ var wincleantests = []PathTest{
111112

112113
func TestClean(t *testing.T) {
113114
testEachOS(t, []OS{Unix, Windows, Plan9}, func(t *testing.T, os OS) {
114-
tests := append([]PathTest{}, cleantests...) // TODO: replace with slices.Clone
115+
tests := slices.Clone(cleantests)
115116
if os == Windows {
116117
for i := range tests {
117118
tests[i].result = FromSlash(tests[i].result, os)
@@ -562,7 +563,7 @@ var winreltests = []RelTests{
562563

563564
func TestRel(t *testing.T) {
564565
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
565-
tests := append([]RelTests{}, reltests...)
566+
tests := slices.Clone(reltests)
566567
if os == Windows {
567568
for i := range tests {
568569
tests[i].want = FromSlash(tests[i].want, Windows)

0 commit comments

Comments
 (0)