2023-02-19 05:51:55 +00:00
package d2plugin
import (
"fmt"
"oss.terrastruct.com/d2/d2graph"
)
type PluginFeature string
// When this is true, objects can set ther `near` key to another object
// When this is false, objects can only set `near` to constants
const NEAR_OBJECT PluginFeature = "near_object"
// When this is true, containers can have dimensions set
const CONTAINER_DIMENSIONS PluginFeature = "container_dimensions"
// When this is true, objects can specify their `top` and `left` keywords
const TOP_LEFT PluginFeature = "top_left"
func FeatureSupportCheck ( info * PluginInfo , g * d2graph . Graph ) error {
// Older version of plugin. Skip checking.
if info . Features == nil {
return nil
}
2023-02-20 17:05:00 +00:00
featureMap := make ( map [ PluginFeature ] struct { } , len ( info . Features ) )
for _ , f := range info . Features {
featureMap [ f ] = struct { } { }
}
2023-02-19 05:51:55 +00:00
for _ , obj := range g . Objects {
if obj . Attributes . Top != nil || obj . Attributes . Left != nil {
2023-02-20 17:05:00 +00:00
if _ , ok := featureMap [ TOP_LEFT ] ; ! ok {
2023-02-19 05:51:55 +00:00
return fmt . Errorf ( ` Object "%s" has attribute "top" and/or "left" set, but layout engine "%s" does not support locked positions. ` , obj . AbsID ( ) , info . Name )
}
}
if ( obj . Attributes . Width != nil || obj . Attributes . Height != nil ) && len ( obj . ChildrenArray ) > 0 {
2023-02-20 17:05:00 +00:00
if _ , ok := featureMap [ CONTAINER_DIMENSIONS ] ; ! ok {
2023-02-19 05:51:55 +00:00
return fmt . Errorf ( ` Object "%s" has attribute "width" and/or "height" set, but layout engine "%s" does not support dimensions set on containers. ` , obj . AbsID ( ) , info . Name )
}
}
if obj . Attributes . NearKey != nil {
_ , isKey := g . Root . HasChild ( d2graph . Key ( obj . Attributes . NearKey ) )
if isKey {
2023-02-20 17:05:00 +00:00
if _ , ok := featureMap [ NEAR_OBJECT ] ; ! ok {
2023-02-19 05:51:55 +00:00
return fmt . Errorf ( ` Object "%s" has "near" set to another object, but layout engine "%s" only supports constant values for "near". ` , obj . AbsID ( ) , info . Name )
}
}
}
}
return nil
}