Help us shape the future of Go! Take the 2026 Go Developer Survey

graph

package
v1.27.2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package graph provides types and functions for constructing graph structures from C4 architecture views.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeBackLinkURL

func ComputeBackLinkURL(currentPath, basename, _ string) string

ComputeBackLinkURL calculates the relative path from current diagram back to parent. currentPath is the dotted path of the current diagram. basename is the output base filename. format is the output format being generated; it is IGNORED — clickable navigation URLs always use ".svg" for browser navigation regardless of the render format (matching ComputeExploreURL), so a .dot diagram still links to the browser-navigable .svg sibling. The parameter is retained to avoid rippling signature changes through the builder call sites.

func ComputeExploreURL

func ComputeExploreURL(currentPath, targetPath, basename, _ string) string

ComputeExploreURL calculates the relative path from current diagram to target. currentPath is the dotted path of the current diagram (empty for C1). targetPath is the dotted path of the target unit (a FULL dotted path such as "mainSystem.sshAuth" because BuildGraphWithPath passes node.ID). basename is the output base filename (from TOML file). format is the output format (e.g., "svg") - ignored; URLs always use "svg" for browser navigation regardless of the format being generated.

File layout under {basename}/:

C1 for ""               -> {basename}.svg           (dir: .)
C2 for "mainapp"        -> mainapp.svg              (dir: .)
C3 for "mainapp.api"    -> mainapp/api.svg          (dir: mainapp/)
C4 for "mainapp.api.v2" -> mainapp/api/v2.svg       (dir: mainapp/api/)

The current file's directory (all segments but the last) and the target file's directory (all segments but the last) are compared to compute a bidirectional relative URL that handles ancestor, sibling, and descendant targets uniformly. Returns the empty string when targetPath is empty or equals currentPath (self-link / empty-target guard — see Gap 1 symptom B, WR-02) so the renderer omits the URL rather than emitting a broken ".svg".

func HasExternalSubunits

func HasExternalSubunits(unit *model.Unit) bool

HasExternalSubunits returns true if the unit has any external subunits. Returns false if unit is nil or has no subunits.

func IconForType

func IconForType(t model.UnitType) string

IconForType returns the emoji icon for a unit type. Per CONTEXT.md design:

  • Person: U+1F464 (person emoji)
  • DB: U+26C1 (flag in hole, used as cylinder proxy)
  • Queue: no icon — queues are drawn as a horizontal pipe by the SVG post-processor (internal/render/pipe.go), so no text-bar cue is emitted.

func IsBoxType

func IsBoxType(t model.UnitType) bool

IsBoxType returns true if the type is a box variant (box, containerBox, componentBox).

func IsComponentType

func IsComponentType(t model.UnitType) bool

IsComponentType returns true if the type is a component or componentBox type.

func IsContainerType

func IsContainerType(t model.UnitType) bool

IsContainerType returns true if the type is a container or containerBox type.

func IsDbType

func IsDbType(t model.UnitType) bool

IsDbType returns true if the type is a database type (any level).

func IsExternalType

func IsExternalType(t model.UnitType) bool

IsExternalType returns true if the type is an external variant.

func IsPersonType

func IsPersonType(t model.UnitType) bool

IsPersonType returns true if the type is a person type (internal or external).

func IsQueueType

func IsQueueType(t model.UnitType) bool

IsQueueType returns true if the type is a queue type (any level).

func IsSystemType

func IsSystemType(t model.UnitType) bool

IsSystemType returns true if the type is a system type.

func LevelForType

func LevelForType(t model.UnitType) int

LevelForType returns the C4 level (1, 2, or 3) for a unit type.

func URLEncodePath

func URLEncodePath(path string) string

URLEncodePath URL-encodes a path segment for use in URLs.

Types

type ArrowDirection

type ArrowDirection string

ArrowDirection represents the direction of an arrow on an edge.

const (
	// ArrowForward indicates an arrow at the target end.
	ArrowForward ArrowDirection = "forward"
	// ArrowReverse indicates an arrow at the source end.
	ArrowReverse ArrowDirection = "reverse"
	// ArrowBoth indicates arrows at both ends.
	ArrowBoth ArrowDirection = "both"
	// ArrowNone indicates no arrow on the edge.
	ArrowNone ArrowDirection = "none"
)
type BackLink struct {
	// Name is the display name for the parent.
	Name string
	// URL is the relative path to the parent diagram.
	URL string
}

BackLink represents a link back to the parent diagram.

type BreadcrumbItem struct {
	// Name is the display name.
	Name string
	// URL is the relative path (empty for current level).
	URL string
}

BreadcrumbItem represents one item in the breadcrumb trail.

func BuildBreadcrumbPath

func BuildBreadcrumbPath(dottedPath, basename, format string) []BreadcrumbItem

BuildBreadcrumbPath creates breadcrumb items from a dotted path. The last item has empty URL (current level).

type Cluster

type Cluster struct {
	// ID is the cluster identifier.
	ID string
	// Label is the cluster label (parent unit info).
	Label *Label
	// Nodes are the nodes inside this cluster.
	Nodes []*Node
	// Clusters are nested clusters inside this cluster (for all-expanded mode).
	Clusters []*Cluster
	// Style contains cluster styling attributes.
	Style *NodeStyle
	// Type is the unit type for HTML label dispatch.
	Type model.UnitType
	// IsExternal indicates if this cluster represents an external unit.
	IsExternal bool
	// ExploreURL is the relative drill-down path for a collapsed container
	// cluster (CTX-03) — the cluster-side analog of Node.ExploreURL. Empty for
	// expanded or leaf clusters.
	ExploreURL string
	// ReferenceURL is the external docs URL (📖) of the unit this cluster
	// renders — the cluster-side analog of Node.ReferenceURL. Empty if none.
	// When both it and ExploreURL apply, the EXTERNAL reference wins the
	// cluster's single URL slot (same ARCHITECTURE-v1.10 §6 (6) Option A
	// precedence nodes use).
	ReferenceURL string
}

Cluster represents a subgraph cluster for expanded units.

type Edge

type Edge struct {
	// Source is the source node ID.
	Source string
	// Target is the target node ID.
	Target string
	// Name is the unique, model-derived, flag-independent cgraph edge
	// identifier assigned by the builder (quick 260831-01u BUG-3):
	// "{source}_to_{target}_{n}" with a per-(source, target) sequence number
	// from the PRE-dedup link walk, sanitized to [A-Za-z0-9_]. The converter
	// uses it verbatim as the cgraph edge name so every builder Edge maps to
	// its own drawn edge regardless of which flags suppressed which
	// attributes (empty for hand-built graphs — the renderer falls back to
	// its legacy name construction).
	Name string
	// Label contains edge label information.
	Label *EdgeLabel
	// Style is the line style (solid, dashed, dotted).
	Style string
	// ArrowHead is the arrow direction.
	ArrowHead ArrowDirection
	// Color is the edge line and label color (from source unit's border color or explicit override).
	Color string
	// MinLen is the minimum length (minlen attribute) for the edge.
	MinLen int
	// NoConstraint excludes the edge from rank computation (constraint=false),
	// so its endpoints may end up on the same rank. Set from link.Rank == "equal".
	NoConstraint bool
	// RankReverse flips the edge's layout ranking (link.Rank == "reverse"):
	// Source/Target stay logical; the renderer swaps the endpoints at emission
	// and inverts the dir attribute so the visual arrow still points at Target.
	RankReverse bool
	// PenWidth is the edge line width. 0 means the renderer applies the default
	// (1.0 in resolved views, 2.0 in --expanded mode).
	PenWidth float64
}

Edge represents a connection between two nodes.

type EdgeLabel

type EdgeLabel struct {
	// Technology appears in brackets (e.g., [HTTP]).
	Technology string
	// Description describes the relationship.
	Description string
	// Position is where the label appears (middle, head, tail).
	Position string
}

EdgeLabel contains label information for an edge.

type Graph

type Graph struct {
	// Title is the diagram title.
	Title string
	// Direction is the layout direction (default: "TB" for top-to-bottom).
	Direction string
	// EdgeStyle is the edge routing style (straight, spline, square).
	EdgeStyle string
	// Opts carries the suppression switches (--plain + granular flags),
	// copied at graph construction by BuildGraph, BuildExpandedGraph and
	// BuildGraphWithPath.
	Opts RenderOpts
	// Nodes are all top-level nodes in the graph.
	Nodes []*Node
	// Edges are all edges connecting nodes.
	Edges []*Edge
	// Clusters are subgraph clusters for expanded units.
	Clusters []*Cluster
	// Legend contains legend information for the diagram.
	Legend *Legend
	// Navigation contains breadcrumb and back-link info (nil for C1).
	Navigation *Navigation
}

Graph represents a graph structure ready for DOT rendering.

func BuildExpandedGraph

func BuildExpandedGraph(v *view.View) *Graph

BuildExpandedGraph constructs a graph with nested clusters for all-expanded mode. Unlike BuildGraph, this creates recursive cluster structures that show all nesting levels.

func BuildGraph

func BuildGraph(v *view.View) *Graph

BuildGraph constructs a graph structure from a view. The graph contains nodes, edges, and clusters ready for DOT rendering. For C2/C3 views, internal nodes are wrapped in a boundary cluster representing the expanded system/container; boundary/sibling entries and the expanded unit render inside their complete ancestor wrapper chains (WRAP-01/02). Fully external boundary nodes remain at the top level.

func BuildGraphWithPath

func BuildGraphWithPath(v *view.View, currentPath, basename, format string) *Graph

BuildGraphWithPath constructs a graph with navigation paths. currentPath is the dotted path of the current diagram (empty for C1). basename is the output base filename. format is the output format (svg or dot).

type Label

type Label struct {
	// Name is the primary name (with [+] indicator if expandable).
	Name string
	// Technology is the technology string (optional).
	Technology string
	// Description is the description text (optional).
	Description string
	// Icon is the emoji/icon prefix (optional).
	Icon string
}

Label represents a node label with multiple parts.

type Legend

type Legend struct {
	Entries []LegendEntry
}

Legend contains the entries rendered in the diagram's floating legend node (upper-right corner). The renderer emits them as colored text — the text itself is the sample — so each entry carries exactly the colour it explains. Entries are conditional on what the view actually shows: element level colours first, then link-kind colours, then author lines.

type LegendEntry added in v1.18.0

type LegendEntry struct {
	// Label explains the row (e.g. "container", "read").
	Label string
	// Color is the colour the row documents (element border/label colour or
	// link-kind colour). Empty falls back to the muted secondary grey.
	Color string
}

LegendEntry is one legend row: a text sample in the colour it documents.

type Navigation struct {
	// BackLink is the parent back-link (nil for C1 root).
	BackLink *BackLink
	// Breadcrumbs is the breadcrumb trail from root to current.
	Breadcrumbs []BreadcrumbItem
}

Navigation holds navigation elements for a diagram. It is nil for C1 root diagrams and populated for C2/C3 diagrams.

type Node

type Node struct {
	// ID is a unique identifier (the unit's full path).
	ID string
	// Label contains the formatted label parts.
	Label *Label
	// Shape is the node shape (determined by type).
	Shape Shape
	// Type is the unit type (used for special rendering like Person HTML labels).
	Type model.UnitType
	// Style contains visual styling attributes.
	Style *NodeStyle
	// IsExternal indicates if this is an external boundary node.
	IsExternal bool
	// IsInCluster indicates if this node is inside a cluster.
	IsInCluster bool
	// ExploreURL is the relative path for drill-down (empty if not expandable).
	ExploreURL string
	// ReferenceURL is the external docs URL (📖), empty if none.
	ReferenceURL string
}

Node represents a single node in the graph.

type NodeStyle

type NodeStyle struct {
	// FillColor is the background color.
	FillColor string
	// BorderColor is the border color.
	BorderColor string
	// FontColor is the text color.
	FontColor string
	// BorderStyle is "solid" or "dashed".
	BorderStyle string
	// BorderWidth is the penwidth attribute emitted on this style; 0 means the
	// renderer default (no attribute emitted). Mirrors Edge.PenWidth (D-02).
	BorderWidth float64
}

NodeStyle contains visual styling for a node.

func GetBoxStyleByContents

func GetBoxStyleByContents(unit *model.Unit) *NodeStyle

GetBoxStyleByContents returns the style for a box based on its contents. - Boxes with external subunits: grey border (PersonExternalBorder) - Internal boxes: level-coloured border (C1 dark blue / C2 blue / C3 light blue) - All boxes: transparent background, dashed border.

func GetStyleForType

func GetStyleForType(t model.UnitType, isExternal bool) *NodeStyle

GetStyleForType returns styling based on unit type and external status. Per CONTEXT.md decisions: - No inheritance: each unit's style is independent - External nodes: same size, solid border, external palette colors - Transparent backgrounds: FillColor is empty for all units.

type RenderOpts added in v1.22.0

type RenderOpts struct {
	// Plain mirrors View.Plain (--plain): author-custom formatting is ignored
	// and units/edges fall back to defaults.
	Plain bool
	// NoColors mirrors View.NoColors (--no-colors): author + kind colouring
	// suppressed, source-border default retained.
	NoColors bool
	// NoStyles mirrors View.NoStyles (--no-styles): line/border styles
	// suppressed.
	NoStyles bool
	// NoLength mirrors View.NoLength (--no-length): minlen suppressed.
	NoLength bool
	// NoRank mirrors View.NoRank (--no-rank): rank hints suppressed.
	NoRank bool
	// NoLabels mirrors View.NoLabels (--no-labels, BUG-2 — supersedes the
	// phase-38 LBL-01 pin): EDGE label text only; node, cluster and legend
	// labels are unaffected.
	NoLabels bool
}

RenderOpts carries the formatting-suppression switches threaded from the CLI onto every graph: --plain (master switch, PLAIN-01/PLAIN-02) plus the four granular opt-outs (KEY-01). --plain is the UNION of the granular switches (KEY-02) over AUTHOR formatting; kind-derived edge colours are semantic and survive plain alone (pinned by the v1.14 plain goldens), but an EXPLICIT --no-colors still removes them — user intent beats plain's keep-semantic-colours default (issue #22, documented in the README --plain composition notes).

type Shape

type Shape string

Shape represents the visual shape of a node in the graph.

const (
	// ShapeRecord is the standard record shape for nodes.
	ShapeRecord Shape = "record"
	// ShapeHTML indicates the node uses an HTML-like label for complex formatting.
	ShapeHTML Shape = "html"
	// ShapeCluster indicates a subgraph cluster containing nested nodes.
	ShapeCluster Shape = "cluster"
)

func ShapeForType

func ShapeForType(_ model.UnitType) Shape

ShapeForType returns the appropriate shape for a unit type. Per CONTEXT.md decision: collapsed units use record shape. Expanded units are rendered as clusters (subgraphs), not nodes.

Jump to

Keyboard shortcuts

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