Skip to content

Commit

Permalink
all: make use of the new slices.Clone and cmp.Or Go 1.22 APIs
Browse files Browse the repository at this point in the history
Minor but nice code simplifications.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I5b3c23af78d2ca2f1eb2a8cdf82d63af32a1158a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198626
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mvdan committed Jul 30, 2024
1 parent 862fcf1 commit 55d1cb1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
6 changes: 2 additions & 4 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"cmp"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -71,10 +72,7 @@ func defaultConfig() (*config, error) {
}

func getLang() language.Tag {
loc := os.Getenv("LC_ALL")
if loc == "" {
loc = os.Getenv("LANG")
}
loc := cmp.Or(os.Getenv("LC_ALL"), os.Getenv("LANG"))
loc, _, _ = strings.Cut(loc, ".")
return language.Make(loc)
}
Expand Down
11 changes: 3 additions & 8 deletions cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"bytes"
"cmp"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -415,10 +416,7 @@ func protoMode(b *buildPlan) error {
func genericMode(cmd *Command, b *buildPlan) error {
pkgFlag := flagPackage.String(cmd)
for _, pkg := range b.insts {
pkgName := pkgFlag
if pkgName == "" {
pkgName = pkg.PkgName
}
pkgName := cmp.Or(pkgFlag, pkg.PkgName)
// TODO: allow if there is a unique package name.
if pkgName == "" && len(b.insts) > 1 {
return fmt.Errorf("must specify package name with the -p flag")
Expand All @@ -435,10 +433,7 @@ func genericMode(cmd *Command, b *buildPlan) error {
}

func getFilename(b *buildPlan, f *ast.File, root string, force bool) (filename string, err error) {
cueFile := f.Filename
if out := flagOutFile.String(b.cmd); out != "" {
cueFile = out
}
cueFile := cmp.Or(flagOutFile.String(b.cmd), f.Filename)

if cueFile != "-" {
switch _, err := os.Stat(cueFile); {
Expand Down
7 changes: 3 additions & 4 deletions cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cue

import (
"cmp"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/build"
Expand Down Expand Up @@ -181,10 +183,7 @@ func (c *Context) BuildExpr(x ast.Expr, options ...BuildOption) Value {
// and the expression resulting from CompileString differently.
astutil.ResolveExpr(x, errFn)

pkgPath := cfg.ImportPath
if pkgPath == "" {
pkgPath = anonymousPkg
}
pkgPath := cmp.Or(cfg.ImportPath, anonymousPkg)

conjunct, err := compile.Expr(&cfg.Config, r, pkgPath, x)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cue/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ func (p list) sanitize() list {
if p == nil {
return p
}
a := make(list, len(p))
copy(a, p)
a := slices.Clone(p)
a.RemoveMultiples()
return a
}
Expand Down
4 changes: 2 additions & 2 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package adt

import (
"fmt"
"slices"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
Expand Down Expand Up @@ -727,13 +728,12 @@ func (v *Vertex) ToDataAll(ctx *OpContext) *Vertex {
w.BaseValue = toDataAll(ctx, w.BaseValue)
w.Arcs = arcs
w.isData = true
w.Conjuncts = make([]Conjunct, len(v.Conjuncts))
w.Conjuncts = slices.Clone(v.Conjuncts)
// TODO(perf): this is not strictly necessary for evaluation, but it can
// hurt performance greatly. Drawback is that it may disable ordering.
for _, s := range w.Structs {
s.Disable = true
}
copy(w.Conjuncts, v.Conjuncts)
for i, c := range w.Conjuncts {
if v, _ := c.x.(Value); v != nil {
w.Conjuncts[i].x = toDataAll(ctx, v).(Value)
Expand Down
9 changes: 4 additions & 5 deletions internal/core/adt/disjunct.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package adt

import (
"slices"

"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
)
Expand Down Expand Up @@ -505,9 +507,7 @@ func clone(v Vertex) Vertex {
case unprocessed:
a := *arc
v.Arcs[i] = &a

a.Conjuncts = make([]Conjunct, len(arc.Conjuncts))
copy(a.Conjuncts, arc.Conjuncts)
a.Conjuncts = slices.Clone(arc.Conjuncts)

default:
a := *arc
Expand All @@ -520,8 +520,7 @@ func clone(v Vertex) Vertex {
}

if a := v.Structs; len(a) > 0 {
v.Structs = make([]*StructInfo, len(a))
copy(v.Structs, a)
v.Structs = slices.Clone(a)
}

return v
Expand Down

0 comments on commit 55d1cb1

Please sign in to comment.