awalterschulze / gographviz

Parses the Graphviz DOT language in golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to set `node[shape=box style=rounded labelloc=b]` global attribute?

tsjsdbd opened this issue · comments

image

it's not allowed use like this:

graph.AddAttr(workflowName, "node", "shape=rect style=rounded, labelloc=b")

but if set attribute one by one, it will not take affect.

graph.AddAttr(workflowName, "shape", "rect")
graph.AddAttr(workflowName, "style", "rounded")
graph.AddAttr(workflowName, "labelloc", "b")

image

finally I have to add Global attribute to every Node by:

for _, node := range(graph.Nodes.Nodes) {
   node.Attrs.Add("shape", "rect")
   node.Attrs.Add("style", "rounded")
   node.Attrs.Add("labelloc", "b")
}

if can support this grammar, that will be great (by the way, it's pretty good now):
node[shape=rect style=rounded labelloc=b]

like:

attrs := make(map[string]string)
attrs["shape"] = "rect"
attrs["style"] = "rounded"
attrs["labelloc"] = "b"
graph.AddNodeAttr(workflowName, attrs)
type Graph struct {
	Attrs     Attrs
        NodeAttrs     Attrs  //<== maybe need this one ?
        EdgeAttrs     Attrs  //<== same
	Name      string
	Directed  bool
	Strict    bool
	Nodes     *Nodes
	Edges     *Edges
	SubGraphs *SubGraphs
	Relations *Relations
}

sorry, I didn't use graphviz before,
I used Gographviz because my go project needed to generate a DAG flowchart, and I found this project.
simply say , i want this code:

graph.String()

can output the strings like this:

digraph tsjsdbd {
node[shape=box style=rounded labelloc=b]
"nodeA" -> "nodeB"
"nodeA"
"nodeB"
}

but now, i can only get output like this:

digraph tsjsdbd {
"nodeA" -> "nodeB"
"nodeA"  [shape=box style=rounded labelloc=b]
"nodeB"  [shape=box style=rounded labelloc=b]
}

Ah I see

This test passes

package gographviz

import (
	"testing"
	"strings"
)

// https://github.com/awalterschulze/gographviz/issues/32
func TestIssue55NodeAttrs(t *testing.T) {
	inputString := `
	digraph tsjsdbd {
		node[shape=box style=rounded labelloc=b]
		"nodeA" -> "nodeB"
		"nodeA"
		"nodeB"
		}
	`

	g, err := Read([]byte(inputString))
	if err != nil {
		t.Fatal(err)
	}

	s := `digraph tsjsdbd {
		"nodeA"->"nodeB";
		"nodeA" [ labelloc=b, shape=box, style=rounded ];
		"nodeB" [ labelloc=b, shape=box, style=rounded ];

	}`

	ss := strings.Join(strings.Fields(s), " ")
	gg := strings.Join(strings.Fields(g.String()), " ")
	if ss != gg {
		t.Fatalf("got <%s> want <%s>", ss, gg)
	}
	
}

So they are semantically equivalent.
This is a simplification that was decided to use during the design of this package.
https://github.com/awalterschulze/gographviz/blob/master/analyse.go#L69

If you want to really print out the same as the initial version
Maybe you could make Graph interface that dedups node attributes that are the same back into your more compact form.

Hope I have answered your question. Going to close this for now, but feel free to reopen.