github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/graph/marshall/state.go (about)

     1  package marshall
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/15mga/kiwi/graph"
     7  	"github.com/15mga/kiwi/util"
     8  )
     9  
    10  type State struct {
    11  }
    12  
    13  func (s *State) Marshall(grp graph.IGraph) []byte {
    14  	var buff util.ByteBuffer
    15  	buff.InitCap(512)
    16  	buff.WStringNoLen("\n``` mermaid")
    17  	buff.WStringNoLen("\nstateDiagram-v2")
    18  	s.marshall(grp, "", &buff)
    19  	buff.WStringNoLen("\n```")
    20  	return buff.All()
    21  }
    22  
    23  func (s *State) marshall(grp graph.IGraph, prefix string, buff *util.ByteBuffer) {
    24  	grp.IterNode(func(nd graph.INode) {
    25  		buff.WStringNoLen(fmt.Sprintf("\n%s  %s[%s]",
    26  			prefix, nd.Name(), nd.Comment()))
    27  	})
    28  
    29  	sp := prefix + "  "
    30  	grp.IterSubGraph(func(sg graph.ISubGraph) {
    31  		buff.WStringNoLen("\n")
    32  		buff.WStringNoLen(fmt.Sprintf("\n%s: %s", sp, sg.Name()))
    33  		buff.WStringNoLen(fmt.Sprintf("\n%sstate {", sp))
    34  		s.marshall(sg, sp, buff)
    35  		buff.WStringNoLen(fmt.Sprintf("\n%s}", sp))
    36  		buff.WStringNoLen("\n")
    37  	})
    38  
    39  	grp.IterLink(func(lnk graph.ILink) {
    40  		in := lnk.In().Node().Name()
    41  		on := lnk.Out().Node().Name()
    42  		typ := lnk.In().Name()
    43  		buff.WStringNoLen(fmt.Sprintf("\n%s  %s --> |%s| %s",
    44  			prefix, on, typ, in))
    45  	})
    46  }