Skip to content

Commit

Permalink
Add support for YAML configuration
Browse files Browse the repository at this point in the history
add read config from yaml files
  • Loading branch information
MrIceman committed Nov 28, 2023
2 parents 8083dfe + f647e88 commit 5106fd5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/mriceman/go-uml

go 1.20

require github.com/fogleman/gg v1.3.0
require (
github.com/fogleman/gg v1.3.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
66 changes: 66 additions & 0 deletions sequence/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sequence

import (
"errors"
"os"
"strings"

"gopkg.in/yaml.v3"
)


// Edge represents an Edge on a Sequence Diagram
type Edge struct {
From string `yaml:"from"`
To string `yaml:"to"`
Label string `yaml:"label"`
Type string `yaml:"type"`
}

type config struct {
Title string `yaml:"title"`
Partecipants []string `yaml:"partecipants"`
Edges []Edge `yaml:"edges"`
}

// DiagramFromYaml create a Diagram from a YAML file
func DiagramFromYaml(file string) (*Diagram, error) {
var cfg config
name := strings.TrimSuffix(file, ".yaml")

data, err := os.ReadFile(file)
if err != nil {
return nil, err
}

err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}

d := NewDiagram(name)

d.SetTitle(cfg.Title)

for _, partecipant := range cfg.Partecipants {
d.AddParticipants(partecipant)
}

for _, edge := range cfg.Edges {
switch edge.Type {
case "->":
err = d.AddDirectionalEdge(edge.From, edge.To, edge.Label)
case "-":
err = d.AddUndirectionalEdge(edge.From, edge.To, edge.Label)

default:
err = errors.New("edge type not valid")
}
}

if err != nil {
return nil, err
}

return d, nil
}
17 changes: 17 additions & 0 deletions sequence/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sequence

import (
"os"
"testing"
)

func TestDiagramFromYaml(t *testing.T) {
d, err := DiagramFromYaml("test.yaml")
if err != nil {
t.Fatalf("err %v", err)
}

defer os.Remove("test.png")

d.Render()
}
Binary file added sequence/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions sequence/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: "example"
partecipants: ["alice", "bob"]
edges:
- from: "alice"
to: "bob"
label: "sending"
type: "->"
- from: "bob"
to: "alice"
label: "receaving"
type: "->"

0 comments on commit 5106fd5

Please sign in to comment.