d2ir: Review fixes #714

This commit is contained in:
Anmol Sethi 2023-01-27 17:19:55 -08:00
parent f056700152
commit 192cb10a61
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
3 changed files with 0 additions and 267 deletions

View file

@ -1,178 +0,0 @@
package d2graph
func (s *Scalar) Copy() *Scalar {
if s == nil {
return nil
}
tmp := *s
return &tmp
}
func (s Style) Copy() Style {
return Style{
Opacity: s.Opacity.Copy(),
Stroke: s.Stroke.Copy(),
Fill: s.Fill.Copy(),
StrokeWidth: s.StrokeWidth.Copy(),
StrokeDash: s.StrokeDash.Copy(),
BorderRadius: s.BorderRadius.Copy(),
Shadow: s.Shadow.Copy(),
ThreeDee: s.ThreeDee.Copy(),
Multiple: s.Multiple.Copy(),
Font: s.Font.Copy(),
FontSize: s.FontSize.Copy(),
FontColor: s.FontColor.Copy(),
Animated: s.Animated.Copy(),
Bold: s.Bold.Copy(),
Italic: s.Italic.Copy(),
Underline: s.Underline.Copy(),
Filled: s.Filled.Copy(),
DoubleBorder: s.DoubleBorder.Copy(),
}
}
func (attrs *Attributes) Copy() *Attributes {
if attrs == nil {
return nil
}
return &Attributes{
Label: attrs.Label,
Style: attrs.Style.Copy(),
Icon: attrs.Icon,
Tooltip: attrs.Tooltip,
Link: attrs.Link,
Width: attrs.Width.Copy(),
Height: attrs.Height.Copy(),
NearKey: attrs.NearKey,
Language: attrs.Language,
Shape: attrs.Shape,
Direction: attrs.Direction,
Constraint: attrs.Constraint,
}
}
func (o *Object) Copy() *Object {
return &Object{
Graph: o.Graph,
Parent: o.Parent,
ID: o.ID,
IDVal: o.IDVal,
Map: o.Map,
LabelDimensions: o.LabelDimensions,
References: append([]Reference(nil), o.References...),
Box: o.Box.Copy(),
LabelPosition: o.LabelPosition,
LabelWidth: o.LabelHeight,
LabelHeight: o.LabelHeight,
IconPosition: o.IconPosition,
Class: o.Class.Copy(),
SQLTable: o.SQLTable.Copy(),
Children: copyChildrenMap(o.Children),
ChildrenArray: append([]*Object(nil), o.ChildrenArray...),
Attributes: o.Attributes.Copy(),
ZIndex: o.ZIndex,
}
}
func copyChildrenMap(children map[string]*Object) map[string]*Object {
children2 := make(map[string]*Object, len(children))
for id, ch := range children {
children2[id] = ch
}
return children2
}
func (e *Edge) Copy() *Edge {
return &Edge{
Index: e.Index,
MinWidth: e.MinWidth,
MinHeight: e.MinHeight,
SrcTableColumnIndex: e.SrcTableColumnIndex,
DstTableColumnIndex: e.DstTableColumnIndex,
LabelDimensions: e.LabelDimensions,
LabelPosition: e.LabelPosition,
LabelPercentage: e.LabelPercentage,
IsCurve: e.IsCurve,
Route: e.Route,
Src: e.Src,
SrcArrow: e.SrcArrow,
SrcArrowhead: e.SrcArrowhead.Copy(),
Dst: e.Dst,
DstArrow: e.DstArrow,
DstArrowhead: e.DstArrowhead.Copy(),
References: append([]EdgeReference(nil), e.References...),
Attributes: e.Attributes.Copy(),
ZIndex: e.ZIndex,
}
}
// Copy copies for use as the base of a step or scenario.
func (g *Graph) Copy() *Graph {
g2 := &Graph{
AST: g.AST,
Root: g.Root.Copy(),
}
absIDMap := make(map[string]*Object, len(g.Objects))
for _, o := range g.Objects {
o2 := o.Copy()
g2.Objects = append(g2.Objects, o2)
absIDMap[o.AbsID()] = o2
}
updateObjectPointers := func(o2 *Object) {
o2.Graph = g2
if o2.Parent != nil {
if o2.Parent.Parent == nil {
o2.Parent = g2.Root
} else {
o2.Parent = absIDMap[o2.Parent.AbsID()]
}
}
for i, ref := range o2.References {
o2.References[i].ScopeObj = absIDMap[ref.ScopeObj.AbsID()]
}
for id, ch := range o2.Children {
o2.Children[id] = absIDMap[ch.AbsID()]
}
for i, ch := range o2.ChildrenArray {
o2.ChildrenArray[i] = absIDMap[ch.AbsID()]
}
}
updateObjectPointers(g2.Root)
for _, o2 := range g2.Objects {
updateObjectPointers(o2)
}
for _, e := range g.Edges {
g2.Edges = append(g2.Edges, e.Copy())
}
for _, e2 := range g2.Edges {
e2.Src = absIDMap[e2.Src.AbsID()]
e2.Dst = absIDMap[e2.Dst.AbsID()]
for i, ref := range e2.References {
e2.References[i].ScopeObj = absIDMap[ref.ScopeObj.AbsID()]
}
}
return g2
}

View file

@ -1,86 +0,0 @@
package d2graph_test
import (
"strings"
"testing"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/d2/d2compiler"
"oss.terrastruct.com/d2/d2graph"
)
func TestCopy(t *testing.T) {
t.Parallel()
tca := []struct {
name string
d2str string
assert func(t *testing.T, g, g2 *d2graph.Graph)
}{
{
name: `objects`,
d2str: `a
b
c
d`,
assert: func(t *testing.T, g, g2 *d2graph.Graph) {
g2.Root.IDVal = `jingleberry`
assert.String(t, ``, g.Root.IDVal)
g2.Root.ChildrenArray[0].IDVal = `saltedmeat`
assert.String(t, `a`, g.Root.ChildrenArray[0].IDVal)
assert.NotEqual(t,
g.Root.ChildrenArray[0].References[0].ScopeObj,
g2.Root.ChildrenArray[0].References[0].ScopeObj,
)
},
},
{
name: `edges`,
d2str: `a -> b
b -> c
c -> d
d -> a`,
assert: func(t *testing.T, g, g2 *d2graph.Graph) {
g2.Edges[0].DstArrow = false
assert.Equal(t, true, g.Edges[0].DstArrow)
assert.NotEqual(t,
g.Edges[0].References[0].ScopeObj,
g2.Edges[0].References[0].ScopeObj,
)
},
},
{
name: `nested`,
d2str: `a.b -> c.d`,
assert: func(t *testing.T, g, g2 *d2graph.Graph) {
g2.Root.ChildrenArray[0].ChildrenArray[0].IDVal = `saltedmeat`
assert.String(t, `b`, g.Root.ChildrenArray[0].ChildrenArray[0].IDVal)
assert.NotEqual(t,
g.Root.ChildrenArray[0].ChildrenArray[0].References[0].ScopeObj,
g2.Root.ChildrenArray[0].ChildrenArray[0].References[0].ScopeObj,
)
g2.Edges[0].DstArrow = false
assert.Equal(t, true, g.Edges[0].DstArrow)
assert.NotEqual(t,
g.Edges[0].References[0].ScopeObj,
g2.Edges[0].References[0].ScopeObj,
)
},
},
}
for _, tc := range tca {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
g, err := d2compiler.Compile("", strings.NewReader(tc.d2str), nil)
assert.Success(t, err)
g2 := g.Copy()
tc.assert(t, g, g2)
})
}
}

View file

@ -56,7 +56,6 @@ type Scalar struct {
}
// TODO maybe rename to Shape
// reminder: When adding new fields remember to update copy.go
type Object struct {
Graph *Graph `json:"-"`
Parent *Object `json:"-"`
@ -89,7 +88,6 @@ type Object struct {
ZIndex int `json:"zIndex"`
}
// reminder: When adding new fields remember to update copy.go
type Attributes struct {
Label Scalar `json:"label"`
Style Style `json:"style"`
@ -131,7 +129,6 @@ func (r Reference) InEdge() bool {
return r.Key != r.MapKey.Key
}
// reminder: When adding new fields remember to update copy.go
type Style struct {
Opacity *Scalar `json:"opacity,omitempty"`
Stroke *Scalar `json:"stroke,omitempty"`