d2graph

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2023 License: MPL-2.0 Imports: 18 Imported by: 43

Documentation

Index

Constants

View Source
const DEFAULT_SHAPE_PADDING = 100.
View Source
const INNER_LABEL_PADDING int = 5

Variables

View Source
var NearConstants map[string]struct{}
View Source
var NearConstantsArray = []string{
	"top-left",
	"top-center",
	"top-right",

	"center-left",
	"center-right",

	"bottom-left",
	"bottom-center",
	"bottom-right",
}

TODO maybe autofmt should allow other values, and transform them to conform e.g. left-center becomes center-left

View Source
var ReservedKeywordHolders = map[string]struct{}{
	"style":            {},
	"source-arrowhead": {},
	"target-arrowhead": {},
}

ReservedKeywordHolders are reserved keywords that are meaningless on its own and exist solely to hold a set of reserved keywords

View Source
var ReservedKeywords = map[string]struct{}{
	"label":      {},
	"desc":       {},
	"shape":      {},
	"icon":       {},
	"constraint": {},
	"tooltip":    {},
	"link":       {},
	"near":       {},
	"width":      {},
	"height":     {},
	"direction":  {},
}
View Source
var StyleKeywords = map[string]struct{}{
	"opacity":       {},
	"stroke":        {},
	"fill":          {},
	"stroke-width":  {},
	"stroke-dash":   {},
	"border-radius": {},

	"font":       {},
	"font-size":  {},
	"font-color": {},
	"bold":       {},
	"italic":     {},
	"underline":  {},

	"shadow":   {},
	"multiple": {},

	"3d": {},

	"animated": {},
	"filled":   {},
}

StyleKeywords are reserved keywords which cannot exist outside of the "style" keyword

Functions

func DeserializeGraph

func DeserializeGraph(bytes []byte, g *Graph) error

func GetTextDimensions added in v0.1.4

func GetTextDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler, t *d2target.MText, fontFamily *d2fonts.FontFamily) *d2target.TextDimensions

func Key

func Key(k *d2ast.KeyPath) []string

func SerializeGraph

func SerializeGraph(g *Graph) ([]byte, error)

Types

type Attributes

type Attributes struct {
	Label   Scalar   `json:"label"`
	Style   Style    `json:"style"`
	Icon    *url.URL `json:"icon,omitempty"`
	Tooltip string   `json:"tooltip,omitempty"`
	Link    string   `json:"link,omitempty"`

	// Only applicable for images right now
	Width  *Scalar `json:"width,omitempty"`
	Height *Scalar `json:"height,omitempty"`

	// TODO consider separate Attributes struct for shape-specific and edge-specific
	// Shapes only
	NearKey  *d2ast.KeyPath `json:"near_key"`
	Language string         `json:"language,omitempty"`
	// TODO: default to ShapeRectangle instead of empty string
	Shape Scalar `json:"shape"`

	Direction Scalar `json:"direction"`
}

type ContainerLevel

type ContainerLevel int

func (ContainerLevel) LabelSize

func (l ContainerLevel) LabelSize() int

type Edge

type Edge struct {
	Index int `json:"index"`

	MinWidth  int `json:"minWidth"`
	MinHeight int `json:"minHeight"`

	SrcTableColumnIndex *int `json:"srcTableColumnIndex,omitempty"`
	DstTableColumnIndex *int `json:"dstTableColumnIndex,omitempty"`

	LabelDimensions d2target.TextDimensions `json:"label_dimensions"`
	LabelPosition   *string                 `json:"labelPosition,omitempty"`
	LabelPercentage *float64                `json:"labelPercentage,omitempty"`

	IsCurve bool         `json:"isCurve"`
	Route   []*geo.Point `json:"route,omitempty"`

	Src          *Object     `json:"-"`
	SrcArrow     bool        `json:"src_arrow"`
	SrcArrowhead *Attributes `json:"srcArrowhead,omitempty"`
	Dst          *Object     `json:"-"`
	// TODO alixander (Mon Sep 12 2022): deprecate SrcArrow and DstArrow and just use SrcArrowhead and DstArrowhead
	DstArrow     bool        `json:"dst_arrow"`
	DstArrowhead *Attributes `json:"dstArrowhead,omitempty"`

	References []EdgeReference `json:"references,omitempty"`
	Attributes Attributes      `json:"attributes"`

	ZIndex int `json:"zIndex"`
}

func (*Edge) AbsID

func (e *Edge) AbsID() string

func (*Edge) ArrowString

func (e *Edge) ArrowString() string

func (*Edge) ContainedBy added in v0.1.0

func (e *Edge) ContainedBy(obj *Object) bool

func (*Edge) GetStroke

func (e *Edge) GetStroke(theme *d2themes.Theme, dashGapSize interface{}) string

func (*Edge) Text

func (e *Edge) Text() *d2target.MText

type EdgeReference

type EdgeReference struct {
	Edge *d2ast.Edge `json:"-"`

	MapKey          *d2ast.Key `json:"-"`
	MapKeyEdgeIndex int        `json:"map_key_edge_index"`
	Scope           *d2ast.Map `json:"-"`
	ScopeObj        *Object    `json:"-"`
}

type Graph

type Graph struct {
	AST *d2ast.Map `json:"ast"`

	Root    *Object   `json:"root"`
	Edges   []*Edge   `json:"edges"`
	Objects []*Object `json:"objects"`
}

TODO: Refactor with a light abstract layer on top of AST implementing scenarios, variables, imports, substitutions and then a final set of structures representing a final graph.

func NewGraph

func NewGraph(ast *d2ast.Map) *Graph

func (*Graph) SetDimensions

func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler, fontFamily *d2fonts.FontFamily) error

func (*Graph) Texts

func (g *Graph) Texts() []*d2target.MText

type Object

type Object struct {
	Graph  *Graph  `json:"-"`
	Parent *Object `json:"-"`

	// IDVal is the actual value of the ID whereas ID is the value in d2 syntax.
	// e.g. ID:    "yes'\""
	//      IDVal: yes'"
	//
	// ID allows joining on . naively and construct a valid D2 key path
	ID              string                  `json:"id"`
	IDVal           string                  `json:"id_val"`
	Map             *d2ast.Map              `json:"-"`
	LabelDimensions d2target.TextDimensions `json:"label_dimensions"`
	References      []Reference             `json:"references,omitempty"`

	*geo.Box      `json:"box,omitempty"`
	LabelPosition *string `json:"labelPosition,omitempty"`
	LabelWidth    *int    `json:"labelWidth,omitempty"`
	LabelHeight   *int    `json:"labelHeight,omitempty"`
	IconPosition  *string `json:"iconPosition,omitempty"`

	Class    *d2target.Class    `json:"class,omitempty"`
	SQLTable *d2target.SQLTable `json:"sql_table,omitempty"`

	Children      map[string]*Object `json:"-"`
	ChildrenArray []*Object          `json:"-"`

	Attributes Attributes `json:"attributes"`

	ZIndex int `json:"zIndex"`
}

TODO maybe rename to Shape

func ResolveUnderscoreKey

func ResolveUnderscoreKey(ida []string, obj *Object) (resolvedObj *Object, resolvedIDA []string, _ error)

func (*Object) AbsID

func (obj *Object) AbsID() string

func (*Object) AbsIDArray

func (obj *Object) AbsIDArray() []string

func (*Object) AppendReferences

func (obj *Object) AppendReferences(ida []string, ref Reference, unresolvedObj *Object)

func (*Object) Connect

func (obj *Object) Connect(srcID, dstID []string, srcArrow, dstArrow bool, label string) (*Edge, error)

func (*Object) ContainedBy added in v0.1.0

func (o *Object) ContainedBy(obj *Object) bool

func (*Object) ContainsAnyEdge added in v0.1.0

func (obj *Object) ContainsAnyEdge(edges []*Edge) bool

func (*Object) ContainsAnyObject added in v0.1.0

func (obj *Object) ContainsAnyObject(objects []*Object) bool

func (*Object) EnsureChild

func (obj *Object) EnsureChild(ids []string) *Object

EnsureChild grabs the child by ids or creates it if it does not exist including all intermediate nodes.

func (*Object) FindEdges

func (obj *Object) FindEdges(mk *d2ast.Key) ([]*Edge, bool)

TODO: remove edges []edge and scope each edge inside Object.

func (*Object) GetDefaultSize added in v0.1.4

func (obj *Object) GetDefaultSize(mtexts []*d2target.MText, ruler *textmeasure.Ruler, fontFamily *d2fonts.FontFamily, labelDims d2target.TextDimensions) (*d2target.TextDimensions, error)

func (*Object) GetFill

func (obj *Object) GetFill(theme *d2themes.Theme) string

func (*Object) GetLabelSize added in v0.1.4

func (obj *Object) GetLabelSize(mtexts []*d2target.MText, ruler *textmeasure.Ruler, fontFamily *d2fonts.FontFamily) (*d2target.TextDimensions, error)

func (*Object) GetPadding added in v0.1.4

func (obj *Object) GetPadding() (x, y float64)

func (*Object) GetStroke

func (obj *Object) GetStroke(theme *d2themes.Theme, dashGapSize interface{}) string

func (*Object) HasChild

func (obj *Object) HasChild(ids []string) (*Object, bool)

func (*Object) HasEdge

func (obj *Object) HasEdge(mk *d2ast.Key) (*Edge, bool)

func (*Object) IsContainer

func (obj *Object) IsContainer() bool

func (*Object) IsSequenceDiagram added in v0.1.0

func (obj *Object) IsSequenceDiagram() bool

func (*Object) IsSequenceDiagramGroup added in v0.1.0

func (obj *Object) IsSequenceDiagramGroup() bool

groups are objects in sequence diagrams that have no messages connected and does not have a note as a child (a note can appear within a group, but it's a child of an actor)

func (*Object) IsSequenceDiagramNote added in v0.1.0

func (obj *Object) IsSequenceDiagramNote() bool

notes are descendant of actors with no edges and no children

func (*Object) Level

func (obj *Object) Level() ContainerLevel

func (*Object) OuterSequenceDiagram added in v0.1.0

func (obj *Object) OuterSequenceDiagram() *Object

func (*Object) Text

func (obj *Object) Text() *d2target.MText

type Reference

type Reference struct {
	Key          *d2ast.KeyPath `json:"key"`
	KeyPathIndex int            `json:"key_path_index"`

	MapKey          *d2ast.Key `json:"-"`
	MapKeyEdgeIndex int        `json:"map_key_edge_index"`
	Scope           *d2ast.Map `json:"-"`
	// The ScopeObj and UnresolvedScopeObj are the same except when the key contains underscores
	ScopeObj           *Object `json:"-"`
	UnresolvedScopeObj *Object `json:"-"`
}

TODO references at the root scope should have their Scope set to root graph AST

func (Reference) InEdge

func (r Reference) InEdge() bool

func (Reference) MapKeyEdgeDest

func (r Reference) MapKeyEdgeDest() bool

type Scalar

type Scalar struct {
	Value  string     `json:"value"`
	MapKey *d2ast.Key `json:"-"`
}

TODO consider having different Scalar types Right now we'll hold any types in Value and just convert, e.g. floats

type SerializedEdge

type SerializedEdge map[string]interface{}

type SerializedGraph

type SerializedGraph struct {
	Root    SerializedObject   `json:"root"`
	Edges   []SerializedEdge   `json:"edges"`
	Objects []SerializedObject `json:"objects"`
}

type SerializedObject

type SerializedObject map[string]interface{}

type Style

type Style struct {
	Opacity      *Scalar `json:"opacity,omitempty"`
	Stroke       *Scalar `json:"stroke,omitempty"`
	Fill         *Scalar `json:"fill,omitempty"`
	StrokeWidth  *Scalar `json:"strokeWidth,omitempty"`
	StrokeDash   *Scalar `json:"strokeDash,omitempty"`
	BorderRadius *Scalar `json:"borderRadius,omitempty"`
	Shadow       *Scalar `json:"shadow,omitempty"`
	ThreeDee     *Scalar `json:"3d,omitempty"`
	Multiple     *Scalar `json:"multiple,omitempty"`
	Font         *Scalar `json:"font,omitempty"`
	FontSize     *Scalar `json:"fontSize,omitempty"`
	FontColor    *Scalar `json:"fontColor,omitempty"`
	Animated     *Scalar `json:"animated,omitempty"`
	Bold         *Scalar `json:"bold,omitempty"`
	Italic       *Scalar `json:"italic,omitempty"`
	Underline    *Scalar `json:"underline,omitempty"`
	Filled       *Scalar `json:"filled,omitempty"`
}

func (*Style) Apply

func (s *Style) Apply(key, value string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL