-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add read config from yaml files
- Loading branch information
Showing
6 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "->" |