How vic/deco works
Project type and purpose
• Deco is an open-source Elixir library that provides compile-time function decorators (around-advice) via a single macro. It is not a full-stack application but a developer tool / metaprogramming library.
File structure
• Top-level files:
• mix.exs / mix.lock – Mix project definitions and dependencies
• README.md, .travis.yml, LICENSE – documentation and CI config
• config/config.exs – standard Mix configuration
• lib/deco.ex – the public entry point defining use Deco and the deco macro
• lib/deco/core.ex – core logic that sequences decorators and transforms AST
• lib/deco/trace.ex – example decorator module (tracing)
• test/ – unit tests for the library
README for additional insights
• Describes the single macro deco DECORATORS in FORM
• Emphasizes minimalist core, zero runtime overhead (all at compile-time)
• Shows built-in convenience functions (trace, pipe_result, update_guard, around)
• Illustrates how decorators are just functions Macro.t() -> Macro.t()
a. Main components
• User Module (client code) that does use Deco and writes decorated functions
• Deco.Public API (lib/deco.ex) containing the deco macro
• Deco.Core (lib/deco/core.ex) which implements the decorator pipeline
• Built-in Decorators (lib/deco/trace.ex, and core helper functions: update_guard, pipe_result, around)
• Elixir Compiler / Mix – processes the AST emitted by the macros and produces BEAM bytecode
• External Service: Logger (used by the Trace decorator)
b. Relationships & interactions
1. At compile time, the user’s deco … in def … end invokes the macro in Deco.Public API.
2. The macro delegates to Deco.Core, passing in the AST of the function and the list of decorator functions.
3. Deco.Core iterates the decorators tuple, invoking each decorator function (e.g., Trace.trace/0, pipe_result/1, around/1) to transform the AST.
4. Deco.Core returns the final transformed AST to the compiler.
5. The Elixir compiler (via Mix) compiles the AST into BEAM code. If the Trace decorator is present, the resulting function will emit logs via the Logger module at runtime.
6. The test suite (in test/) runs the compiled functions to validate behavior.
c. Architectural patterns & design principles
• Metaprogramming via macros: all decoration happens at compile time to avoid run-time overhead
• Functional composition: decorators are plain functions that transform AST, composed in a pipeline
• Single responsibility: one macro in deco.ex, core pipeline in core.ex, each decorator in its own module/function
d. Key technologies & libraries
• Elixir Macros (Macro.t AST transformations)
• Mix build tool (mix.exs)
• Logger (for Trace decorator)
• ExUnit (test framework in test/)
Diagramming suggestions
• Use clear labels for each component: “User Module,” “Deco Macro (lib/deco.ex),” “Decorator Pipeline (lib/deco/core.ex),” “Decorator Functions,” “Elixir Compiler,” “Logger.”
• from User Module → Deco Macro → Decorator Pipeline → Elixir Compiler
• from Decorator Pipeline → each Decorator Function → back to Decorator Pipeline
• from Decorated Functions at runtime → Logger (if Trace is used)
• Core engine (Decorator Pipeline) in another
• Decorators (trace, pipe_result, around) in a third
Detail without over-segmentation
• Compile-time AST transformation flow end-to-end
• How user code maps into the library and out to compiled BEAM code
• Test suite as a verification component, but keep the focus on the decorator mechanism