...

Source file src/oss.terrastruct.com/d2/d2ir/merge.go

Documentation: oss.terrastruct.com/d2/d2ir

     1  package d2ir
     2  
     3  func OverlayMap(base, overlay *Map) {
     4  	for _, of := range overlay.Fields {
     5  		bf := base.GetField(of.Name)
     6  		if bf == nil {
     7  			base.Fields = append(base.Fields, of.Copy(base).(*Field))
     8  			continue
     9  		}
    10  		OverlayField(bf, of)
    11  	}
    12  
    13  	for _, oe := range overlay.Edges {
    14  		bea := base.GetEdges(oe.ID, nil, nil)
    15  		if len(bea) == 0 {
    16  			base.Edges = append(base.Edges, oe.Copy(base).(*Edge))
    17  			continue
    18  		}
    19  		be := bea[0]
    20  		OverlayEdge(be, oe)
    21  	}
    22  }
    23  
    24  func OverlayField(bf, of *Field) {
    25  	if of.Primary_ != nil {
    26  		bf.Primary_ = of.Primary_.Copy(bf).(*Scalar)
    27  	}
    28  
    29  	if of.Composite != nil {
    30  		if bf.Map() != nil && of.Map() != nil {
    31  			OverlayMap(bf.Map(), of.Map())
    32  		} else {
    33  			bf.Composite = of.Composite.Copy(bf).(Composite)
    34  		}
    35  	}
    36  
    37  	bf.References = append(bf.References, of.References...)
    38  }
    39  
    40  func OverlayEdge(be, oe *Edge) {
    41  	if oe.Primary_ != nil {
    42  		be.Primary_ = oe.Primary_.Copy(be).(*Scalar)
    43  	}
    44  	if oe.Map_ != nil {
    45  		if be.Map_ != nil {
    46  			OverlayMap(be.Map(), oe.Map_)
    47  		} else {
    48  			be.Map_ = oe.Map_.Copy(be).(*Map)
    49  		}
    50  	}
    51  	be.References = append(be.References, oe.References...)
    52  }
    53  

View as plain text