...

Source file src/edge-infra.dev/pkg/f8n/warehouse/lift/cmd/graph/dot.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/lift/cmd/graph

     1  package graph
     2  
     3  import (
     4  	"errors"
     5  
     6  	"edge-infra.dev/pkg/lib/text/drawing"
     7  )
     8  
     9  var errUnsupportedDotNodeType = errors.New("unsupported type for dot node")
    10  
    11  // Creates a dot graph representation of the graph from its root node
    12  func (g *graph) toDot() (*drawing.Digraph, error) {
    13  	// as nodes are traversed depth-first, the last node is the root
    14  	root := g.keys[len(g.keys)-1]
    15  	rootNode := g.nodes[root]
    16  
    17  	rootDotNode, dotNodes, err := rootNode.toDot()
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	dt := &drawing.DotTree{
    23  		Root:  rootDotNode,
    24  		Nodes: dotNodes,
    25  		Attributes: map[string]string{
    26  			"rankdir": "TB",
    27  		},
    28  	}
    29  
    30  	return &drawing.Digraph{
    31  		Title: rootNode.name,
    32  		Subgraphs: []drawing.Subgraph{
    33  			&palletLegend{},
    34  			dt,
    35  		},
    36  	}, nil
    37  }
    38  
    39  // Creates a dot representation of the node and its dependencies, returning the root node and a map of all nodes
    40  func (n *node) toDot() (rootNode *drawing.DotNode, nodes map[string]*drawing.DotNode, err error) {
    41  	children := []*drawing.DotNode{}
    42  	for _, dep := range n.deps {
    43  		var child *drawing.DotNode
    44  		child, nodes, err = dep.toDot()
    45  		if err != nil {
    46  			return nil, nil, err
    47  		}
    48  		children = append(children, child)
    49  	}
    50  
    51  	rootNode, err = dotNodeByType(n.label(), n.nodeType)
    52  	if err != nil {
    53  		return nil, nil, err
    54  	}
    55  
    56  	rootNode.Children = children
    57  	if n.nodeType == objectsNode {
    58  		rootNode.Data = n.data
    59  	}
    60  
    61  	if nodes == nil {
    62  		nodes = map[string]*drawing.DotNode{}
    63  	}
    64  	nodes[n.digest.String()] = rootNode
    65  
    66  	return rootNode, nodes, err
    67  }
    68  

View as plain text