From 31564f148769f753a6d95e750bb104825c015b57 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 26 Dec 2022 20:51:37 -0800 Subject: [PATCH 1/2] exercise serde --- d2layouts/d2sequence/layout.go | 31 ++++++++++++++++++++----------- e2etests/e2e_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/d2layouts/d2sequence/layout.go b/d2layouts/d2sequence/layout.go index 1747c859b..49ae00b43 100644 --- a/d2layouts/d2sequence/layout.go +++ b/d2layouts/d2sequence/layout.go @@ -12,16 +12,7 @@ import ( "oss.terrastruct.com/util-go/go2" ) -// Layout runs the sequence diagram layout engine on objects of shape sequence_diagram -// -// 1. Traverse graph from root, skip objects with shape not `sequence_diagram` -// 2. Construct a sequence diagram from all descendant objects and edges -// 3. Remove those objects and edges from the main graph -// 4. Run layout on sequence diagrams -// 5. Set the resulting dimensions to the main graph shape -// 6. Run core layouts (still without sequence diagram innards) -// 7. Put back sequence diagram innards in correct location -func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error { +func WithoutSequenceDiagrams(ctx context.Context, g *d2graph.Graph) (map[string]*sequenceDiagram, map[string]int, map[string]int, error) { objectsToRemove := make(map[*d2graph.Object]struct{}) edgesToRemove := make(map[*d2graph.Edge]struct{}) sequenceDiagrams := make(map[string]*sequenceDiagram) @@ -41,7 +32,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte sd, err := layoutSequenceDiagram(g, obj) if err != nil { - return err + return nil, nil, nil, err } obj.Children = make(map[string]*d2graph.Object) obj.ChildrenArray = nil @@ -72,6 +63,24 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte // TODO this isn't a proper deletion because the objects still appear as children of the object g.Objects = layoutObjects + return sequenceDiagrams, objectOrder, edgeOrder, nil +} + +// Layout runs the sequence diagram layout engine on objects of shape sequence_diagram +// +// 1. Traverse graph from root, skip objects with shape not `sequence_diagram` +// 2. Construct a sequence diagram from all descendant objects and edges +// 3. Remove those objects and edges from the main graph +// 4. Run layout on sequence diagrams +// 5. Set the resulting dimensions to the main graph shape +// 6. Run core layouts (still without sequence diagram innards) +// 7. Put back sequence diagram innards in correct location +func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error { + sequenceDiagrams, objectOrder, edgeOrder, err := WithoutSequenceDiagrams(ctx, g) + if err != nil { + return err + } + if g.Root.IsSequenceDiagram() { // the sequence diagram is the only layout engine if the whole diagram is // shape: sequence_diagram diff --git a/e2etests/e2e_test.go b/e2etests/e2e_test.go index ec6a2e2cc..ccf4fec8d 100644 --- a/e2etests/e2e_test.go +++ b/e2etests/e2e_test.go @@ -17,9 +17,12 @@ import ( "oss.terrastruct.com/util-go/assert" "oss.terrastruct.com/util-go/diff" + "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2layouts/d2elklayout" + "oss.terrastruct.com/d2/d2layouts/d2near" + "oss.terrastruct.com/d2/d2layouts/d2sequence" "oss.terrastruct.com/d2/d2lib" "oss.terrastruct.com/d2/d2renderers/d2svg" "oss.terrastruct.com/d2/d2target" @@ -88,6 +91,27 @@ func runa(t *testing.T, tcs []testCase) { } } +// serde exercises serializing and deserializing the graph +func serde(t *testing.T, tc testCase, ruler *textmeasure.Ruler) { + ctx := context.Background() + ctx = log.WithTB(ctx, t, nil) + g, err := d2compiler.Compile("", strings.NewReader(tc.script), &d2compiler.CompileOptions{ + UTF16: false, + }) + tassert.Nil(t, err) + if len(g.Objects) > 0 { + err = g.SetDimensions(nil, ruler, nil) + tassert.Nil(t, err) + d2near.WithoutConstantNears(ctx, g) + d2sequence.WithoutSequenceDiagrams(ctx, g) + } + b, err := d2graph.SerializeGraph(g) + tassert.Nil(t, err) + var newG d2graph.Graph + err = d2graph.DeserializeGraph(b, &newG) + tassert.Nil(t, err) +} + func run(t *testing.T, tc testCase) { ctx := context.Background() ctx = log.WithTB(ctx, t, nil) @@ -98,6 +122,8 @@ func run(t *testing.T, tc testCase) { return } + serde(t, tc, ruler) + layoutsTested := []string{"dagre", "elk"} for _, layoutName := range layoutsTested { From 45797eb8599350e641f83cb8d66ec1849f62cf94 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 26 Dec 2022 20:52:47 -0800 Subject: [PATCH 2/2] comment --- e2etests/e2e_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/e2etests/e2e_test.go b/e2etests/e2e_test.go index ccf4fec8d..994770426 100644 --- a/e2etests/e2e_test.go +++ b/e2etests/e2e_test.go @@ -92,6 +92,7 @@ func runa(t *testing.T, tcs []testCase) { } // serde exercises serializing and deserializing the graph +// We want to run all the steps leading up to serialization in the course of regular layout func serde(t *testing.T, tc testCase, ruler *textmeasure.Ruler) { ctx := context.Background() ctx = log.WithTB(ctx, t, nil)