Skip to content

Commit

Permalink
all: make use of some more Go 1.22 std APIs
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Jul 23, 2024
1 parent 1aaf802 commit af83dad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cue/load/loader_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *importStack) Pop() {
}

func (s *importStack) Copy() []string {
return append([]string{}, *s...)
return slices.Clone(*s)
}

type fileProcessor struct {
Expand Down
14 changes: 4 additions & 10 deletions encoding/gocode/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gocode

import (
"bytes"
"cmp"
"fmt"
"go/ast"
"go/format"
Expand Down Expand Up @@ -159,7 +160,7 @@ func Generate(pkgPath string, inst cue.InstanceOrValue, c *Config) (b []byte, er

g.exec(loadCode, map[string]string{
"runtime": g.RuntimeVar,
"prefix": strValue(g.Prefix, defaultPrefix),
"prefix": cmp.Or(g.Prefix, defaultPrefix),
"data": string(b),
})

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

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

// @go attribute options
"func": isFunc,
"validate": lookupName(attr, "validate", strValue(g.ValidateName, "Validate")),
"validate": lookupName(attr, "validate", cmp.Or(g.ValidateName, "Validate")),
"complete": lookupName(attr, "complete", g.CompleteName),
})
}
Expand All @@ -267,13 +268,6 @@ func lookupName(attr cue.Attribute, option, config string) string {
return name
}

func strValue(have, fallback string) string {
if have == "" {
return fallback
}
return have
}

func mappedGoTypes(s string) bool {
switch s {
case "bool", "float32", "float64",
Expand Down
15 changes: 5 additions & 10 deletions internal/_e2e/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package e2e_test

import (
"bytes"
"cmp"
"context"
cryptorand "crypto/rand"
"fmt"
"os"
Expand Down Expand Up @@ -74,17 +76,17 @@ var (
// githubPublicRepo is a GitHub public repository
// with the "cue.works authz" GitHub App installed.
// The repository can be entirely empty, as it's only needed for authz.
githubPublicRepo = envOr("GITHUB_PUBLIC_REPO", "github.com/cue-labs-modules-testing/e2e-public")
githubPublicRepo = cmp.Or(os.Getenv("GITHUB_PUBLIC_REPO"), "github.com/cue-labs-modules-testing/e2e-public")

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

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

func TestScript(t *testing.T) {
Expand Down Expand Up @@ -180,13 +182,6 @@ func TestScript(t *testing.T) {

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

func envOr(name, fallback string) string {
if s := os.Getenv(name); s != "" {
return s
}
return fallback
}

func envMust(t *testing.T, name string) string {
if s := os.Getenv(name); s != "" {
return s
Expand Down
5 changes: 3 additions & 2 deletions pkg/path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"reflect"
"runtime"
"slices"
"testing"
)

Expand Down Expand Up @@ -111,7 +112,7 @@ var wincleantests = []PathTest{

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

func TestRel(t *testing.T) {
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
tests := append([]RelTests{}, reltests...)
tests := slices.Clone(reltests)
if os == Windows {
for i := range tests {
tests[i].want = FromSlash(tests[i].want, Windows)
Expand Down

0 comments on commit af83dad

Please sign in to comment.