Skip to content

Commit c43a0ea

Browse files
committed
modern go fixes
1 parent f6ae302 commit c43a0ea

11 files changed

Lines changed: 30 additions & 74 deletions

vibes/capability_contracts.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vibes
33
import (
44
"fmt"
55
"reflect"
6+
"slices"
67
)
78

89
type capabilityContractScanner struct {
@@ -77,12 +78,7 @@ func (s *capabilityContractScanner) containsCallable(val Value) bool {
7778
return false
7879
}
7980
s.seenArrays[id] = struct{}{}
80-
for _, item := range values {
81-
if s.containsCallable(item) {
82-
return true
83-
}
84-
}
85-
return false
81+
return slices.ContainsFunc(values, s.containsCallable)
8682
case KindHash, KindObject:
8783
entries := val.Hash()
8884
ptr := reflect.ValueOf(entries).Pointer()

vibes/capability_jobqueue.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vibes
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"time"
78
)
89

@@ -343,8 +344,6 @@ func mergeHash(dest map[string]Value, src map[string]Value) map[string]Value {
343344
if dest == nil {
344345
dest = make(map[string]Value, len(src))
345346
}
346-
for k, v := range src {
347-
dest[k] = v
348-
}
347+
maps.Copy(dest, src)
349348
return dest
350349
}

vibes/env.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package vibes
22

3+
import "maps"
4+
35
type Env struct {
46
parent *Env
57
values map[string]Value
@@ -39,8 +41,6 @@ func (e *Env) Assign(name string, val Value) bool {
3941

4042
func (e *Env) CloneShallow() *Env {
4143
clone := newEnv(e.parent)
42-
for k, v := range e.values {
43-
clone.values[k] = v
44-
}
44+
maps.Copy(clone.values, e.values)
4545
return clone
4646
}

vibes/examples_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vibes
22

33
import (
44
"context"
5+
"maps"
56
"os"
67
"path/filepath"
78
"strings"
@@ -1812,7 +1813,6 @@ func TestExamples(t *testing.T) {
18121813
}
18131814

18141815
for _, tc := range cases {
1815-
tc := tc
18161816
t.Run(tc.name, func(t *testing.T) {
18171817
if tc.skip {
18181818
t.Skip("example is pending implementation")
@@ -1895,17 +1895,13 @@ func arrayVal(elems ...Value) Value {
18951895

18961896
func hashVal(entries map[string]Value) Value {
18971897
cp := make(map[string]Value, len(entries))
1898-
for k, v := range entries {
1899-
cp[k] = v
1900-
}
1898+
maps.Copy(cp, entries)
19011899
return NewHash(cp)
19021900
}
19031901

19041902
func objectVal(entries map[string]Value) Value {
19051903
cp := make(map[string]Value, len(entries))
1906-
for k, v := range entries {
1907-
cp[k] = v
1908-
}
1904+
maps.Copy(cp, entries)
19091905
return NewObject(cp)
19101906
}
19111907

@@ -2009,9 +2005,7 @@ func cloneKwargs(in map[string]Value) map[string]Value {
20092005
return nil
20102006
}
20112007
out := make(map[string]Value, len(in))
2012-
for k, v := range in {
2013-
out[k] = v
2014-
}
2008+
maps.Copy(out, in)
20152009
return out
20162010
}
20172011

vibes/execution.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"maps"
78
"math"
89
"regexp"
10+
"slices"
911
"sort"
1012
"strings"
1113
"time"
@@ -1508,12 +1510,8 @@ func hashMember(obj Value, property string) (Value, error) {
15081510
base := receiver.Hash()
15091511
addition := args[0].Hash()
15101512
out := make(map[string]Value, len(base)+len(addition))
1511-
for k, v := range base {
1512-
out[k] = v
1513-
}
1514-
for k, v := range addition {
1515-
out[k] = v
1516-
}
1513+
maps.Copy(out, base)
1514+
maps.Copy(out, addition)
15171515
return NewHash(out), nil
15181516
}), nil
15191517
case "slice":
@@ -2835,13 +2833,7 @@ func arrayMember(array Value, property string) (Value, error) {
28352833
arr := receiver.Array()
28362834
unique := make([]Value, 0, len(arr))
28372835
for _, item := range arr {
2838-
found := false
2839-
for _, existing := range unique {
2840-
if item.Equal(existing) {
2841-
found = true
2842-
break
2843-
}
2844-
}
2836+
found := slices.ContainsFunc(unique, item.Equal)
28452837
if !found {
28462838
unique = append(unique, item)
28472839
}
@@ -3798,13 +3790,7 @@ func subtractValues(left, right Value) (Value, error) {
37983790
rArr := right.Array()
37993791
out := make([]Value, 0, len(lArr))
38003792
for _, item := range lArr {
3801-
found := false
3802-
for _, remove := range rArr {
3803-
if item.Equal(remove) {
3804-
found = true
3805-
break
3806-
}
3807-
}
3793+
found := slices.ContainsFunc(rArr, item.Equal)
38083794
if !found {
38093795
out = append(out, item)
38103796
}

vibes/integration_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func TestComplexExamplesCompile(t *testing.T) {
5858
"tests/complex/chudnovsky.vibe",
5959
}
6060
for _, path := range files {
61-
path := path
6261
t.Run(filepath.Base(path), func(t *testing.T) {
6362
full := filepath.Join("..", path)
6463
data, err := os.ReadFile(full)
@@ -262,7 +261,6 @@ func TestComplexExamplesRun(t *testing.T) {
262261
}
263262

264263
for _, tc := range cases {
265-
tc := tc
266264
t.Run(tc.name, func(t *testing.T) {
267265
script := compileComplexExample(t, tc.file)
268266
opts := CallOptions{}
@@ -421,7 +419,6 @@ func TestProgramFixtures(t *testing.T) {
421419
}
422420

423421
for _, tc := range cases {
424-
tc := tc
425422
t.Run(tc.name, func(t *testing.T) {
426423
script := compileTestProgram(t, tc.file)
427424
result, err := script.Call(context.Background(), tc.function, nil, CallOptions{})
@@ -492,7 +489,7 @@ func TestComplexExamplesStress(t *testing.T) {
492489
}
493490

494491
piScript := compileComplexExampleWithConfig(t, "chudnovsky.vibe", highQuota)
495-
for i := 0; i < 50; i++ {
492+
for i := range 50 {
496493
val, err := piScript.Call(context.Background(), "pi_approx_precise", []Value{intVal(5_000)}, CallOptions{})
497494
if err != nil {
498495
t.Fatalf("pi_approx_precise run %d failed: %v", i, err)
@@ -529,7 +526,6 @@ func TestAllVibeFilesCompileAndRun(t *testing.T) {
529526

530527
engine := MustNewEngine(Config{StepQuota: 5_000_000})
531528
for _, path := range files {
532-
path := path
533529
rel, _ := filepath.Rel(testsDir, path)
534530
t.Run(rel, func(t *testing.T) {
535531
source, err := os.ReadFile(path)
@@ -731,7 +727,7 @@ end
731727
t.Fatalf("compile: %v", err)
732728
}
733729

734-
for i := 0; i < 100; i++ {
730+
for i := range 100 {
735731
result, err := script.Call(context.Background(), "run", nil, CallOptions{})
736732
if err != nil {
737733
t.Fatalf("run %d: %v", i, err)
@@ -762,7 +758,7 @@ end
762758
t.Fatalf("compile: %v", err)
763759
}
764760

765-
for i := 0; i < 50; i++ {
761+
for i := range 50 {
766762
result, err := script.Call(context.Background(), "run", nil, CallOptions{})
767763
if err != nil {
768764
t.Fatalf("run %d: %v", i, err)

vibes/interpreter.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vibes
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"os"
78
"strings"
89
"sync"
@@ -285,9 +286,7 @@ func (e *Engine) RegisterZeroArgBuiltin(name string, fn BuiltinFunc) {
285286
// Builtins returns a copy of the registered builtin map.
286287
func (e *Engine) Builtins() map[string]Value {
287288
out := make(map[string]Value, len(e.builtins))
288-
for k, v := range e.builtins {
289-
out[k] = v
290-
}
289+
maps.Copy(out, e.builtins)
291290
return out
292291
}
293292

vibes/memory_quota_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,7 @@ func TestAggregateBuiltinArgumentsAreChecked(t *testing.T) {
578578

579579
argA := newMemoryEstimator().value(NewString(payloadA))
580580
argB := newMemoryEstimator().value(NewString(payloadB))
581-
single := argA
582-
if argB > single {
583-
single = argB
584-
}
581+
single := max(argB, argA)
585582
combined := argA + argB
586583
quota := base + single + (combined-single)/2
587584
if quota <= base+single {
@@ -891,10 +888,7 @@ func TestAggregateYieldArgumentsAreChecked(t *testing.T) {
891888

892889
argA := newMemoryEstimator().value(NewString(payloadA))
893890
argB := newMemoryEstimator().value(NewString(payloadB))
894-
single := argA
895-
if argB > single {
896-
single = argB
897-
}
891+
single := max(argB, argA)
898892
combined := argA + argB
899893
quota := base + single + (combined-single)/2
900894
if quota <= base+single {

vibes/modules.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/fs"
77
"os"
88
"path/filepath"
9+
"slices"
910
"strings"
1011
)
1112

@@ -60,12 +61,7 @@ func isExplicitRelativeModulePath(name string) bool {
6061

6162
func containsPathTraversal(cleanPath string) bool {
6263
sep := string(filepath.Separator)
63-
for _, part := range strings.Split(cleanPath, sep) {
64-
if part == ".." {
65-
return true
66-
}
67-
}
68-
return false
64+
return slices.Contains(strings.Split(cleanPath, sep), "..")
6965
}
7066

7167
func moduleCacheKey(root, relative string) string {

vibes/modules_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ end`)
497497
const goroutines = 10
498498
results := make(chan error, goroutines)
499499

500-
for i := 0; i < goroutines; i++ {
500+
for range goroutines {
501501
go func() {
502502
result, err := script.Call(context.Background(), "run", nil, CallOptions{})
503503
if err != nil {
@@ -512,7 +512,7 @@ end`)
512512
}()
513513
}
514514

515-
for i := 0; i < goroutines; i++ {
515+
for range goroutines {
516516
if err := <-results; err != nil {
517517
t.Fatalf("concurrent call failed: %v", err)
518518
}

0 commit comments

Comments
 (0)