How dave/dst works
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.