feat: add basic map for array

上级 0b61dc89
package tequila package tequila
import ( import (
"fmt"
"github.com/awalterschulze/gographviz" "github.com/awalterschulze/gographviz"
"sort" "sort"
"strconv" "strconv"
...@@ -141,24 +140,41 @@ func (fullGraph *FullGraph) ToDot(split string, include func(string) bool) *gogr ...@@ -141,24 +140,41 @@ func (fullGraph *FullGraph) ToDot(split string, include func(string) bool) *gogr
return graph return graph
} }
func (fullGraph *FullGraph) ToMapDot(split string, include func(key string) bool) { type GraphNode struct {
text string
children []*GraphNode
}
func (fullGraph *FullGraph) ToMapDot(split string, include func(key string) bool) *GraphNode {
graph := gographviz.NewGraph() graph := gographviz.NewGraph()
_ = graph.SetName("G") _ = graph.SetName("G")
//nodeIndex := 1 graphNode := &GraphNode{}
//layerIndex := 1
//nodes := make(map[string]string)
layerMap := make(map[string][]string)
for nodeKey := range fullGraph.NodeList { for nodeKey := range fullGraph.NodeList {
tmp := strings.Split(nodeKey, split) tmp := strings.Split(nodeKey, split)
packageName := tmp[0] graphNode.text = tmp[0]
if _, ok := layerMap[packageName]; !ok { graphNode = buildNode(tmp[1:], graphNode)
layerMap[packageName] = make([]string, 0)
} }
layerMap[packageName] = append(layerMap[packageName], nodeKey)
return graphNode
}
func buildNode(arr []string, node *GraphNode) *GraphNode {
if node.text == arr[0] {
return node
}
child := &GraphNode{}
if len(arr) == 1 {
child.text = arr[0]
node.children = append(node.children, child)
} else {
child.text = arr[0]
graphNode := buildNode(arr[1:], child)
node.children = append(node.children, graphNode)
} }
fmt.Println(layerMap) return node
} }
...@@ -28,7 +28,11 @@ func Test_VisualDemo(t *testing.T) { ...@@ -28,7 +28,11 @@ func Test_VisualDemo(t *testing.T) {
return true return true
} }
fullGraph.ToMapDot(".", nodeFilter) node := fullGraph.ToMapDot(".", nodeFilter)
g.Expect(true).To(Equal(true)) g.Expect(node.text).To(Equal("com"))
children := node.children
g.Expect(len(children)).To(Equal(2))
g.Expect(children[0].text).To(Equal("phodal"))
g.Expect(children[1].text).To(Equal("spring"))
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册