forked from okteto/okteto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Init dag package * Add copyright headers * linting * more linting
- Loading branch information
Showing
5 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024 The Okteto Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dag | ||
|
||
import ( | ||
"github.com/heimdalr/dag" | ||
) | ||
|
||
type Node interface { | ||
ID() string | ||
DependsOn() []string | ||
} | ||
|
||
type callback func(Node) | ||
|
||
func (cb callback) Visit(vx dag.Vertexer) { | ||
_, value := vx.Vertex() | ||
cb(value.(Node)) | ||
} | ||
|
||
type Tree struct { | ||
graph *dag.DAG | ||
} | ||
|
||
func (tree *Tree) Traverse(fn func(n Node)) { | ||
tree.graph.OrderedWalk(callback(fn)) | ||
} | ||
|
||
func From(nodes ...Node) (*Tree, error) { | ||
tree := &Tree{graph: dag.NewDAG()} | ||
|
||
for _, n := range nodes { | ||
if _, err := tree.graph.AddVertex(n); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
for _, n := range nodes { | ||
for _, dep := range n.DependsOn() { | ||
if err := tree.graph.AddEdge(dep, n.ID()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
return tree, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Copyright 2024 The Okteto Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dag | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testNode struct { | ||
id string | ||
dependsOn []string | ||
} | ||
|
||
func (n *testNode) ID() string { return n.id } | ||
func (n *testNode) DependsOn() []string { return n.dependsOn } | ||
|
||
func TestTraverseError(t *testing.T) { | ||
nodes := []Node{ | ||
&testNode{id: "v1", dependsOn: []string{"v2"}}, | ||
&testNode{id: "v2", dependsOn: []string{"v3"}}, | ||
&testNode{id: "v3", dependsOn: []string{"v1"}}, | ||
} | ||
_, err := From(nodes...) | ||
require.EqualError(t, err, "edge between 'v1' and 'v3' would create a loop") | ||
|
||
nodes = []Node{ | ||
&testNode{id: "v1", dependsOn: []string{"v2"}}, | ||
&testNode{id: "v2"}, | ||
&testNode{id: "v3"}, | ||
&testNode{id: "v1", dependsOn: []string{"v3"}}, | ||
} | ||
_, err = From(nodes...) | ||
require.EqualError(t, err, "the id 'v1' is already known") | ||
|
||
nodes = []Node{ | ||
&testNode{id: "v1", dependsOn: []string{"v2"}}, | ||
&testNode{id: "v2"}, | ||
&testNode{id: "v3", dependsOn: []string{"v3"}}, | ||
} | ||
_, err = From(nodes...) | ||
require.EqualError(t, err, "src ('v3') and dst ('v3') equal") | ||
|
||
} | ||
func TestTraverse(t *testing.T) { | ||
|
||
var tt = []struct { | ||
name string | ||
nodes []Node | ||
expected []string | ||
}{ | ||
{ | ||
// v1 | ||
// ^ | ||
// | | ||
// v2 | ||
// ^ | ||
// | | ||
// v3 | ||
// ^ | ||
// | | ||
// v4 | ||
// ^ | ||
// | | ||
// v5 | ||
name: "linear", | ||
nodes: []Node{ | ||
&testNode{id: "v1"}, | ||
&testNode{id: "v2", dependsOn: []string{"v1"}}, | ||
&testNode{id: "v3", dependsOn: []string{"v2"}}, | ||
&testNode{id: "v4", dependsOn: []string{"v3"}}, | ||
&testNode{id: "v5", dependsOn: []string{"v4"}}, | ||
}, | ||
expected: []string{"v1", "v2", "v3", "v4", "v5"}, | ||
}, | ||
{ | ||
// v5 --> v4 | ||
// | ||
// | ||
// v2 --> v1 | ||
// ^ | ||
// | | ||
// v3 | ||
name: "sparse-two-roots", | ||
nodes: []Node{ | ||
&testNode{id: "v1"}, | ||
&testNode{id: "v2", dependsOn: []string{"v1"}}, | ||
&testNode{id: "v3", dependsOn: []string{"v1"}}, | ||
&testNode{id: "v4"}, | ||
&testNode{id: "v5", dependsOn: []string{"v4"}}, | ||
}, | ||
expected: []string{"v1", "v4", "v2", "v3", "v5"}, | ||
}, | ||
{ | ||
// v1 v2 | ||
// ^ ^ | ||
// | | | ||
// v4 --> v3 | ||
// ^ | ||
// | | ||
// v5 | ||
name: "fork", | ||
nodes: []Node{ | ||
&testNode{id: "v1"}, | ||
&testNode{id: "v2"}, | ||
&testNode{id: "v3", dependsOn: []string{"v2"}}, | ||
&testNode{id: "v4", dependsOn: []string{"v1"}}, | ||
&testNode{id: "v5", dependsOn: []string{"v4"}}, | ||
}, | ||
expected: []string{"v1", "v2", "v4", "v3", "v5"}, | ||
}, | ||
{ | ||
// v1 v3 <---┐ | ||
// ^ | | ||
// | | | ||
// v2 <-- v5 --> v4 | ||
name: "edgy", | ||
nodes: []Node{ | ||
&testNode{id: "v1"}, | ||
&testNode{id: "v2"}, | ||
&testNode{id: "v3"}, | ||
&testNode{id: "v4", dependsOn: []string{"v3"}}, | ||
&testNode{id: "v5", dependsOn: []string{"v4", "v3", "v2"}}, | ||
}, | ||
expected: []string{"v1", "v2", "v3", "v4", "v5"}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tree, err := From(tc.nodes...) | ||
require.NoError(t, err) | ||
|
||
var result []string | ||
tree.Traverse(func(n Node) { | ||
result = append(result, n.ID()) | ||
}) | ||
assert.Equal(t, tc.expected, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 The Okteto Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// package dag provides generic interface for working with dependencies resolution | ||
// via Directed Acyclic Graphs (DAGs) | ||
// | ||
// It is required for clients of this package to implement the Node interface | ||
// which consists of two methods: ID() and DependsOn(): | ||
// - ID: It's the ID of the node and how it will be identified in the tree | ||
// - DependsOn: It's the dependencies of this node and should come BEFORE this node | ||
// | ||
// IDs should be unique and there should not be any cycles. An example of a cycle | ||
// is: A->B, B->C, C->A | ||
// | ||
// Usage: | ||
// ```go | ||
// | ||
// // testNode is an custom implementation for resolving dependencies | ||
// type testNode struct { | ||
// id string | ||
// dependsOn []string | ||
// } | ||
// | ||
// func (n *testNode) ID() string { return n.id } | ||
// func (n *testNode) DependsOn() []string { return n.dependsOn } | ||
// | ||
// nodes := []dag.Node{ | ||
// &testNode{id: "v1"}, | ||
// &testNode{id: "v2", dependsOn: []string{"v1"}}, | ||
// &testNode{id: "v3", dependsOn: []string{"v2"}}, | ||
// &testNode{id: "v4", dependsOn: []string{"v3"}}, | ||
// &testNode{id: "v5", dependsOn: []string{"v4"}}, | ||
// } | ||
// result := []string{} | ||
// tree, _ := dag.From(nodes...) | ||
// tree.Traverse(func(n dag.Node) { | ||
// result = append(result, n.ID()) | ||
// }) | ||
// fmt.Println(strings.Join(result, ",")) // v1,v2,v3,v4,v5 | ||
// | ||
// ``` | ||
// | ||
// Traverse() takes care of walking the DAG and calling the callback in order | ||
// based on the DependsOn() | ||
package dag |