modified compileReserved for "icon"

This commit is contained in:
melsonic 2025-03-22 15:03:37 +05:30
parent 559216d76d
commit 8c93ff8d26
No known key found for this signature in database
GPG key ID: DFA426742F621CD7
2 changed files with 26 additions and 64 deletions

View file

@ -383,24 +383,13 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
} else if isReserved { } else if isReserved {
c.compileReserved(&obj.Attributes, f) c.compileReserved(&obj.Attributes, f)
if keyword == "icon" && f.Map() != nil {
for _, ff := range f.Map().Fields {
if ff.Name.ScalarString() == "style" && ff.Name.IsUnquoted() {
if f.Map() == nil || len(f.Map().Fields) == 0 {
c.errorf(f.LastRef().AST(), `"style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`)
return
}
c.compileIconStyle(&obj.Attributes, ff.Map())
}
}
}
return return
} else if f.Name.ScalarString() == "style" && f.Name.IsUnquoted() { } else if f.Name.ScalarString() == "style" && f.Name.IsUnquoted() {
if f.Map() == nil || len(f.Map().Fields) == 0 { if f.Map() == nil || len(f.Map().Fields) == 0 {
c.errorf(f.LastRef().AST(), `"style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`) c.errorf(f.LastRef().AST(), `"style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`)
return return
} }
c.compileStyle(&obj.Attributes, f.Map()) c.compileStyle(&obj.Attributes, f.Map(), false)
return return
} }
@ -592,6 +581,17 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
} }
attrs.Icon = iconURL attrs.Icon = iconURL
c.compilePosition(attrs, f) c.compilePosition(attrs, f)
if f.Map() != nil {
for _, ff := range f.Map().Fields {
if ff.Name.ScalarString() == "style" && ff.Name.IsUnquoted() {
if ff.Map() == nil || len(ff.Map().Fields) == 0 {
c.errorf(f.LastRef().AST(), `"style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`)
return
}
c.compileStyle(attrs, ff.Map(), true)
}
}
}
case "near": case "near":
nearKey, err := d2parser.ParseKey(scalar.ScalarString()) nearKey, err := d2parser.ParseKey(scalar.ScalarString())
if err != nil { if err != nil {
@ -752,19 +752,13 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
} }
} }
func (c *compiler) compileStyle(attrs *d2graph.Attributes, m *d2ir.Map) { func (c *compiler) compileStyle(attrs *d2graph.Attributes, m *d2ir.Map, compileIconStyle bool) {
for _, f := range m.Fields { for _, f := range m.Fields {
c.compileStyleField(attrs, f) c.compileStyleField(attrs, f, compileIconStyle)
} }
} }
func (c *compiler) compileIconStyle(attrs *d2graph.Attributes, m *d2ir.Map) { func (c *compiler) compileStyleField(attrs *d2graph.Attributes, f *d2ir.Field, compileIconStyle bool) {
for _, f := range m.Fields {
c.compileIconStyleField(attrs, f)
}
}
func (c *compiler) compileStyleField(attrs *d2graph.Attributes, f *d2ir.Field) {
if _, ok := d2ast.StyleKeywords[strings.ToLower(f.Name.ScalarString())]; !(ok && f.Name.IsUnquoted()) { if _, ok := d2ast.StyleKeywords[strings.ToLower(f.Name.ScalarString())]; !(ok && f.Name.IsUnquoted()) {
c.errorf(f.LastRef().AST(), `invalid style keyword: "%s"`, f.Name.ScalarString()) c.errorf(f.LastRef().AST(), `invalid style keyword: "%s"`, f.Name.ScalarString())
return return
@ -772,28 +766,18 @@ func (c *compiler) compileStyleField(attrs *d2graph.Attributes, f *d2ir.Field) {
if f.Primary() == nil { if f.Primary() == nil {
return return
} }
compileStyleFieldInit(attrs, f)
var err error
scalar := f.Primary().Value scalar := f.Primary().Value
err := attrs.Style.Apply(f.Name.ScalarString(), scalar.ScalarString()) if compileIconStyle {
if err != nil { compileIconStyleFieldInit(attrs, f)
c.errorf(scalar, err.Error()) err = attrs.IconStyle.Apply(f.Name.ScalarString(), scalar.ScalarString())
return } else {
compileStyleFieldInit(attrs, f)
err = attrs.Style.Apply(f.Name.ScalarString(), scalar.ScalarString())
} }
}
func (c *compiler) compileIconStyleField(attrs *d2graph.Attributes, f *d2ir.Field) {
if _, ok := d2ast.StyleKeywords[strings.ToLower(f.Name.ScalarString())]; !(ok && f.Name.IsUnquoted()) {
c.errorf(f.LastRef().AST(), `invalid style keyword: "%s"`, f.Name.ScalarString())
return
}
if f.Primary() == nil {
return
}
compileIconStyleFieldInit(attrs, f)
scalar := f.Primary().Value
err := attrs.IconStyle.Apply(f.Name.ScalarString(), scalar.ScalarString())
if err != nil { if err != nil {
c.errorf(scalar, err.Error()) c.errorf(scalar, err.Error())
return return
@ -938,23 +922,12 @@ func (c *compiler) compileEdgeField(edge *d2graph.Edge, f *d2ir.Field) {
_, isReserved := d2ast.SimpleReservedKeywords[keyword] _, isReserved := d2ast.SimpleReservedKeywords[keyword]
if isReserved { if isReserved {
c.compileReserved(&edge.Attributes, f) c.compileReserved(&edge.Attributes, f)
if keyword == "icon" && f.Map() != nil {
for _, ff := range f.Map().Fields {
if ff.Name.ScalarString() == "style" && ff.Name.IsUnquoted() {
if f.Map() == nil || len(f.Map().Fields) == 0 {
c.errorf(f.LastRef().AST(), `"style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`)
return
}
c.compileIconStyle(&edge.Attributes, ff.Map())
}
}
}
return return
} else if f.Name.ScalarString() == "style" { } else if f.Name.ScalarString() == "style" {
if f.Map() == nil { if f.Map() == nil {
return return
} }
c.compileStyle(&edge.Attributes, f.Map()) c.compileStyle(&edge.Attributes, f.Map(), false)
return return
} }
@ -988,23 +961,12 @@ func (c *compiler) compileArrowheads(edge *d2graph.Edge, f *d2ir.Field) {
isReserved = isReserved && f2.Name.IsUnquoted() isReserved = isReserved && f2.Name.IsUnquoted()
if isReserved { if isReserved {
c.compileReserved(attrs, f2) c.compileReserved(attrs, f2)
if keyword == "icon" && f.Map() != nil {
for _, ff := range f.Map().Fields {
if ff.Name.ScalarString() == "style" && ff.Name.IsUnquoted() {
if f.Map() == nil || len(f.Map().Fields) == 0 {
c.errorf(f.LastRef().AST(), `"style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`)
return
}
c.compileIconStyle(attrs, ff.Map())
}
}
}
continue continue
} else if f2.Name.ScalarString() == "style" && f2.Name.IsUnquoted() { } else if f2.Name.ScalarString() == "style" && f2.Name.IsUnquoted() {
if f2.Map() == nil { if f2.Map() == nil {
continue continue
} }
c.compileStyle(attrs, f2.Map()) c.compileStyle(attrs, f2.Map(), false)
continue continue
} else { } else {
c.errorf(f2.LastRef().AST(), `source-arrowhead/target-arrowhead map keys must be reserved keywords`) c.errorf(f2.LastRef().AST(), `source-arrowhead/target-arrowhead map keys must be reserved keywords`)

View file

@ -198,7 +198,7 @@ type Attributes struct {
Style Style `json:"style"` Style Style `json:"style"`
Icon *url.URL `json:"icon,omitempty"` Icon *url.URL `json:"icon,omitempty"`
IconStyle Style `json:"iconStyle,omitempty"` IconStyle Style `json:"iconStyle"`
Tooltip *Scalar `json:"tooltip,omitempty"` Tooltip *Scalar `json:"tooltip,omitempty"`
Link *Scalar `json:"link,omitempty"` Link *Scalar `json:"link,omitempty"`