this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

all: make use of the new slices.Clone and cmp.Or Go 1.22 APIs

Minor but nice code simplifications.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I5b3c23af78d2ca2f1eb2a8cdf82d63af32a1158a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198626
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Matthew Sackman <matthew@cue.works>

+15 -25
+2 -4
cmd/cue/cmd/common.go
··· 15 15 package cmd 16 16 17 17 import ( 18 + "cmp" 18 19 "os" 19 20 "path/filepath" 20 21 "regexp" ··· 71 72 } 72 73 73 74 func getLang() language.Tag { 74 - loc := os.Getenv("LC_ALL") 75 - if loc == "" { 76 - loc = os.Getenv("LANG") 77 - } 75 + loc := cmp.Or(os.Getenv("LC_ALL"), os.Getenv("LANG")) 78 76 loc, _, _ = strings.Cut(loc, ".") 79 77 return language.Make(loc) 80 78 }
+3 -8
cmd/cue/cmd/import.go
··· 16 16 17 17 import ( 18 18 "bytes" 19 + "cmp" 19 20 "fmt" 20 21 "os" 21 22 "path/filepath" ··· 415 416 func genericMode(cmd *Command, b *buildPlan) error { 416 417 pkgFlag := flagPackage.String(cmd) 417 418 for _, pkg := range b.insts { 418 - pkgName := pkgFlag 419 - if pkgName == "" { 420 - pkgName = pkg.PkgName 421 - } 419 + pkgName := cmp.Or(pkgFlag, pkg.PkgName) 422 420 // TODO: allow if there is a unique package name. 423 421 if pkgName == "" && len(b.insts) > 1 { 424 422 return fmt.Errorf("must specify package name with the -p flag") ··· 435 433 } 436 434 437 435 func getFilename(b *buildPlan, f *ast.File, root string, force bool) (filename string, err error) { 438 - cueFile := f.Filename 439 - if out := flagOutFile.String(b.cmd); out != "" { 440 - cueFile = out 441 - } 436 + cueFile := cmp.Or(flagOutFile.String(b.cmd), f.Filename) 442 437 443 438 if cueFile != "-" { 444 439 switch _, err := os.Stat(cueFile); {
+3 -4
cue/context.go
··· 15 15 package cue 16 16 17 17 import ( 18 + "cmp" 19 + 18 20 "cuelang.org/go/cue/ast" 19 21 "cuelang.org/go/cue/ast/astutil" 20 22 "cuelang.org/go/cue/build" ··· 181 183 // and the expression resulting from CompileString differently. 182 184 astutil.ResolveExpr(x, errFn) 183 185 184 - pkgPath := cfg.ImportPath 185 - if pkgPath == "" { 186 - pkgPath = anonymousPkg 187 - } 186 + pkgPath := cmp.Or(cfg.ImportPath, anonymousPkg) 188 187 189 188 conjunct, err := compile.Expr(&cfg.Config, r, pkgPath, x) 190 189 if err != nil {
+1 -2
cue/errors/errors.go
··· 398 398 if p == nil { 399 399 return p 400 400 } 401 - a := make(list, len(p)) 402 - copy(a, p) 401 + a := slices.Clone(p) 403 402 a.RemoveMultiples() 404 403 return a 405 404 }
+2 -2
internal/core/adt/composite.go
··· 16 16 17 17 import ( 18 18 "fmt" 19 + "slices" 19 20 20 21 "cuelang.org/go/cue/ast" 21 22 "cuelang.org/go/cue/errors" ··· 727 728 w.BaseValue = toDataAll(ctx, w.BaseValue) 728 729 w.Arcs = arcs 729 730 w.isData = true 730 - w.Conjuncts = make([]Conjunct, len(v.Conjuncts)) 731 + w.Conjuncts = slices.Clone(v.Conjuncts) 731 732 // TODO(perf): this is not strictly necessary for evaluation, but it can 732 733 // hurt performance greatly. Drawback is that it may disable ordering. 733 734 for _, s := range w.Structs { 734 735 s.Disable = true 735 736 } 736 - copy(w.Conjuncts, v.Conjuncts) 737 737 for i, c := range w.Conjuncts { 738 738 if v, _ := c.x.(Value); v != nil { 739 739 w.Conjuncts[i].x = toDataAll(ctx, v).(Value)
+4 -5
internal/core/adt/disjunct.go
··· 15 15 package adt 16 16 17 17 import ( 18 + "slices" 19 + 18 20 "cuelang.org/go/cue/errors" 19 21 "cuelang.org/go/cue/token" 20 22 ) ··· 505 507 case unprocessed: 506 508 a := *arc 507 509 v.Arcs[i] = &a 508 - 509 - a.Conjuncts = make([]Conjunct, len(arc.Conjuncts)) 510 - copy(a.Conjuncts, arc.Conjuncts) 510 + a.Conjuncts = slices.Clone(arc.Conjuncts) 511 511 512 512 default: 513 513 a := *arc ··· 520 520 } 521 521 522 522 if a := v.Structs; len(a) > 0 { 523 - v.Structs = make([]*StructInfo, len(a)) 524 - copy(v.Structs, a) 523 + v.Structs = slices.Clone(a) 525 524 } 526 525 527 526 return v