Skip to content

Commit

Permalink
Merge pull request #1462 from alixander/portal
Browse files Browse the repository at this point in the history
compiler: portal keyword
  • Loading branch information
alixander authored Jul 1, 2023
2 parents e2b76ab + 92cfc5c commit 49eabd0
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 0 deletions.
33 changes: 33 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,36 @@ func (c *compiler) compilePosition(attrs *d2graph.Attributes, f *d2ir.Field) {
}
}

func (c *compiler) compilePortal(attrs *d2graph.Attributes, f *d2ir.Field) {
if f.Map() != nil {
for _, f := range f.Map().Fields {
if f.Name == "portal" {
if f.Primary() == nil {
c.errorf(f.LastPrimaryKey(), `invalid "portal" field`)
} else {
scalar := f.Primary().Value
b, err := strconv.ParseBool(strings.ToLower(scalar.ScalarString()))
if err != nil {
c.errorf(f.LastPrimaryKey(), `invalid "portal" field`)
}
if b {
attrs.Portal = &d2graph.Scalar{}
attrs.Portal.Value = scalar.ScalarString()
attrs.Portal.MapKey = f.LastPrimaryKey()
}
}
} else {
if f.LastPrimaryKey() != nil {
c.errorf(f.LastPrimaryKey(), `unexpected field %s`, f.Name)
}
}
}
if len(f.Map().Edges) > 0 {
c.errorf(f.LastPrimaryKey(), "unexpected edges in map")
}
}
}

func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
if f.Primary() == nil {
if f.Composite != nil {
Expand All @@ -442,6 +472,8 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
}
case "label", "icon":
c.compilePosition(attrs, f)
case "link":
c.compilePortal(attrs, f)
default:
c.errorf(f.LastPrimaryKey(), "reserved field %v does not accept composite", f.Name)
}
Expand Down Expand Up @@ -534,6 +566,7 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Link = &d2graph.Scalar{}
attrs.Link.Value = scalar.ScalarString()
attrs.Link.MapKey = f.LastPrimaryKey()
c.compilePortal(attrs, f)
case "direction":
dirs := []string{"up", "down", "right", "left"}
if !go2.Contains(dirs, scalar.ScalarString()) {
Expand Down
19 changes: 19 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,25 @@ object: {
`,
expErr: `d2/testdata/d2compiler/TestCompile/reserved-composite.d2:1:1: reserved field shape does not accept composite`,
},
{
name: "portal",

text: `x: {
link: layers.y {
portal: true
}
}
layers: {
y: {
a
}
}
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "true", g.Objects[0].Attributes.Portal.Value)
},
},
}

for _, tc := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ type Attributes struct {
LabelPosition *Scalar `json:"labelPosition,omitempty"`
IconPosition *Scalar `json:"iconPosition,omitempty"`

Portal *Scalar `json:"portal,omitempty"`

// These names are attached to the rendered elements in SVG
// so that users can target them however they like outside of D2
Classes []string `json:"classes,omitempty"`
Expand Down Expand Up @@ -1666,6 +1668,7 @@ var CompositeReservedKeywords = map[string]struct{}{
"constraint": {},
"label": {},
"icon": {},
"link": {},
}

// StyleKeywords are reserved keywords which cannot exist outside of the "style" keyword
Expand Down
Loading

0 comments on commit 49eabd0

Please sign in to comment.