Skip to content

Commit 2965841

Browse files
authored
feat: use external package for ordered maps (#1797)
1 parent dbe6e41 commit 2965841

File tree

24 files changed

+501
-488
lines changed

24 files changed

+501
-488
lines changed

args/args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// Parse parses command line argument: tasks and global variables
1010
func Parse(args ...string) ([]*ast.Call, *ast.Vars) {
1111
calls := []*ast.Call{}
12-
globals := &ast.Vars{}
12+
globals := ast.NewVars()
1313

1414
for _, arg := range args {
1515
if !strings.Contains(arg, "=") {

args/args_test.go

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/stretchr/testify/assert"
88

99
"github.com/go-task/task/v3/args"
10-
"github.com/go-task/task/v3/internal/omap"
1110
"github.com/go-task/task/v3/taskfile/ast"
1211
)
1312

@@ -34,45 +33,55 @@ func TestArgs(t *testing.T) {
3433
{Task: "task-b"},
3534
{Task: "task-c"},
3635
},
37-
ExpectedGlobals: &ast.Vars{
38-
OrderedMap: omap.FromMapWithOrder(
39-
map[string]ast.Var{
40-
"FOO": {Value: "bar"},
41-
"BAR": {Value: "baz"},
42-
"BAZ": {Value: "foo"},
36+
ExpectedGlobals: ast.NewVars(
37+
&ast.VarElement{
38+
Key: "FOO",
39+
Value: ast.Var{
40+
Value: "bar",
4341
},
44-
[]string{"FOO", "BAR", "BAZ"},
45-
),
46-
},
42+
},
43+
&ast.VarElement{
44+
Key: "BAR",
45+
Value: ast.Var{
46+
Value: "baz",
47+
},
48+
},
49+
&ast.VarElement{
50+
Key: "BAZ",
51+
Value: ast.Var{
52+
Value: "foo",
53+
},
54+
},
55+
),
4756
},
4857
{
4958
Args: []string{"task-a", "CONTENT=with some spaces"},
5059
ExpectedCalls: []*ast.Call{
5160
{Task: "task-a"},
5261
},
53-
ExpectedGlobals: &ast.Vars{
54-
OrderedMap: omap.FromMapWithOrder(
55-
map[string]ast.Var{
56-
"CONTENT": {Value: "with some spaces"},
62+
ExpectedGlobals: ast.NewVars(
63+
&ast.VarElement{
64+
Key: "CONTENT",
65+
Value: ast.Var{
66+
Value: "with some spaces",
5767
},
58-
[]string{"CONTENT"},
59-
),
60-
},
68+
},
69+
),
6170
},
6271
{
6372
Args: []string{"FOO=bar", "task-a", "task-b"},
6473
ExpectedCalls: []*ast.Call{
6574
{Task: "task-a"},
6675
{Task: "task-b"},
6776
},
68-
ExpectedGlobals: &ast.Vars{
69-
OrderedMap: omap.FromMapWithOrder(
70-
map[string]ast.Var{
71-
"FOO": {Value: "bar"},
77+
ExpectedGlobals: ast.NewVars(
78+
&ast.VarElement{
79+
Key: "FOO",
80+
Value: ast.Var{
81+
Value: "bar",
7282
},
73-
[]string{"FOO"},
74-
),
75-
},
83+
},
84+
),
7685
},
7786
{
7887
Args: nil,
@@ -85,15 +94,20 @@ func TestArgs(t *testing.T) {
8594
{
8695
Args: []string{"FOO=bar", "BAR=baz"},
8796
ExpectedCalls: []*ast.Call{},
88-
ExpectedGlobals: &ast.Vars{
89-
OrderedMap: omap.FromMapWithOrder(
90-
map[string]ast.Var{
91-
"FOO": {Value: "bar"},
92-
"BAR": {Value: "baz"},
97+
ExpectedGlobals: ast.NewVars(
98+
&ast.VarElement{
99+
Key: "FOO",
100+
Value: ast.Var{
101+
Value: "bar",
93102
},
94-
[]string{"FOO", "BAR"},
95-
),
96-
},
103+
},
104+
&ast.VarElement{
105+
Key: "BAR",
106+
Value: ast.Var{
107+
Value: "baz",
108+
},
109+
},
110+
),
97111
},
98112
}
99113

@@ -104,8 +118,8 @@ func TestArgs(t *testing.T) {
104118
calls, globals := args.Parse(test.Args...)
105119
assert.Equal(t, test.ExpectedCalls, calls)
106120
if test.ExpectedGlobals.Len() > 0 || globals.Len() > 0 {
107-
assert.Equal(t, test.ExpectedGlobals.Keys(), globals.Keys())
108-
assert.Equal(t, test.ExpectedGlobals.Values(), globals.Values())
121+
assert.Equal(t, test.ExpectedGlobals, globals)
122+
assert.Equal(t, test.ExpectedGlobals, globals)
109123
}
110124
})
111125
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/chainguard-dev/git-urls v1.0.2
1010
github.com/davecgh/go-spew v1.1.1
1111
github.com/dominikbraun/graph v0.23.0
12+
github.com/elliotchance/orderedmap/v2 v2.6.0
1213
github.com/fatih/color v1.18.0
1314
github.com/go-git/go-billy/v5 v5.6.0
1415
github.com/go-git/go-git/v5 v5.12.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ github.com/dominikbraun/graph v0.23.0 h1:TdZB4pPqCLFxYhdyMFb1TBdFxp8XLcJfTTBQucV
3838
github.com/dominikbraun/graph v0.23.0/go.mod h1:yOjYyogZLY1LSG9E33JWZJiq5k83Qy2C6POAuiViluc=
3939
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU=
4040
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
41+
github.com/elliotchance/orderedmap/v2 v2.6.0 h1:Zzo4k/u6hTRSt4NbYVphwOn5fBKlLpcbaV00INfJ1WI=
42+
github.com/elliotchance/orderedmap/v2 v2.6.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
4143
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
4244
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
4345
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=

internal/compiler/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// GetEnviron the all return all environment variables encapsulated on a
1111
// ast.Vars
1212
func GetEnviron() *ast.Vars {
13-
m := &ast.Vars{}
13+
m := ast.NewVars()
1414
for _, e := range os.Environ() {
1515
keyVal := strings.SplitN(e, "=", 2)
1616
key, val := keyVal[0], keyVal[1]

internal/deepcopy/deepcopy.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package deepcopy
22

33
import (
44
"reflect"
5+
6+
"github.com/elliotchance/orderedmap/v2"
57
)
68

79
type Copier[T any] interface {
@@ -38,6 +40,21 @@ func Map[K comparable, V any](orig map[K]V) map[K]V {
3840
return c
3941
}
4042

43+
func OrderedMap[K comparable, V any](orig *orderedmap.OrderedMap[K, V]) *orderedmap.OrderedMap[K, V] {
44+
if orig.Len() == 0 {
45+
return orderedmap.NewOrderedMap[K, V]()
46+
}
47+
c := orderedmap.NewOrderedMap[K, V]()
48+
for pair := orig.Front(); pair != nil; pair = pair.Next() {
49+
if copyable, ok := any(pair.Value).(Copier[V]); ok {
50+
c.Set(pair.Key, copyable.DeepCopy())
51+
} else {
52+
c.Set(pair.Key, pair.Value)
53+
}
54+
}
55+
return c
56+
}
57+
4158
// TraverseStringsFunc runs the given function on every string in the given
4259
// value by traversing it recursively. If the given value is a string, the
4360
// function will run on a copy of the string and return it. If the value is a

internal/omap/orderedmap.go

Lines changed: 0 additions & 164 deletions
This file was deleted.

0 commit comments

Comments
 (0)