d2

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ClassTemplate = NewTemplate("Class", `
"{{- $.Name }}": {
  shape: class
{{- range $property := $.Properties }}
  "{{ $property.Name }}": "{{ $property.Type }}"
{{- end }}
}`)
View Source
var ContainerTemplate = NewTemplate("Container", `
{{- if $.Name -}}
"{{ $.Name }}": {
{{- range $container := $.Containers -}}
{{ $container.Render | indent 4 }}

{{ end -}}
{{- range $class := $.Classes }}
{{ class $class | indent 4 }}
{{ end -}}
}
{{- else -}}
{{- range $container := $.Containers -}}
{{ $container.Render }}

{{ end -}}
{{- range $class := $.Classes }}
{{ class $class }}
{{ end -}}
{{- end }}`, template.FuncMap{
	"class": RenderClass,
	"indent": func(amount int, input string) string {
		lines := strings.Split(input, "\n")
		var builder strings.Builder
		for i, line := range lines {
			builder.WriteString(strings.Repeat(" ", amount) + line)
			if i != len(lines)-1 {
				builder.WriteString("\n")
			}
		}

		return builder.String()
	},
})
View Source
var ErrNoClasses = errors.New("no classes parsed")

ErrNoClasses is returned when no Classes are parsed

View Source
var ErrParserFailure = errors.New("failed to parse classes or relations")

ErrParserFailure is returned when there is some failure by the parser

View Source
var ErrUnknownFormat = errors.New("unknown format")

ErrUnknownFormat is returned when the supplied format is not recognized

View Source
var RelationTemplate = NewTemplate("Relation", `
{{- $.From.Name }} -- {{ $.To.Name }}: "{{ $.Type | safe }}"`)

Functions

func D2

func D2(parser Parser, cfg *Config) ([]byte, error)

D2 transform Parser with Config into .d2 or .svg output

func NewTemplate

func NewTemplate(name, input string, funcs ...template.FuncMap) *template.Template

NewTemplate from name, input

func RenderClass

func RenderClass(class *domain.Class) string

RenderClass to string output

func RenderContainer

func RenderContainer(container *Container) string

RenderContainer to string output

func RenderRelation

func RenderRelation(relation *domain.Relation) string

RenderRelation to string output

Types

type Config

type Config struct {
	// Format used when outputting diagram
	Format Format

	// Tool used to render svg (if SVG Format)
	Tool string

	// Args used by the tool
	Args []string

	// ContainerBasePath if set will wrap all domain.Class in containers based on the
	// DirContainerParser
	ContainerBasePath string
}

Config used when rendering D2

type Container

type Container struct {
	// Name of the container
	Name string

	// Classes in the container
	Classes []*domain.Class

	// Containers nested in this Container
	Containers Containers
}

Container to put domain.Class in as a grouping

func (*Container) Add

func (c *Container) Add(class *domain.Class, parser ContainerParser)

Add a domain.Class to the Container by parsing its container names using a ContainerParser

func (*Container) Render

func (c *Container) Render() string

Render a Container using RenderContainer. This avoids the circular dependency problem my adding the RenderContainer as a function to the ContainerTemplate

type ContainerParser

type ContainerParser interface {
	Containers(source domain.Source) []string
}

ContainerParser derives the nesting of containers for a particular domain.Source e.g. if 'some,nested,value' is returned the eventual class is rendered inside a three layer deep Container

type Containers

type Containers []*Container

Containers is a helper type to perform methods on a group of Container

func (Containers) Contains

func (c Containers) Contains(name string) (*Container, bool)

Contains a Container with Container.Name returns true and the Container if found or false otherwise

type DirContainerParser

type DirContainerParser struct {
	// RootPath to remove from the domain.Source to determine directory containers
	RootPath string
}

DirContainerParser uses a base directory to parse containers by stripping the RootPath from the domain.Source of the domain.Class and using the directory representation as containers

func (DirContainerParser) Containers

func (d DirContainerParser) Containers(source domain.Source) []string

Containers separated by the filepath.Separator in the filesystem relative to some RootPath if the source contains the file:// protocol, it's removed automatically

type Format

type Format string

Format of output string supported by D2

const Native Format = "d2"

Native D2 script format

const PNG Format = "png"

PNG format

const SVG Format = "svg"

SVG format

func FormatFromFile

func FormatFromFile(path string) (Format, error)

FormatFromFile parses the given path and determines the output format used by the D2 parser

func (Format) Render

func (f Format) Render(buffer *bytes.Buffer, cfg *Config) ([]byte, error)

Render Format to a byte slice

type Parser

type Parser interface {
	Classes() ([]*domain.Class, error)
	Relations() ([]*domain.Relation, error)
}

Parser implementation that returns a parse.Class slice

Jump to

Keyboard shortcuts

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