From 21083910e59bbc88b19bd2751dbec6080c57af05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20C=C3=A9sar=20Batista?= Date: Thu, 1 Dec 2022 11:47:27 -0800 Subject: [PATCH] Rename to Layout --- cmd/d2/main.go | 6 ------ d2.go | 8 +++----- d2layouts/d2sequence/layout.go | 8 +------- d2layouts/d2sequence/layout_test.go | 28 +++++++++++++++++++++++++--- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/cmd/d2/main.go b/cmd/d2/main.go index 063783e37..b1f5d5e3d 100644 --- a/cmd/d2/main.go +++ b/cmd/d2/main.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "os" "os/exec" "path/filepath" "strings" @@ -14,7 +13,6 @@ import ( "github.com/spf13/pflag" "oss.terrastruct.com/d2" - "oss.terrastruct.com/d2/d2layouts/d2sequence" "oss.terrastruct.com/d2/d2plugin" "oss.terrastruct.com/d2/d2renderers/d2svg" "oss.terrastruct.com/d2/d2renderers/textmeasure" @@ -191,10 +189,6 @@ func compile(ctx context.Context, ms *xmain.State, isWatching bool, plugin d2plu } layout := plugin.Layout - // TODO: remove, this is just a feature flag to test sequence diagrams as we work on them - if os.Getenv("D2_SEQUENCE") == "1" { - layout = d2sequence.Layout - } d, err := d2.Compile(ctx, string(input), &d2.CompileOptions{ Layout: layout, Ruler: ruler, diff --git a/d2.go b/d2.go index 8331ee456..afa8085fa 100644 --- a/d2.go +++ b/d2.go @@ -40,11 +40,9 @@ func Compile(ctx context.Context, input string, opts *CompileOptions) (*d2target return nil, err } - if layout, err := getLayout(opts); err == nil { - if err := d2sequence.Layout2(ctx, g, layout); err != nil { - return nil, err - } - } else { + if layout, err := getLayout(opts); err != nil { + return nil, err + } else if err := d2sequence.Layout(ctx, g, layout); err != nil { return nil, err } diff --git a/d2layouts/d2sequence/layout.go b/d2layouts/d2sequence/layout.go index 9c31ac915..858b5a5a5 100644 --- a/d2layouts/d2sequence/layout.go +++ b/d2layouts/d2sequence/layout.go @@ -15,7 +15,7 @@ import ( "oss.terrastruct.com/d2/lib/shape" ) -func Layout2(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error { +func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error { oldObjects := g.Objects oldEdges := g.Edges @@ -97,12 +97,6 @@ func Layout2(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Cont return nil } -func Layout(ctx context.Context, g *d2graph.Graph) (err error) { - sd := newSequenceDiagram(nil, nil) - sd.layout() - return nil -} - type sequenceDiagram struct { messages []*d2graph.Edge lifelines []*d2graph.Edge diff --git a/d2layouts/d2sequence/layout_test.go b/d2layouts/d2sequence/layout_test.go index 2fcf61ad0..cd017f38d 100644 --- a/d2layouts/d2sequence/layout_test.go +++ b/d2layouts/d2sequence/layout_test.go @@ -26,6 +26,7 @@ func TestBasicSequenceDiagram(t *testing.T) { // ◄───────────────────────┤ // │ │ g := d2graph.NewGraph(nil) + g.Root.Attributes.Shape = d2graph.Scalar{Value: d2target.ShapeSequenceDiagram} n1 := g.Root.EnsureChild([]string{"n1"}) n1.Box = geo.NewBox(nil, 100, 100) n2 := g.Root.EnsureChild([]string{"n2"}) @@ -58,7 +59,17 @@ func TestBasicSequenceDiagram(t *testing.T) { nEdges := len(g.Edges) ctx := log.WithTB(context.Background(), t, nil) - Layout(ctx, g) + Layout(ctx, g, func(ctx context.Context, g *d2graph.Graph) error { + // just set some position as if it had been properly placed + for _, obj := range g.Objects { + obj.TopLeft = geo.NewPoint(0, 0) + } + + for _, edge := range g.Edges { + edge.Route = []*geo.Point{geo.NewPoint(1, 1)} + } + return nil + }) // asserts that actors were placed in the expected x order and at y=0 actors := []*d2graph.Object{ @@ -158,6 +169,7 @@ func TestSpansSequenceDiagram(t *testing.T) { // t2 ││ │ // ├┘◄─────────────────────┤ g := d2graph.NewGraph(nil) + g.Root.Attributes.Shape = d2graph.Scalar{Value: d2target.ShapeSequenceDiagram} a := g.Root.EnsureChild([]string{"a"}) a.Box = geo.NewBox(nil, 100, 100) a.Attributes = d2graph.Attributes{ @@ -190,7 +202,17 @@ func TestSpansSequenceDiagram(t *testing.T) { } ctx := log.WithTB(context.Background(), t, nil) - Layout(ctx, g) + Layout(ctx, g, func(ctx context.Context, g *d2graph.Graph) error { + // just set some position as if it had been properly placed + for _, obj := range g.Objects { + obj.TopLeft = geo.NewPoint(0, 0) + } + + for _, edge := range g.Edges { + edge.Route = []*geo.Point{geo.NewPoint(1, 1)} + } + return nil + }) // check properties if a.Attributes.Shape.Value != shape.PERSON_TYPE { @@ -335,7 +357,7 @@ func TestNestedSequenceDiagrams(t *testing.T) { } ctx := log.WithTB(context.Background(), t, nil) - if err = Layout2(ctx, g, layoutFn); err != nil { + if err = Layout(ctx, g, layoutFn); err != nil { t.Fatal(err) }