From 1ff119d8035050b6a48f642f4ad534924b475031 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 18 Jun 2023 23:57:08 -0700 Subject: [PATCH] baseAST --- d2compiler/compile.go | 1 + d2graph/d2graph.go | 2 ++ d2ir/compile.go | 3 +++ d2ir/d2ir.go | 42 ++++++++++++++++++++++++++++++++++++++++++ d2ir/merge.go | 8 ++++++-- 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index b8579ffe3..6bf07ecd7 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -111,6 +111,7 @@ func (c *compiler) compileBoardsField(g *d2graph.Graph, ir *d2ir.Map, fieldName g2 := d2graph.NewGraph() g2.Parent = g g2.AST = f.Map().AST().(*d2ast.Map) + g2.BaseAST = f.Map().BaseAST().(*d2ast.Map) c.compileBoard(g2, f.Map()) g2.Name = f.Name switch fieldName { diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index a91c95e12..77cf7d944 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -42,6 +42,8 @@ type Graph struct { // the board tree. IsFolderOnly bool `json:"isFolderOnly"` AST *d2ast.Map `json:"ast"` + // BaseAST is the AST of the original graph without inherited fields and edges + BaseAST *d2ast.Map `json:"baseAST,omitempty"` Root *Object `json:"root"` Edges []*Edge `json:"edges"` diff --git a/d2ir/compile.go b/d2ir/compile.go index 2c4022edc..3995efffe 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -153,6 +153,9 @@ func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) c.err.Errors = append(c.err.Errors, err.(d2ast.Error)) return } + if f.Inherited { + f.Inherited = false + } if refctx.Key.Primary.Unbox() != nil { f.Primary_ = &Scalar{ diff --git a/d2ir/d2ir.go b/d2ir/d2ir.go index 47651fdc6..1255d144d 100644 --- a/d2ir/d2ir.go +++ b/d2ir/d2ir.go @@ -196,7 +196,25 @@ func (m *Map) CopyBase(newParent Node) *Map { layers := m.DeleteField("layers") scenarios := m.DeleteField("scenarios") steps := m.DeleteField("steps") + + had := make(map[string]struct{}) + if parentM, ok := newParent.(*Map); ok { + for _, f := range parentM.Fields { + had[f.Name] = struct{}{} + } + } + // TODO pretty sure you don't need one for edges. + // Maybe if both boards had a (x -> y)[0]? + m2 := m.Copy(newParent).(*Map) + for i := range m2.Fields { + if _, ok := had[m2.Fields[i].Name]; !ok { + m2.Fields[i].Inherited = true + } + } + for i := range m2.Edges { + m2.Edges[i].Inherited = true + } if layers != nil { m.Fields = append(m.Fields, layers) } @@ -277,6 +295,9 @@ type Field struct { Composite Composite `json:"composite,omitempty"` References []*FieldReference `json:"references,omitempty"` + + // Whether it's from a parent board or imported + Inherited bool `json:"inherited"` } func (f *Field) Copy(newParent Node) Node { @@ -431,6 +452,8 @@ type Edge struct { Map_ *Map `json:"map,omitempty"` References []*EdgeReference `json:"references,omitempty"` + + Inherited bool `json:"inherited"` } func (e *Edge) Copy(newParent Node) Node { @@ -915,6 +938,25 @@ func (a *Array) AST() d2ast.Node { return astArray } +func (m *Map) BaseAST() d2ast.Node { + if m == nil { + return nil + } + astMap := &d2ast.Map{} + astMap.Range = d2ast.MakeRange(",0:0:0-1:0:0") + for _, f := range m.Fields { + if !f.Inherited { + astMap.Nodes = append(astMap.Nodes, d2ast.MakeMapNodeBox(f.AST().(d2ast.MapNode))) + } + } + for _, e := range m.Edges { + if !e.Inherited { + astMap.Nodes = append(astMap.Nodes, d2ast.MakeMapNodeBox(e.AST().(d2ast.MapNode))) + } + } + return astMap +} + func (m *Map) AST() d2ast.Node { if m == nil { return nil diff --git a/d2ir/merge.go b/d2ir/merge.go index 15ba2a9ec..7cb4748aa 100644 --- a/d2ir/merge.go +++ b/d2ir/merge.go @@ -4,7 +4,9 @@ func OverlayMap(base, overlay *Map) { for _, of := range overlay.Fields { bf := base.GetField(of.Name) if bf == nil { - base.Fields = append(base.Fields, of.Copy(base).(*Field)) + f := of.Copy(base).(*Field) + f.Inherited = true + base.Fields = append(base.Fields, f) continue } OverlayField(bf, of) @@ -13,7 +15,9 @@ func OverlayMap(base, overlay *Map) { for _, oe := range overlay.Edges { bea := base.GetEdges(oe.ID) if len(bea) == 0 { - base.Edges = append(base.Edges, oe.Copy(base).(*Edge)) + e := oe.Copy(base).(*Edge) + e.Inherited = true + base.Edges = append(base.Edges, e) continue } be := bea[0]