2016-11-09 15:58:52 +01:00
|
|
|
package dag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2016-11-15 16:15:52 +01:00
|
|
|
)
|
|
|
|
|
2016-11-09 15:58:52 +01:00
|
|
|
// the marshal* structs are for serialization of the graph data.
|
|
|
|
type marshalGraph struct {
|
2016-11-10 19:40:27 +01:00
|
|
|
// Type is always "Graph", for identification as a top level object in the
|
|
|
|
// JSON stream.
|
|
|
|
Type string
|
|
|
|
|
2016-11-10 15:28:42 +01:00
|
|
|
// Each marshal structure requires a unique ID so that it can be referenced
|
2016-11-10 00:47:46 +01:00
|
|
|
// by other structures.
|
|
|
|
ID string `json:",omitempty"`
|
|
|
|
|
|
|
|
// Human readable name for this graph.
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
|
|
|
|
// Arbitrary attributes that can be added to the output.
|
|
|
|
Attrs map[string]string `json:",omitempty"`
|
|
|
|
|
|
|
|
// List of graph vertices, sorted by ID.
|
|
|
|
Vertices []*marshalVertex `json:",omitempty"`
|
|
|
|
|
|
|
|
// List of edges, sorted by Source ID.
|
|
|
|
Edges []*marshalEdge `json:",omitempty"`
|
|
|
|
|
|
|
|
// Any number of subgraphs. A subgraph itself is considered a vertex, and
|
|
|
|
// may be referenced by either end of an edge.
|
|
|
|
Subgraphs []*marshalGraph `json:",omitempty"`
|
|
|
|
|
|
|
|
// Any lists of vertices that are included in cycles.
|
|
|
|
Cycles [][]*marshalVertex `json:",omitempty"`
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *marshalGraph) vertexByID(id string) *marshalVertex {
|
|
|
|
for _, v := range g.Vertices {
|
|
|
|
if id == v.ID {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type marshalVertex struct {
|
2016-11-10 00:47:46 +01:00
|
|
|
// Unique ID, used to reference this vertex from other structures.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Human readable name
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
|
2016-11-09 15:58:52 +01:00
|
|
|
Attrs map[string]string `json:",omitempty"`
|
2016-11-10 00:47:46 +01:00
|
|
|
|
|
|
|
// This is to help transition from the old Dot interfaces. We record if the
|
2016-12-04 00:10:26 +01:00
|
|
|
// node was a GraphNodeDotter here, so we can call it to get attributes.
|
|
|
|
graphNodeDotter GraphNodeDotter
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
func newMarshalVertex(v Vertex) *marshalVertex {
|
2016-12-04 00:10:26 +01:00
|
|
|
dn, ok := v.(GraphNodeDotter)
|
|
|
|
if !ok {
|
|
|
|
dn = nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 19:23:12 +02:00
|
|
|
// the name will be quoted again later, so we need to ensure it's properly
|
|
|
|
// escaped without quotes.
|
|
|
|
name := strconv.Quote(VertexName(v))
|
|
|
|
name = name[1 : len(name)-1]
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
return &marshalVertex{
|
|
|
|
ID: marshalVertexID(v),
|
2020-06-15 19:23:12 +02:00
|
|
|
Name: name,
|
2016-11-10 19:40:27 +01:00
|
|
|
Attrs: make(map[string]string),
|
2016-12-04 00:10:26 +01:00
|
|
|
graphNodeDotter: dn,
|
2016-11-10 19:40:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:47:46 +01:00
|
|
|
// vertices is a sort.Interface implementation for sorting vertices by ID
|
2016-11-09 15:58:52 +01:00
|
|
|
type vertices []*marshalVertex
|
|
|
|
|
|
|
|
func (v vertices) Less(i, j int) bool { return v[i].Name < v[j].Name }
|
|
|
|
func (v vertices) Len() int { return len(v) }
|
|
|
|
func (v vertices) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
|
|
|
|
|
|
|
|
type marshalEdge struct {
|
2016-11-10 00:47:46 +01:00
|
|
|
// Human readable name
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Source and Target Vertices by ID
|
2016-11-09 15:58:52 +01:00
|
|
|
Source string
|
|
|
|
Target string
|
2016-11-10 00:47:46 +01:00
|
|
|
|
|
|
|
Attrs map[string]string `json:",omitempty"`
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
func newMarshalEdge(e Edge) *marshalEdge {
|
|
|
|
return &marshalEdge{
|
|
|
|
Name: fmt.Sprintf("%s|%s", VertexName(e.Source()), VertexName(e.Target())),
|
|
|
|
Source: marshalVertexID(e.Source()),
|
|
|
|
Target: marshalVertexID(e.Target()),
|
|
|
|
Attrs: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:47:46 +01:00
|
|
|
// edges is a sort.Interface implementation for sorting edges by Source ID
|
2016-11-09 15:58:52 +01:00
|
|
|
type edges []*marshalEdge
|
|
|
|
|
|
|
|
func (e edges) Less(i, j int) bool { return e[i].Name < e[j].Name }
|
|
|
|
func (e edges) Len() int { return len(e) }
|
|
|
|
func (e edges) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
|
|
|
|
|
2016-11-10 00:47:46 +01:00
|
|
|
// build a marshalGraph structure from a *Graph
|
2016-11-09 15:58:52 +01:00
|
|
|
func newMarshalGraph(name string, g *Graph) *marshalGraph {
|
2016-11-10 19:40:27 +01:00
|
|
|
mg := &marshalGraph{
|
|
|
|
Type: "Graph",
|
2016-11-09 15:58:52 +01:00
|
|
|
Name: name,
|
|
|
|
Attrs: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
id := marshalVertexID(v)
|
2016-11-09 22:52:22 +01:00
|
|
|
if sg, ok := marshalSubgrapher(v); ok {
|
2016-11-10 19:40:27 +01:00
|
|
|
smg := newMarshalGraph(VertexName(v), sg)
|
|
|
|
smg.ID = id
|
|
|
|
mg.Subgraphs = append(mg.Subgraphs, smg)
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
mv := newMarshalVertex(v)
|
|
|
|
mg.Vertices = append(mg.Vertices, mv)
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
sort.Sort(vertices(mg.Vertices))
|
2016-11-09 15:58:52 +01:00
|
|
|
|
|
|
|
for _, e := range g.Edges() {
|
2016-11-10 19:40:27 +01:00
|
|
|
mg.Edges = append(mg.Edges, newMarshalEdge(e))
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
sort.Sort(edges(mg.Edges))
|
2016-11-09 15:58:52 +01:00
|
|
|
|
|
|
|
for _, c := range (&AcyclicGraph{*g}).Cycles() {
|
|
|
|
var cycle []*marshalVertex
|
|
|
|
for _, v := range c {
|
2016-11-10 19:40:27 +01:00
|
|
|
mv := newMarshalVertex(v)
|
|
|
|
cycle = append(cycle, mv)
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
2016-11-10 19:40:27 +01:00
|
|
|
mg.Cycles = append(mg.Cycles, cycle)
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
return mg
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to return a unique ID for any vertex.
|
|
|
|
func marshalVertexID(v Vertex) string {
|
|
|
|
val := reflect.ValueOf(v)
|
|
|
|
switch val.Kind() {
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
|
|
|
|
return strconv.Itoa(int(val.Pointer()))
|
|
|
|
case reflect.Interface:
|
|
|
|
return strconv.Itoa(int(val.InterfaceData()[1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := v.(Hashable); ok {
|
|
|
|
h := v.Hashcode()
|
|
|
|
if h, ok := h.(string); ok {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:47:46 +01:00
|
|
|
// fallback to a name, which we hope is unique.
|
|
|
|
return VertexName(v)
|
|
|
|
|
2016-11-09 15:58:52 +01:00
|
|
|
// we could try harder by attempting to read the arbitrary value from the
|
|
|
|
// interface, but we shouldn't get here from terraform right now.
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:52:22 +01:00
|
|
|
// check for a Subgrapher, and return the underlying *Graph.
|
|
|
|
func marshalSubgrapher(v Vertex) (*Graph, bool) {
|
|
|
|
sg, ok := v.(Subgrapher)
|
2016-11-09 15:58:52 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:52:22 +01:00
|
|
|
switch g := sg.Subgraph().DirectedGraph().(type) {
|
|
|
|
case *Graph:
|
|
|
|
return g, true
|
|
|
|
case *AcyclicGraph:
|
|
|
|
return &g.Graph, true
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 22:52:22 +01:00
|
|
|
return nil, false
|
2016-11-09 15:58:52 +01:00
|
|
|
}
|