...

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

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

     1  package graph
     2  
     3  import (
     4  	"strings"
     5  
     6  	"edge-infra.dev/pkg/lib/text/drawing"
     7  )
     8  
     9  // Creates a tree representation of the graph from its root node
    10  func (g *graph) toTree(providerLabels bool) (*drawing.StringTree, error) {
    11  	// as nodes are traversed depth-first, the last node is the root
    12  	root := g.keys[len(g.keys)-1]
    13  
    14  	rootNode := g.nodes[root]
    15  	return rootNode.toTree(providerLabels)
    16  }
    17  
    18  // Creates a tree representation of the node and its dependencies
    19  func (n *node) toTree(providerLabels bool) (*drawing.StringTree, error) {
    20  	children := []*drawing.StringTree{}
    21  	for _, dep := range n.deps {
    22  		if dep.nodeType == objectsNode {
    23  			// split each internal object into a new tree node
    24  			objects := createNodesFromObjects(dep)
    25  			children = append(children, objects...)
    26  		} else {
    27  			child, err := dep.toTree(providerLabels)
    28  			if err != nil {
    29  				return nil, err
    30  			}
    31  			children = append(children, child)
    32  		}
    33  	}
    34  
    35  	labels := map[string]string{}
    36  	if providerLabels {
    37  		labels = n.labels
    38  	}
    39  
    40  	tree := &drawing.StringTree{
    41  		Data:     n.label(),
    42  		Children: children,
    43  		Labels:   labels,
    44  	}
    45  
    46  	return tree, nil
    47  }
    48  
    49  func createNodesFromObjects(n *node) []*drawing.StringTree {
    50  	objects := strings.Split(n.data, "\n")
    51  
    52  	objectNodes := []*drawing.StringTree{}
    53  	for _, object := range objects {
    54  		objectNodes = append(objectNodes, &drawing.StringTree{
    55  			Data: object,
    56  		})
    57  	}
    58  
    59  	return objectNodes
    60  }
    61  

View as plain text