Skip to content

Commit

Permalink
Add dot formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
stilvoid committed Jun 21, 2015
1 parent 4584140 commit 29578ea
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Please parses data structures in the above formats and can output them as:

* bash `declare` syntax
* YAML
* dot (graphviz)

If you're not familiar with associative arrays in bash or how `declare` works, it's worth reading the following:

Expand Down Expand Up @@ -195,3 +196,7 @@ Making use of the bash-declare output format

$ echo '{"json": ["array", "values"]}' | please parse json | (declare -A data=$(cat -); echo ${data[1]})
values

Generating a graph from some json (you need graphviz installed)

$ echo '{"vars": ["foo", "bar", "baz"], "cake": {"is_lie": true}}' | please parse -o dot | dot -Tpng > graph.png
1 change: 1 addition & 0 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func init() {
formatters = map[string]func(interface{}, string) string{
"bash": formatter.Bash,
"yaml": formatter.Yaml,
"dot": formatter.Dot,
}
}

Expand Down
155 changes: 155 additions & 0 deletions formatter/dot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package formatter

import (
"bytes"
"fmt"
"os"
"reflect"
"strconv"
"strings"
)

type node struct {
name string
label string
}

type link struct {
left string
right string
}

func wrap(in string) string {
out := strings.Replace(in, "\\", "\\\\", -1)
out = strings.Replace(out, "\"", "\\\"", -1)
out = strings.Replace(out, "\n", "\\n", -1)
out = fmt.Sprintf("\"%s\"", out)

return out
}

func Dot(in interface{}, path string) (out string) {
nodes, links := flatten(in, path, "root")

nodes = append(nodes, node{
name: "root",
label: "[Root]",
})

var buf bytes.Buffer

for _, node := range nodes {
buf.WriteString(fmt.Sprintf("%s [label=%s];\n", wrap(node.name), wrap(node.label)))
}

for _, link := range links {
buf.WriteString(fmt.Sprintf("%s -- %s;\n", wrap(link.left), wrap(link.right)))
}

return fmt.Sprintf("graph{\n%s}", buf.String())
}

func flatten(in interface{}, path string, current_path string) ([]node, []link) {
var nodes []node
var links []link

if in == nil {
return nodes, links
}

val := reflect.ValueOf(in)

split_path := strings.SplitN(path, ".", 2)

this_path := split_path[0]
var next_path string

if len(split_path) > 1 {
next_path = split_path[1]
}

switch val.Kind() {
case reflect.Map:
vv := in.(map[string]interface{})

if this_path != "" {
if _, ok := vv[this_path]; !ok {
fmt.Fprintf(os.Stderr, "Key does not exist: %s\n", this_path)
os.Exit(1)
}

return flatten(vv[this_path], next_path, this_path)
}

for key, value := range vv {
target := current_path + "-" + key

nodes = append(nodes, node{
name: target,
label: key,
})

if current_path != "" {
links = append(links, link{
left: current_path,
right: target,
})
}

new_nodes, new_links := flatten(value, path, target)

nodes = append(nodes, new_nodes...)
links = append(links, new_links...)
}

return nodes, links
case reflect.Array, reflect.Slice:
if this_path != "" {
index, err := strconv.Atoi(this_path)

if err != nil || index < 0 || index >= val.Len() {
fmt.Fprintf(os.Stderr, "Key does not exist: %s\n", this_path)
os.Exit(1)
}

return flatten(val.Index(index).Interface(), next_path, this_path)
}

for index := 0; index < val.Len(); index++ {
value := val.Index(index).Interface()

target := current_path + "-" + fmt.Sprint(index)

nodes = append(nodes, node{
name: target,
label: fmt.Sprintf("[%d]", index),
})

if current_path != "" {
links = append(links, link{
left: current_path,
right: target,
})
}

new_nodes, new_links := flatten(value, path, target)

nodes = append(nodes, new_nodes...)
links = append(links, new_links...)
}

return nodes, links
default:
if this_path != "" {
fmt.Fprintf(os.Stderr, "Key does not exist: %s\n", this_path)
os.Exit(1)
}

links = append(links, link{
left: current_path,
right: fmt.Sprint(in),
})

return nodes, links
}
}

0 comments on commit 29578ea

Please sign in to comment.