This commit is contained in:
Alexander Wang 2023-06-18 23:57:08 -07:00
parent 14703dd991
commit 1ff119d803
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
5 changed files with 54 additions and 2 deletions

View file

@ -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 {

View file

@ -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"`

View file

@ -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{

View file

@ -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

View file

@ -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]