Skip to content

Commit

Permalink
Merge pull request #21 from k1LoW/relations
Browse files Browse the repository at this point in the history
Add `relations:` to describe the relationship between components
  • Loading branch information
k1LoW authored Oct 3, 2020
2 parents 00438eb + 082c499 commit 6340590
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 192 deletions.
16 changes: 8 additions & 8 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,31 @@ var docCmd = &cobra.Command{
}

// tags
for _, nw := range cfg.Tags() {
for _, rel := range cfg.Tags() {
cfg, err := newConfig()
if err != nil {
printFatalln(cmd, err)
}
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)
}
}
Expand Down Expand Up @@ -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)
}
Expand Down
74 changes: 38 additions & 36 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}{})
Expand All @@ -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),
})
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
89 changes: 69 additions & 20 deletions config/network.go → config/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 6340590

Please sign in to comment.