Skip to content

Commit

Permalink
feat: use external package for ordered maps
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Dec 30, 2024
1 parent 1bda388 commit 735a623
Show file tree
Hide file tree
Showing 24 changed files with 501 additions and 488 deletions.
2 changes: 1 addition & 1 deletion args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Parse parses command line argument: tasks and global variables
func Parse(args ...string) ([]*ast.Call, *ast.Vars) {
calls := []*ast.Call{}
globals := &ast.Vars{}
globals := ast.NewVars()

for _, arg := range args {
if !strings.Contains(arg, "=") {
Expand Down
82 changes: 48 additions & 34 deletions args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/assert"

"github.com/go-task/task/v3/args"
"github.com/go-task/task/v3/internal/omap"
"github.com/go-task/task/v3/taskfile/ast"
)

Expand All @@ -34,45 +33,55 @@ func TestArgs(t *testing.T) {
{Task: "task-b"},
{Task: "task-c"},
},
ExpectedGlobals: &ast.Vars{
OrderedMap: omap.FromMapWithOrder(
map[string]ast.Var{
"FOO": {Value: "bar"},
"BAR": {Value: "baz"},
"BAZ": {Value: "foo"},
ExpectedGlobals: ast.NewVars(
&ast.VarElement{
Key: "FOO",
Value: ast.Var{
Value: "bar",
},
[]string{"FOO", "BAR", "BAZ"},
),
},
},
&ast.VarElement{
Key: "BAR",
Value: ast.Var{
Value: "baz",
},
},
&ast.VarElement{
Key: "BAZ",
Value: ast.Var{
Value: "foo",
},
},
),
},
{
Args: []string{"task-a", "CONTENT=with some spaces"},
ExpectedCalls: []*ast.Call{
{Task: "task-a"},
},
ExpectedGlobals: &ast.Vars{
OrderedMap: omap.FromMapWithOrder(
map[string]ast.Var{
"CONTENT": {Value: "with some spaces"},
ExpectedGlobals: ast.NewVars(
&ast.VarElement{
Key: "CONTENT",
Value: ast.Var{
Value: "with some spaces",
},
[]string{"CONTENT"},
),
},
},
),
},
{
Args: []string{"FOO=bar", "task-a", "task-b"},
ExpectedCalls: []*ast.Call{
{Task: "task-a"},
{Task: "task-b"},
},
ExpectedGlobals: &ast.Vars{
OrderedMap: omap.FromMapWithOrder(
map[string]ast.Var{
"FOO": {Value: "bar"},
ExpectedGlobals: ast.NewVars(
&ast.VarElement{
Key: "FOO",
Value: ast.Var{
Value: "bar",
},
[]string{"FOO"},
),
},
},
),
},
{
Args: nil,
Expand All @@ -85,15 +94,20 @@ func TestArgs(t *testing.T) {
{
Args: []string{"FOO=bar", "BAR=baz"},
ExpectedCalls: []*ast.Call{},
ExpectedGlobals: &ast.Vars{
OrderedMap: omap.FromMapWithOrder(
map[string]ast.Var{
"FOO": {Value: "bar"},
"BAR": {Value: "baz"},
ExpectedGlobals: ast.NewVars(
&ast.VarElement{
Key: "FOO",
Value: ast.Var{
Value: "bar",
},
[]string{"FOO", "BAR"},
),
},
},
&ast.VarElement{
Key: "BAR",
Value: ast.Var{
Value: "baz",
},
},
),
},
}

Expand All @@ -104,8 +118,8 @@ func TestArgs(t *testing.T) {
calls, globals := args.Parse(test.Args...)
assert.Equal(t, test.ExpectedCalls, calls)
if test.ExpectedGlobals.Len() > 0 || globals.Len() > 0 {
assert.Equal(t, test.ExpectedGlobals.Keys(), globals.Keys())
assert.Equal(t, test.ExpectedGlobals.Values(), globals.Values())
assert.Equal(t, test.ExpectedGlobals, globals)
assert.Equal(t, test.ExpectedGlobals, globals)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/chainguard-dev/git-urls v1.0.2
github.com/davecgh/go-spew v1.1.1
github.com/dominikbraun/graph v0.23.0
github.com/elliotchance/orderedmap/v2 v2.6.0
github.com/fatih/color v1.18.0
github.com/go-git/go-billy/v5 v5.6.0
github.com/go-git/go-git/v5 v5.12.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/dominikbraun/graph v0.23.0 h1:TdZB4pPqCLFxYhdyMFb1TBdFxp8XLcJfTTBQucV
github.com/dominikbraun/graph v0.23.0/go.mod h1:yOjYyogZLY1LSG9E33JWZJiq5k83Qy2C6POAuiViluc=
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU=
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/elliotchance/orderedmap/v2 v2.6.0 h1:Zzo4k/u6hTRSt4NbYVphwOn5fBKlLpcbaV00INfJ1WI=
github.com/elliotchance/orderedmap/v2 v2.6.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// GetEnviron the all return all environment variables encapsulated on a
// ast.Vars
func GetEnviron() *ast.Vars {
m := &ast.Vars{}
m := ast.NewVars()
for _, e := range os.Environ() {
keyVal := strings.SplitN(e, "=", 2)
key, val := keyVal[0], keyVal[1]
Expand Down
17 changes: 17 additions & 0 deletions internal/deepcopy/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package deepcopy

import (
"reflect"

"github.com/elliotchance/orderedmap/v2"
)

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

func OrderedMap[K comparable, V any](orig *orderedmap.OrderedMap[K, V]) *orderedmap.OrderedMap[K, V] {
if orig.Len() == 0 {
return orderedmap.NewOrderedMap[K, V]()
}
c := orderedmap.NewOrderedMap[K, V]()
for pair := orig.Front(); pair != nil; pair = pair.Next() {
if copyable, ok := any(pair.Value).(Copier[V]); ok {
c.Set(pair.Key, copyable.DeepCopy())
} else {
c.Set(pair.Key, pair.Value)
}
}
return c
}

// TraverseStringsFunc runs the given function on every string in the given
// value by traversing it recursively. If the given value is a string, the
// function will run on a copy of the string and return it. If the value is a
Expand Down
164 changes: 0 additions & 164 deletions internal/omap/orderedmap.go

This file was deleted.

Loading

0 comments on commit 735a623

Please sign in to comment.