Skip to content

Commit

Permalink
Don't output trailing commas in dot attrs
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Chlebek <[email protected]>
  • Loading branch information
echlebek committed Mar 14, 2019
1 parent 8a93f9f commit c7a9451
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions dot/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package dot
import (
"bytes"
"fmt"
"github.com/echlebek/goraph"
"io"
"sort"
"strings"

"github.com/echlebek/goraph"
)

const (
Expand Down Expand Up @@ -67,10 +69,11 @@ func writeAttrs(w *bytes.Buffer, name string, tabs int, attrs map[string]interfa
keys = append(keys, k)
}
sort.Sort(keys)
list := []string{}
for _, k := range keys {
fmt.Fprintf(w, "%s=%v, ", k, attrs[k])
list = append(list, fmt.Sprintf("%s=%v", k, attrs[k]))
}
fmt.Fprint(w, "];\n")
fmt.Fprintln(w, strings.Join(list, ", "), "];")
}

// WriteDot writes dot to w. It returns the number of bytes written and
Expand Down
11 changes: 6 additions & 5 deletions dot/dot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package dot

import (
"bytes"
"github.com/echlebek/goraph"
"testing"

"github.com/echlebek/goraph"
)

func TestDirectedDotWriter(t *testing.T) {
Expand Down Expand Up @@ -37,15 +38,15 @@ func TestDirectedDotWriter(t *testing.T) {
WriteDot(buf, dot)

expected := `digraph {
graph [ splines=false, ];
0 [ label=Happy, ];
1 [ label=Sleepy, shape=egg, ];
graph [ splines=false ];
0 [ label=Happy ];
1 [ label=Sleepy, shape=egg ];
1 -> 0;
1 -> 2;
3 -> 1;
4 -> 3;
4 -> 5;
5 -> 6 [ arrowhead=diamond, ];
5 -> 6 [ arrowhead=diamond ];
6 -> 7;
6 -> 9;
9 -> 8;
Expand Down

0 comments on commit c7a9451

Please sign in to comment.