-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Azure/haitao/new-dag-lib
drop terraform dag lib
- Loading branch information
Showing
7 changed files
with
120 additions
and
806 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,80 @@ | ||
package graph | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type GraphErrorCode string | ||
|
||
const ( | ||
ErrDuplicateNode GraphErrorCode = "node with same key already exists in this graph" | ||
ErrConnectNotExistingNode GraphErrorCode = "node to connect does not exist in this graph" | ||
) | ||
|
||
func (ge GraphErrorCode) Error() string { | ||
return string(ge) | ||
} | ||
|
||
type NodeConstrain interface { | ||
// Name of the node, used as key in the graph, so should be unique. | ||
GetName() string | ||
} | ||
|
||
type Edge[NT NodeConstrain] struct { | ||
From NT | ||
To NT | ||
} | ||
|
||
type Graph[NT NodeConstrain] struct { | ||
nodes map[string]NT | ||
nodeEdges map[string][]*Edge[NT] | ||
} | ||
|
||
func NewGraph[NT NodeConstrain]() *Graph[NT] { | ||
return &Graph[NT]{ | ||
nodes: make(map[string]NT), | ||
nodeEdges: make(map[string][]*Edge[NT]), | ||
} | ||
} | ||
|
||
func (g *Graph[NT]) AddNode(n NT) error { | ||
nodeKey := n.GetName() | ||
if _, ok := g.nodes[nodeKey]; ok { | ||
return ErrDuplicateNode | ||
} | ||
g.nodes[nodeKey] = n | ||
|
||
return nil | ||
} | ||
|
||
func (g *Graph[NT]) Connect(from, to string) error { | ||
var nodeFrom, nodeTo NT | ||
var ok bool | ||
if nodeFrom, ok = g.nodes[from]; !ok { | ||
return ErrConnectNotExistingNode | ||
} | ||
|
||
if nodeTo, ok = g.nodes[to]; !ok { | ||
return ErrConnectNotExistingNode | ||
} | ||
|
||
g.nodeEdges[from] = append(g.nodeEdges[from], &Edge[NT]{From: nodeFrom, To: nodeTo}) | ||
return nil | ||
} | ||
|
||
// https://en.wikipedia.org/wiki/DOT_(graph_description_language) | ||
func (g *Graph[NT]) ToDotGraph() (string, error) { | ||
edges := make(map[string][]string) | ||
for _, nodeEdges := range g.nodeEdges { | ||
for _, edge := range nodeEdges { | ||
edges[edge.From.GetName()] = append(edges[edge.From.GetName()], edge.To.GetName()) | ||
} | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
err := digraphTemplate.Execute(buf, edges) | ||
if err != nil { | ||
return "", err | ||
} | ||
return buf.String(), 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,15 @@ | ||
package graph | ||
|
||
import ( | ||
"text/template" | ||
) | ||
|
||
var digraphTemplate = template.Must(template.New("digraph").Parse(digraphTemplateText)) | ||
|
||
const digraphTemplateText = `digraph { | ||
compound = "true" | ||
newrank = "true" | ||
subgraph "root" { | ||
{{ range $from, $toList := $}}{{ range $_, $to := $toList}} "{{$from}}" -> "{{$to}}" | ||
{{ end }}{{ end }} } | ||
}` |
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