diff --git a/cmd/doc.go b/cmd/doc.go index ef8f8220..96a30598 100644 --- a/cmd/doc.go +++ b/cmd/doc.go @@ -176,7 +176,7 @@ var docCmd = &cobra.Command{ } // tags - for _, nw := range cfg.Tags() { + for _, rel := range cfg.Tags() { cfg, err := newConfig() if err != nil { printFatalln(cmd, err) @@ -184,23 +184,23 @@ var docCmd = &cobra.Command{ o := md.New(cfg) // generate md - mPath := filepath.Join(cfg.DocPath, config.MdPath("network-tag", []string{nw.Id()})) + mPath := filepath.Join(cfg.DocPath, config.MdPath("tag", []string{rel.Id()})) file, err := os.Create(mPath) if err != nil { printFatalln(cmd, err) } - if err := o.OutputTag(file, nw); err != nil { + if err := o.OutputTag(file, rel); err != nil { printFatalln(cmd, err) } // draw diagram diag := gviz.New(cfg) - dPath := filepath.Join(cfg.DocPath, config.ImagePath("network-tag", []string{nw.Id()}, format)) + dPath := filepath.Join(cfg.DocPath, config.ImagePath("tag", []string{rel.Id()}, format)) dFile, err := os.OpenFile(dPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) // #nosec if err != nil { printFatalln(cmd, err) } - if err := diag.OutputTag(dFile, nw); err != nil { + if err := diag.OutputTag(dFile, rel); err != nil { printFatalln(cmd, err) } } @@ -257,12 +257,12 @@ func diagExists(cfg *config.Config) error { } // tags - for _, nw := range cfg.Tags() { - mPath := filepath.Join(cfg.DocPath, config.ImagePath("network-tag", []string{nw.Id()}, format)) + for _, rel := range cfg.Tags() { + mPath := filepath.Join(cfg.DocPath, config.ImagePath("tag", []string{rel.Id()}, format)) if _, err := os.Lstat(mPath); err == nil { return fmt.Errorf("%s already exist", mPath) } - dPath := filepath.Join(cfg.DocPath, config.MdPath("network-tag", []string{nw.Id()})) + dPath := filepath.Join(cfg.DocPath, config.MdPath("tag", []string{rel.Id()})) if _, err := os.Lstat(dPath); err == nil { return fmt.Errorf("%s already exist", dPath) } diff --git a/config/config.go b/config/config.go index 95ebf8b5..b254201f 100644 --- a/config/config.go +++ b/config/config.go @@ -37,11 +37,11 @@ type Attr struct { } type NEdge struct { - Src *Component - Dst *Component - Desc string - Network *Network - Attrs []*Attr + Src *Component + Dst *Component + Desc string + Relation *Relation + Attrs []*Attr } type Layer struct { @@ -50,14 +50,14 @@ type Layer struct { } type Config struct { - Name string `yaml:"name"` - Desc string `yaml:"desc,omitempty"` - DocPath string `yaml:"docPath"` - DescPath string `yaml:"descPath"` - Diagrams []*Diagram `yaml:"diagrams"` - Nodes []*Node `yaml:"nodes"` - Networks []*Network `yaml:"networks"` - rawNetworks []*rawNetwork + Name string `yaml:"name"` + Desc string `yaml:"desc,omitempty"` + DocPath string `yaml:"docPath"` + DescPath string `yaml:"descPath"` + Diagrams []*Diagram `yaml:"diagrams"` + Nodes []*Node `yaml:"nodes"` + Relations []*Relation `yaml:"relations"` + rawRelations []*rawRelation realNodes []*RealNode layers []*Layer clusters Clusters @@ -175,7 +175,7 @@ func (cfg *Config) Build() error { if err := cfg.buildComponents(); err != nil { return err } - if err := cfg.buildNetworks(); err != nil { + if err := cfg.buildRelations(); err != nil { return err } if err := cfg.checkUnique(); err != nil { @@ -293,8 +293,8 @@ func (cfg *Config) buildComponents() error { gc := orderedmap.NewOrderedMap() nc := orderedmap.NewOrderedMap() cc := orderedmap.NewOrderedMap() - for _, nw := range cfg.rawNetworks { - for _, r := range nw.Route { + for _, rel := range cfg.rawRelations { + for _, r := range rel.Components { switch sepCount(r) { case 2: // cluster components cc.Set(r, struct{}{}) @@ -308,7 +308,7 @@ func (cfg *Config) buildComponents() error { // global components for _, c := range gc.Keys() { - // create global component from network route + // create global component from relations cfg.globalComponents = append(cfg.globalComponents, &Component{ Name: c.(string), }) @@ -335,7 +335,7 @@ func (cfg *Config) buildComponents() error { } } if !belongTo { - // create node component from network route + // create node component from relations component := &Component{ Name: comName, Node: n, @@ -353,7 +353,7 @@ func (cfg *Config) buildComponents() error { belongTo := false for _, cl := range cfg.Clusters() { if strings.EqualFold(cl.FullName(), clName) { - // create cluster component from network route + // create cluster component from relations com := &Component{ Cluster: cl, Name: comName, @@ -410,41 +410,43 @@ func (cfg *Config) parseClusterLabel(label string) (*Cluster, error) { return newC, nil } -func (cfg *Config) buildNetworks() error { - nwTags := orderedmap.NewOrderedMap() - for _, nw := range cfg.rawNetworks { - nnw := &Network{ - NetworkId: nw.Id, - Tags: nw.Tags, +func (cfg *Config) buildRelations() error { + relTags := orderedmap.NewOrderedMap() + for _, rel := range cfg.rawRelations { + nrel := &Relation{ + RelationId: rel.Id, + Type: rel.Type, + Tags: rel.Tags, + Attrs: rel.Attrs, } - for _, r := range nw.Route { + for _, r := range rel.Components { c, err := cfg.FindComponent(r) if err != nil { return err } - nnw.Route = append(nnw.Route, c) + nrel.Components = append(nrel.Components, c) } - cfg.Networks = append(cfg.Networks, nnw) + cfg.Relations = append(cfg.Relations, nrel) // tags - for _, t := range nw.Tags { + for _, t := range rel.Tags { var nt *Tag - nti, ok := nwTags.Get(t) + nti, ok := relTags.Get(t) if ok { nt = nti.(*Tag) } else { nt = &Tag{ Name: t, } - nwTags.Set(t, nt) + relTags.Set(t, nt) } - nt.Networks = append(nt.Networks, nnw) + nt.Relations = append(nt.Relations, nrel) } } - cfg.nEdges = SplitNetworks(cfg.Networks) + cfg.nEdges = SplitRelations(cfg.Relations) - for _, k := range nwTags.Keys() { - nt, _ := nwTags.Get(k) + for _, k := range relTags.Keys() { + nt, _ := relTags.Get(k) cfg.tags = append(cfg.tags, nt.(*Tag)) } @@ -545,7 +547,7 @@ func (cfg *Config) buildDescriptions() error { if t.Desc != "" { continue } - desc, err := cfg.readDescFile(MdPath("_network-tag", []string{t.Id()})) + desc, err := cfg.readDescFile(MdPath("_tag", []string{t.Id()})) if err != nil { return err } diff --git a/config/config_test.go b/config/config_test.go index d5cccb01..7c446b65 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -89,7 +89,7 @@ func TestBuildNestedCluster(t *testing.T) { cGlobalComponentLen := len(d.GlobalComponents()) cClusterComponentLen := len(d.ClusterComponents()) cNodeComponentLen := len(d.NodeComponents()) - cNetworkLen := len(d.Networks) + cRelationLen := len(d.Relations) gotClusters, gotNodes, gotNEdges, err := d.BuildNestedClusters(tt.layers) if err != nil { @@ -126,8 +126,8 @@ func TestBuildNestedCluster(t *testing.T) { if got := len(d.NodeComponents()); got != cNodeComponentLen { t.Errorf("TestBuildNestedCluster(%d) got %v want %v", i, got, cNodeComponentLen) } - if got := len(d.Networks); got != cNetworkLen { - t.Errorf("TestBuildNestedCluster(%d) got %v want %v", i, got, cNetworkLen) + if got := len(d.Relations); got != cRelationLen { + t.Errorf("TestBuildNestedCluster(%d) got %v want %v", i, got, cRelationLen) } } } diff --git a/config/network.go b/config/relation.go similarity index 50% rename from config/network.go rename to config/relation.go index 724cf70e..0089169c 100644 --- a/config/network.go +++ b/config/relation.go @@ -7,30 +7,78 @@ import ( "github.com/elliotchance/orderedmap" ) -type Network struct { - NetworkId string - Route []*Component - Tags []string +type RelationType struct { + Name string + ComponentsKey string + Attrs []*Attr } -func (n *Network) FullName() string { - return fmt.Sprintf(n.NetworkId) +var RelationTypeDefault = &RelationType{ + Name: "relation", + ComponentsKey: "components", + Attrs: []*Attr{ + &Attr{ + Key: "arrowhead", + Value: "dot", + }, + &Attr{ + Key: "arrowhead", + Value: "dot", + }, + &Attr{ + Key: "style", + Value: "dashed", + }, + }, } -func (n *Network) Id() string { +var RelationTypeNetwork = &RelationType{ + Name: "network", + ComponentsKey: "route", + Attrs: []*Attr{ + &Attr{ + Key: "arrowhead", + Value: "normal", + }, + &Attr{ + Key: "arrowhead", + Value: "normal", + }, + &Attr{ + Key: "style", + Value: "bold", + }, + }, +} + +type Relation struct { + RelationId string + Type *RelationType + Components []*Component + Tags []string + Attrs []*Attr +} + +func (n *Relation) FullName() string { + return fmt.Sprintf(n.RelationId) +} + +func (n *Relation) Id() string { return strings.ToLower(n.FullName()) } -type rawNetwork struct { - Id string - Route []string - Tags []string +type rawRelation struct { + Id string + Type *RelationType + Components []string + Tags []string + Attrs []*Attr } type Tag struct { - Name string - Desc string - Networks []*Network + Name string + Desc string + Relations []*Relation } func (t *Tag) FullName() string { @@ -41,17 +89,18 @@ func (t *Tag) Id() string { return strings.ToLower(t.FullName()) } -func SplitNetworks(networks []*Network) []*NEdge { +func SplitRelations(relations []*Relation) []*NEdge { var prev *Component edges := []*NEdge{} - for _, nw := range networks { + for _, rel := range relations { prev = nil - for _, r := range nw.Route { + for _, r := range rel.Components { if prev != nil { edge := &NEdge{ - Src: prev, - Dst: r, - Network: nw, + Src: prev, + Dst: r, + Relation: rel, + Attrs: rel.Attrs, } prev.NEdges = append(prev.NEdges, edge) r.NEdges = append(r.NEdges, edge) diff --git a/config/yaml.go b/config/yaml.go index c801b9b2..3c030094 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "regexp" + "sort" "strings" "github.com/goccy/go-yaml" @@ -12,12 +13,13 @@ import ( func (d *Config) UnmarshalYAML(data []byte) error { raw := struct { - Name string `yaml:"name"` - Desc string `yaml:"desc,omitempty"` - DocPath string `yaml:"docPath"` - Diagrams []*Diagram `yaml:"diagrams"` - Nodes []*Node `yaml:"nodes"` - Networks []interface{} `yaml:"networks"` + Name string `yaml:"name"` + Desc string `yaml:"desc,omitempty"` + DocPath string `yaml:"docPath"` + Diagrams []*Diagram `yaml:"diagrams"` + Nodes []*Node `yaml:"nodes"` + Networks []interface{} `yaml:"networks"` + Relations []interface{} `yaml:"relations"` }{} if err := yaml.Unmarshal(data, &raw); err != nil { @@ -29,68 +31,20 @@ func (d *Config) UnmarshalYAML(data []byte) error { d.Diagrams = raw.Diagrams d.Nodes = raw.Nodes - for _, nw := range raw.Networks { - route := []string{} - tags := []string{} - switch v := nw.(type) { - case []interface{}: - for _, r := range v { - route = append(route, r.(string)) - } - if len(route) < 2 { - return fmt.Errorf("invalid network format: %s", v) - } - id, err := genNetworkId(route) - if err != nil { - return err - } - tags = []string{id} - rnw := &rawNetwork{ - Id: id, - Route: route, - Tags: tags, - } - d.rawNetworks = append(d.rawNetworks, rnw) - case map[string]interface{}: - var ( - id string - err error - ) - idi, ok := v["id"] - if ok { - id = idi.(string) - } else { - id, err = genNetworkId(route) - if err != nil { - return err - } - } - ri, ok := v["route"] - if !ok { - return fmt.Errorf("invalid network format: %s", v) - } - for _, r := range ri.([]interface{}) { - route = append(route, r.(string)) - } - if len(route) < 2 { - return fmt.Errorf("invalid network format: %s", v) - } - ti, ok := v["tags"] - if ok { - for _, t := range ti.([]interface{}) { - tags = append(tags, t.(string)) - } - } - if len(tags) == 0 { - tags = []string{id} - } - rnw := &rawNetwork{ - Id: id, - Route: route, - Tags: tags, - } - d.rawNetworks = append(d.rawNetworks, rnw) + for _, rel := range raw.Networks { + rel, err := parseRelation(RelationTypeNetwork, rel) + if err != nil { + return err + } + d.rawRelations = append(d.rawRelations, rel) + } + + for _, rel := range raw.Relations { + rel, err := parseRelation(RelationTypeDefault, rel) + if err != nil { + return err } + d.rawRelations = append(d.rawRelations, rel) } return nil } @@ -124,11 +78,106 @@ func (n *Node) UnmarshalYAML(data []byte) error { return nil } -func genNetworkId(route []string) (string, error) { +func parseRelation(relType *RelationType, rel interface{}) (*rawRelation, error) { + components := []string{} + tags := []string{} + switch v := rel.(type) { + case []interface{}: + for _, r := range v { + components = append(components, r.(string)) + } + if len(components) < 2 { + return nil, fmt.Errorf("invalid %s format: %s", relType.Name, v) + } + id, err := genRelationId(components) + if err != nil { + return nil, err + } + tags = []string{id} + return &rawRelation{ + Id: id, + Type: relType, + Components: components, + Tags: tags, + Attrs: relType.Attrs, + }, nil + case map[string]interface{}: + var ( + id string + err error + ) + idi, ok := v["id"] + if ok { + id = idi.(string) + } else { + id, err = genRelationId(components) + if err != nil { + return nil, err + } + } + ri, ok := v[relType.ComponentsKey] + if !ok { + return nil, fmt.Errorf("invalid %s format: %s", relType.Name, v) + } + for _, r := range ri.([]interface{}) { + components = append(components, r.(string)) + } + if len(components) < 2 { + return nil, fmt.Errorf("invalid %s format: %s", relType.Name, v) + } + typei, ok := v["type"] + if ok { + switch typei.(string) { + case "network": + relType = RelationTypeNetwork + default: + return nil, fmt.Errorf("invalid %s format: %s", relType.Name, v) + } + } + ti, ok := v["tags"] + if ok { + for _, t := range ti.([]interface{}) { + tags = append(tags, t.(string)) + } + } + if len(tags) == 0 { + tags = []string{id} + } + attrs := []*Attr{} + attrsi, ok := v["attrs"] + if ok { + for k, v := range attrsi.(map[string]interface{}) { + attrs = append(attrs, &Attr{ + Key: k, + Value: v.(string), + }) + } + } + sort.Slice(attrs, func(i, j int) bool { + if attrs[i].Key == attrs[j].Key { + return attrs[i].Value < attrs[j].Value + } + return attrs[i].Key < attrs[j].Key + }) + attrs = append(relType.Attrs, attrs...) + + return &rawRelation{ + Id: id, + Type: relType, + Components: components, + Tags: tags, + Attrs: attrs, + }, nil + default: + return nil, fmt.Errorf("invalid relation format: %s", v) + } +} + +func genRelationId(components []string) (string, error) { h := sha256.New() - if _, err := io.WriteString(h, fmt.Sprintf("%s", route)); err != nil { + if _, err := io.WriteString(h, fmt.Sprintf("%s", components)); err != nil { return "", err } s := fmt.Sprintf("%x", h.Sum(nil)) - return fmt.Sprintf("nw-%s", s[:12]), nil + return fmt.Sprintf("rel-%s", s[:12]), nil } diff --git a/output/dot/dot.go b/output/dot/dot.go index 329c8c14..8f049070 100644 --- a/output/dot/dot.go +++ b/output/dot/dot.go @@ -62,7 +62,7 @@ func (d *Dot) OutputLayer(wr io.Writer, l *config.Layer) error { L: for _, e := range nEdges { for _, n := range remain { - // remove nw with global nodes + // remove rel with global nodes if strings.HasPrefix(e.Src.Id(), fmt.Sprintf("%s:", n.Id())) { continue L } @@ -70,7 +70,7 @@ L: continue L } } - // remove nw with global components + // remove rel with global components if (e.Src.Node == nil && e.Src.Cluster == nil) || (e.Dst.Node == nil && e.Dst.Cluster == nil) { continue L } @@ -167,7 +167,7 @@ func (d *Dot) OutputTag(wr io.Writer, t *config.Tag) error { nIds := orderedmap.NewOrderedMap() globalComponents := []*config.Component{} gIds := orderedmap.NewOrderedMap() - edges := config.SplitNetworks(t.Networks) + edges := config.SplitRelations(t.Relations) for _, e := range edges { switch { @@ -214,7 +214,7 @@ func (d *Dot) OutputTag(wr io.Writer, t *config.Tag) error { return nil } -func (d *Dot) OutputNetwork(wr io.Writer, nw *config.Network) error { +func (d *Dot) OutputRelation(wr io.Writer, rel *config.Relation) error { ts, err := d.box.FindString("diagram.dot.tmpl") if err != nil { return err @@ -230,7 +230,7 @@ func (d *Dot) OutputNetwork(wr io.Writer, nw *config.Network) error { edges := []*config.NEdge{} for _, e := range d.config.NEdges() { - if e.Network.Id() != nw.Id() { + if e.Relation.Id() != rel.Id() { continue } switch { diff --git a/output/dot/templates/diagram.dot.tmpl b/output/dot/templates/diagram.dot.tmpl index b59e037b..18a3beef 100644 --- a/output/dot/templates/diagram.dot.tmpl +++ b/output/dot/templates/diagram.dot.tmpl @@ -67,7 +67,7 @@ digraph ndiag { {{ end }} {{ range $e := .Edges }} -"{{ $e.Src | id }}" -> "{{ $e.Dst | id }}"[arrowhead=normal, arrowtail=normal, headlabel="", taillabel="", color="#33333399", labelfontcolor="#333333", style=bold, fontname="Arial"{{ $e.Attrs | attrs }}]; +"{{ $e.Src | id }}" -> "{{ $e.Dst | id }}"[color="#33333399", labelfontcolor="#333333", fontname="Arial"{{ $e.Attrs | attrs }}]; {{ end }} } diff --git a/output/dot/templates/node.dot.tmpl b/output/dot/templates/node.dot.tmpl index 84dfc99f..989e47ef 100644 --- a/output/dot/templates/node.dot.tmpl +++ b/output/dot/templates/node.dot.tmpl @@ -72,7 +72,7 @@ digraph ndiag { {{ end }} {{ range $e := .Edges }} -"{{ $e.Src | id }}" -> "{{ $e.Dst | id }}"[arrowhead=normal, arrowtail=normal, headlabel="", taillabel="", color="#33333399", labelfontcolor="#333333", style=bold, fontname="Arial"{{ $e.Attrs | attrs }}]; +"{{ $e.Src | id }}" -> "{{ $e.Dst | id }}"[color="#33333399", labelfontcolor="#333333", style=bold, fontname="Arial"{{ $e.Attrs | attrs }}]; {{ end }} } diff --git a/output/gviz/gviz.go b/output/gviz/gviz.go index 4713b992..7d9d7251 100644 --- a/output/gviz/gviz.go +++ b/output/gviz/gviz.go @@ -53,9 +53,9 @@ func (g *Gviz) OutputTag(wr io.Writer, t *config.Tag) error { return g.render(wr, buf.Bytes()) } -func (g *Gviz) OutputNetwork(wr io.Writer, nw *config.Network) error { +func (g *Gviz) OutputRelation(wr io.Writer, rel *config.Relation) error { buf := &bytes.Buffer{} - if err := g.dot.OutputNetwork(buf, nw); err != nil { + if err := g.dot.OutputRelation(buf, rel); err != nil { return err } return g.render(wr, buf.Bytes()) diff --git a/output/md/md.go b/output/md/md.go index 5ec91641..b19610b9 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -29,7 +29,7 @@ func (m *Md) OutputDiagram(wr io.Writer, d *config.Diagram) error { return err } - rel, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) + relPath, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) if err != nil { return err } @@ -47,7 +47,7 @@ func (m *Md) OutputDiagram(wr io.Writer, d *config.Diagram) error { tmplData := map[string]interface{}{ "Diagram": d, "DiagFormat": m.config.DiagFormat(), - "DescPath": rel, + "DescPath": relPath, "Layers": layers, "Nodes": m.config.Nodes, "Tags": m.config.Tags(), @@ -64,7 +64,7 @@ func (m *Md) OutputLayer(wr io.Writer, l *config.Layer) error { return err } - rel, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) + relPath, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) if err != nil { return err } @@ -78,7 +78,7 @@ func (m *Md) OutputLayer(wr io.Writer, l *config.Layer) error { tmplData := map[string]interface{}{ "Layer": l, "DiagFormat": m.config.DiagFormat(), - "DescPath": rel, + "DescPath": relPath, "Clusters": clusters, } if err := tmpl.Execute(wr, tmplData); err != nil { @@ -93,26 +93,26 @@ func (m *Md) OutputNode(wr io.Writer, n *config.Node) error { return err } - rel, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) + relPath, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) if err != nil { return err } tags := []*config.Tag{} - nwTags := orderedmap.NewOrderedMap() + relTags := orderedmap.NewOrderedMap() for _, c := range n.Components { for _, e := range c.NEdges { - for _, ts := range e.Network.Tags { + for _, ts := range e.Relation.Tags { for _, t := range m.config.Tags() { if ts == t.Name { - nwTags.Set(ts, t) + relTags.Set(ts, t) } } } } } - for _, k := range nwTags.Keys() { - t, _ := nwTags.Get(k) + for _, k := range relTags.Keys() { + t, _ := relTags.Get(k) tags = append(tags, t.(*config.Tag)) } @@ -120,7 +120,7 @@ func (m *Md) OutputNode(wr io.Writer, n *config.Node) error { tmplData := map[string]interface{}{ "Node": n, "DiagFormat": m.config.DiagFormat(), - "DescPath": rel, + "DescPath": relPath, "Components": n.Components, "RealNodes": n.RealNodes, "Tags": tags, @@ -132,12 +132,12 @@ func (m *Md) OutputNode(wr io.Writer, n *config.Node) error { } func (m *Md) OutputTag(wr io.Writer, t *config.Tag) error { - ts, err := m.box.FindString("network-tag.md.tmpl") + ts, err := m.box.FindString("tag.md.tmpl") if err != nil { return err } - rel, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) + relPath, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) if err != nil { return err } @@ -146,7 +146,7 @@ func (m *Md) OutputTag(wr io.Writer, t *config.Tag) error { tmplData := map[string]interface{}{ "Tag": t, "DiagFormat": m.config.DiagFormat(), - "DescPath": rel, + "DescPath": relPath, } if err := tmpl.Execute(wr, tmplData); err != nil { @@ -156,22 +156,22 @@ func (m *Md) OutputTag(wr io.Writer, t *config.Tag) error { return nil } -func (m *Md) OutputNetwork(wr io.Writer, nw *config.Network) error { - ts, err := m.box.FindString("network.md.tmpl") +func (m *Md) OutputRelation(wr io.Writer, rel *config.Relation) error { + ts, err := m.box.FindString("relation.md.tmpl") if err != nil { return err } - rel, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) + relPath, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) if err != nil { return err } - tmpl := template.Must(template.New(nw.Id()).Funcs(output.FuncMap).Parse(ts)) + tmpl := template.Must(template.New(rel.Id()).Funcs(output.FuncMap).Parse(ts)) tmplData := map[string]interface{}{ - "Network": nw, + "Relation": rel, "DiagFormat": m.config.DiagFormat(), - "DescPath": rel, + "DescPath": relPath, } if err := tmpl.Execute(wr, tmplData); err != nil { @@ -187,7 +187,7 @@ func (m *Md) OutputIndex(wr io.Writer) error { return err } - rel, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) + relPath, err := filepath.Rel(filepath.Join("root", m.config.DocPath), filepath.Join("root", m.config.DescPath)) if err != nil { return err } @@ -197,7 +197,7 @@ func (m *Md) OutputIndex(wr io.Writer) error { "Config": m.config, "Diagram": m.config.PrimaryDiagram(), "DiagFormat": m.config.DiagFormat(), - "DescPath": rel, + "DescPath": relPath, "Diagrams": m.config.Diagrams, "Layers": m.config.Layers(), "Nodes": m.config.Nodes, diff --git a/output/md/templates/diagram.md.tmpl b/output/md/templates/diagram.md.tmpl index b3c4acae..a5aa77bf 100644 --- a/output/md/templates/diagram.md.tmpl +++ b/output/md/templates/diagram.md.tmpl @@ -23,12 +23,12 @@ {{- range $i, $n := .Nodes }} | [{{ $n | fullname }}]({{ mdpath "node" ($n | id) }}) ({{ len $n.RealNodes }}) | {{ if ne $n.Desc "" }}{{ $n.Desc | summary }}{{ else }}:pencil2:{{ end }} | {{- end }} -## Network tag groups +## Tag groups | Name | Description | | --- | --- | {{- range $i, $t := .Tags }} -| [{{ $t | fullname }}]({{ mdpath "network-tag" ($t | id) }}) | {{ if ne $t.Desc "" }}{{ $t.Desc | summary }}{{ else }}:pencil2:{{ end }} | +| [{{ $t | fullname }}]({{ mdpath "tag" ($t | id) }}) | {{ if ne $t.Desc "" }}{{ $t.Desc | summary }}{{ else }}:pencil2:{{ end }} | {{- end }} --- diff --git a/output/md/templates/index.md.tmpl b/output/md/templates/index.md.tmpl index 5712048a..d4906e94 100644 --- a/output/md/templates/index.md.tmpl +++ b/output/md/templates/index.md.tmpl @@ -28,12 +28,12 @@ | [{{ $n | fullname }}]({{ mdpath "node" ($n | id) }}) ({{ len $n.RealNodes }}) | {{ if ne $n.Desc "" }}{{ $n.Desc | summary }}{{ else }}:pencil2:{{ end }} | {{- end }} -## Network tag groups +## Tag groups | Name | Description | | --- | --- | {{- range $i, $t := .Tags }} -| [{{ $t | fullname }}]({{ mdpath "network-tag" ($t | id) }}) | {{ if ne $t.Desc "" }}{{ $t.Desc | summary }}{{ else }}:pencil2:{{ end }} | +| [{{ $t | fullname }}]({{ mdpath "tag" ($t | id) }}) | {{ if ne $t.Desc "" }}{{ $t.Desc | summary }}{{ else }}:pencil2:{{ end }} | {{- end }} --- diff --git a/output/md/templates/node.md.tmpl b/output/md/templates/node.md.tmpl index fb176ceb..d2507a45 100644 --- a/output/md/templates/node.md.tmpl +++ b/output/md/templates/node.md.tmpl @@ -10,17 +10,17 @@ ## Components -| Name | Description | From (Network) | To (Network) | +| Name | Description | From (Relation) | To (Relation) | | --- | --- | --- | --- | {{- range $i, $c := .Components }} | {{ $c | id }} | {{ $c.Desc | trim | nl2br }} :pencil2: | {{ fromlinks $c.NEdges $c }} | {{ tolinks $c.NEdges $c }} | {{- end }} -## Network tag groups +## Tag groups | Name | Description | | --- | --- | {{- range $i, $t := .Tags }} -| [{{ $t | fullname }}]({{ mdpath "network-tag" ($t | id) }}) | {{ if ne $t.Desc "" }}{{ $t.Desc | summary }}{{ else }}:pencil2:{{ end }} | +| [{{ $t | fullname }}]({{ mdpath "tag" ($t | id) }}) | {{ if ne $t.Desc "" }}{{ $t.Desc | summary }}{{ else }}:pencil2:{{ end }} | {{- end }} ## Real nodes {{ range $i, $rn := .RealNodes }} diff --git a/output/md/templates/network.md.tmpl b/output/md/templates/relation.md.tmpl similarity index 51% rename from output/md/templates/network.md.tmpl rename to output/md/templates/relation.md.tmpl index 9c28c7f8..838c6cac 100644 --- a/output/md/templates/network.md.tmpl +++ b/output/md/templates/relation.md.tmpl @@ -1,18 +1,18 @@ -# {{ .Network | fullname }} +# {{ .Relation | fullname }} -![diagram]({{ imgpath "network" (.Network | id) .DiagFormat }}) +![diagram]({{ imgpath "relation" (.Relation | id) .DiagFormat }}) -{{ .Network.Desc }} +{{ .Relation.Desc }}

- [ :pencil2: Edit description ] + [ :pencil2: Edit description ]

-## Route +## Components | # | Component | Component Description | | --- | --- | --- | -{{- range $i, $c := .Network.Route }} +{{- range $i, $c := .Relation.Components }} | {{ $i }} | {{ $c | id }} | {{ $c.Desc | trim | nl2br }} :pencil2: | {{- end }} diff --git a/output/md/templates/network-tag.md.tmpl b/output/md/templates/tag.md.tmpl similarity index 51% rename from output/md/templates/network-tag.md.tmpl rename to output/md/templates/tag.md.tmpl index 920bef99..c0dfae8e 100644 --- a/output/md/templates/network-tag.md.tmpl +++ b/output/md/templates/tag.md.tmpl @@ -1,19 +1,19 @@ # {{ .Tag | fullname }} -![diagram]({{ imgpath "network-tag" (.Tag | id) .DiagFormat }}) +![diagram]({{ imgpath "tag" (.Tag | id) .DiagFormat }}) {{ .Tag.Desc }}

- [ :pencil2: Edit description ] + [ :pencil2: Edit description ]

-## Route +## Components -{{- range $j, $nw := .Tag.Networks }} -| # | Component | Component Description | +{{- range $j, $rel := .Tag.Relations }} +| # | Name | Description | | --- | --- | --- | -{{- range $i, $c := $nw.Route }} +{{- range $i, $c := $rel.Components }} | {{ $i }} | {{ $c | id }} | {{ $c.Desc | trim | nl2br }} :pencil2: | {{- end }} {{ end }} diff --git a/output/output.go b/output/output.go index 3133bdc4..d68dc3c3 100644 --- a/output/output.go +++ b/output/output.go @@ -69,7 +69,7 @@ var FuncMap = template.FuncMap{ return config.MdPath(prefix, strs) }, "componentlink": componentLink, - "nwlink": nwLink, + "rellink": relLink, "fromlinks": func(edges []*config.NEdge, base *config.Component) string { links := []string{} for _, e := range edges { @@ -94,7 +94,7 @@ var FuncMap = template.FuncMap{ } var out string for _, a := range attrs { - out = fmt.Sprintf("%s, %s=%s", out, a.Key, a.Value) + out = fmt.Sprintf("%s, %s=\"%s\"", out, a.Key, a.Value) } return out }, @@ -129,12 +129,12 @@ func componentLink(c *config.Component) string { } } -func nwLink(nw *config.Network) string { +func relLink(rel *config.Relation) string { cIds := []string{} - for _, r := range nw.Route { + for _, r := range rel.Components { cIds = append(cIds, r.FullName()) } - return fmt.Sprintf("[%s](%s)", strings.Join(cIds, " -> "), config.MdPath("network", []string{nw.Id()})) + return fmt.Sprintf("[%s](%s)", strings.Join(cIds, " -> "), config.MdPath("relation", []string{rel.Id()})) } func unique(in []string) []string {