# dave/dst architecture

> Architecture diagram of the GitHub repository dave/dst, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dave/dst
- Repository: https://github.com/dave/dst

## Overview

##### Main Components

- Core DST Package:
– Represents the central data structure and types for the decorated syntax tree (files like dst.go, dstutil/…).
– Contains core functions for traversing and printing the DST, handling decorations (Before, After, Start, End).

- Decorator Module (in the “decorator” directory):
– Provides functions to parse Go source code into a DST (e.g., decorator.Parse).
– Contains conversion logic (from standard AST to DST and vice versa).
– Houses testing suites (decorator_test.go and various *resolver and *load helpers).

- Resolver Components:
– These come in several subpackages (goast, gobuild, gopackages, gotypes, guess, simple) and handle tasks such as resolving package names and import management when converting between DST and AST.
– They support both decorator and restorer roles by providing the necessary contextual information (for example, managing dot-imports or vendor specifics).

- Restoration (Restorer):
– Converts the decorated DST back into a standard Go AST while auto-managing the import block.
– This component is integrated with the resolver modules to correctly map package paths back to names.
– Files like restorer.go and its accompanying tests are part of this component.

- Utility & Helper Functions:
– dstutil offers functions to inspect or rewrite DST nodes and provides additional decoration management (e.g., listing decoration attachment points, rewriting nodes).
– The “clone” utilities, “print” helpers, explorer functions, and sample applications under gendst illustrate common applications.

- Example/Usage Applications:
– The gendst directory provides an example of how to utilize the library in a concrete setting.

##### Diagramming the Architecture

a. Input/Parsing Stage
– Start with “Source Code (Go)” as an external input.

b. Decoration Process
– Inside the Decorator Module, illustrate the core DST representation.
– Show “Decoration Attachment Points” (Before, Start, End, After) as sub-elements of the DST nodes.

c. Transformation & Modification
– Represent a “Modification” component that can adjust the DST (e.g., swapping nodes, modifying comments).
– This indicates that the DST is mutable and can be manipulated while preserving formatting.

d. Resolver & Import Management

e. Restoration Stage
– Show the “Restorer” component that takes the modified DST and converts it back to a valid, re-formatted AST (and eventually to source code).
– Indicate that the restorer also auto-manages import declarations using import-aware resolvers.

f. Utility Functions
– Include a distinct section for “dstutil” which provides supplementary capabilities like node inspection, decoration listing, and rewriting utilities.
– Connect these utility functions to both the Decorator and the Restoration layers.

##### Highlighting Structural Patterns and Interactions

- Maintain clear labels on each component (e.g., “Decorator”, “Restorer”, “Resolver – goast/gotypes/etc.”, “dstutil”) to ensure readability.
– From “Source Code” to “Parser/Decorator” (parsing process).
– From “Decorator” to “Modification/Transformation”, then to “Restorer” (data flow through modifications).
– From “Restorer” back to “Output Code” (final code generation).
- Separate testing and example usage (files like decorations_test.go, decorator_test.go, and the gendst example) as “Testing & Examples” to illustrate how the architecture is validated in real-world usage.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Input
    Source["Source Code (Go)"]:::input

    %% Parser/Decorator Module
    Parser["Parser/Decorator"]:::decorator

    %% Core DST Package Subgraph
    subgraph "Core DST Package"
        DSTCore["DST Core (dst.go)"]:::core
        DSTUtil["Decoration Utilities (dstutil/)"]:::core
        NoteCore["Decoration Markers:<br/>Before,Start,End,After"]:::note
    end

    %% Modification/Transformation Component
    Transformation["Modification/Transformation"]:::transformation

    %% Resolver Modules
    Resolver["Resolver Modules<br/>(goast, gobuild,<br/>gopackages,gotypes,guess,simple)"]:::resolver

    %% Restorer Component
    Restorer["Restorer (decorator/restorer.go)"]:::restorer

    %% Output Node
    Output["Output Code (Go)"]:::output

    %% Utility & Helper Functions
    Utility["Utility & Helper Functions<br/>(print.go, dstutil helpers)"]:::utility

    %% Example/Usage Applications
    Example["Example/Usage Applications (gendst/)"]:::example

    %% Testing & Examples
    Testing["Testing & Examples"]:::testing

    %% Data Flow Connections
    Source -->|"parse"| Parser
    Parser -->|"build DST using"| DSTCore
    Parser -->|"build DST using"| DSTUtil
    DSTCore --> Transformation
    DSTUtil --> Transformation
    Transformation --> Resolver
    Resolver --> Restorer
    Restorer --> Output

    %% Utility connections
    Utility --- Parser
    Utility --- Restorer

    %% Example and Testing connections
    Example --- Parser
    Testing --- Parser
    Testing --- Restorer

    %% Click Events
    click Parser "https://github.com/dave/dst/blob/master/decorator/decorator.go"
    click DSTCore "https://github.com/dave/dst/blob/master/dst.go"
    click DSTUtil "https://github.com/dave/dst/tree/master/dstutil/"
    click Resolver "https://github.com/dave/dst/tree/master/decorator/resolver/"
    click Restorer "https://github.com/dave/dst/blob/master/decorator/restorer.go"
    click Utility "https://github.com/dave/dst/blob/master/print.go"
    click Example "https://github.com/dave/dst/tree/master/gendst/"

    %% Styles
    classDef input fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef decorator fill:#F08080,stroke:#000,stroke-width:1px;
    classDef core fill:#FFD700,stroke:#000,stroke-width:1px;
    classDef transformation fill:#FFFFE0,stroke:#000,stroke-width:1px;
    classDef resolver fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef restorer fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef output fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef utility fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef example fill:#FFDEAD,stroke:#000,stroke-width:1px;
    classDef testing fill:#E6E6FA,stroke:#000,stroke-width:1px;
    classDef note fill:#eee,stroke:#666,stroke-width:1px,stroke-dasharray: 5, 5;
```
