# cloudwego/dynamicgo architecture

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

- Interactive diagram: https://gitdiagram.com/cloudwego/dynamicgo
- Repository: https://github.com/cloudwego/dynamicgo

## Overview

##### Project Type & Purpose
- This is a Go library/tool designed to efficiently process data described in Thrift and Protobuf formats. It supports dynamic, runtime parsing and conversion between protocols.
- The README sections show separate treatment for Thrift (with generic and annotation-based APIs) and Protobuf (with low-level wire parsing and a binary protocol tool). Additionally, there are conversion modules for mapping between JSON, Thrift, and Protobuf data.

##### Main Components
a. Protocol Processing Modules:
- Thrift Modules: Under the “thrift” directory, you have three main areas:
- thrift/generic: Provides reflection APIs to modify and process thrift messages (with or without runtime type descriptors).
- thrift/base: Metadata transport (caller, address, log-id, etc.) used by other parts of the system.
- thrift/annotation: Implements third-party annotation standards used in conversion modules.
- Protobuf Modules: Under the “proto” directory you see:
- proto/generic: Reflection APIs for protobuf.
- proto/protowire: Contains low-level encoding/decoding functionality.
- proto/binary: Tools for handling the binary representations directly.

b. Conversion Modules (conv):
- These live in the “conv” directory and provide conversion between different protocols:
- conv/j2t: Converts JSON (or HTTP JSON bodies) to Thrift messages.
- conv/t2j: Converts Thrift messages to JSON (or HTTP JSON responses).
- conv/j2p: Converts JSON to Protobuf.
- conv/p2j: Converts Protobuf messages to JSON.

c. HTTP Wrapper:
- The “http” directory wraps HTTP request/response handling. This is used (for example) when converting HTTP JSON requests into Thrift messages.

d. Internal and Native Optimizations:
- “internal” packages provide utilities that power the core functions:
- internal/caching and native modules for low-level optimizations.
- internal/json: Handling JSON-specific optimized encoding/decoding.
- internal/rt (runtime) & util modules: Various helper functions to maximize performance.
- The “native” directory contains C code and other native implementations that accelerate parsing, conversion, and other operations.

e. Other Supporting Components:
- “meta”: Contains metadata definitions, constants, and error handling that cut across modules.
- “proto”, “thrift”, and testdata directories illustrate how the libraries can be integrated with actual IDLs and real-world examples.
- CI/CD configurations (.github/workflows) and Makefile indicate build and testing pipelines but are peripheral to the architectural logic.

##### Relationship & Data Flow
- The “Protocol Processing” layer with Thrift and Protobuf components.
- The “Conversion” layer (conv module) sits atop these, orchestrating how data moves from JSON (or HTTP) into the low-level Thrift/Protobuf formats and vice versa.
- A dedicated “HTTP” block sits adjacent—indicating that HTTP interfaces use the conversion layer to translate between JSON bodies and native messaging.
- The “Internal/Native Optimizations” layer supports the processing modules enabling faster in-memory operations, caching, optimized math and memory operations.
- From external input (HTTP/JSON) into the conv modules.
- From conv modules to the underlying protocol parsers/serializers.
- And between the modules where the data may be transformed through generic reflection APIs.

##### Architectural Patterns & Design Principles
- Reflection-based design which allows dynamic parsing and converting message types without static code generation.
- Separation of concerns: Each module (protocol processing, conversion, HTTP handling) has clearly delineated responsibilities.
- The design leverages performance optimizations using native code/C-based implementations (seen in directories like “native” and “internal/native”) making for a layered architecture where high-level conversions rely on low-level optimizations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Interface
    subgraph "External Interface"
        HW["HTTP Wrapper"]:::http
    end

    %% Conversion Layer
    subgraph "Conversion Layer"
        convJ2T["JSON→Thrift Converter"]:::conversion
        convT2J["Thrift→JSON Converter"]:::conversion
        convJ2P["JSON→Protobuf Converter"]:::conversion
        convP2J["Protobuf→JSON Converter"]:::conversion
    end

    %% Protocol Processing - Thrift
    subgraph "Protocol Processing - Thrift"
        TGen["Thrift Generic Module"]:::protocol
        TBase["Thrift Base Module"]:::protocol
        TAnno["Thrift Annotation Module"]:::protocol
    end

    %% Protocol Processing - Protobuf
    subgraph "Protocol Processing - Protobuf"
        PGen["Protobuf Generic Module"]:::protocol
        PWire["Protobuf Protowire Module"]:::protocol
        PBin["Protobuf Binary Module"]:::protocol
    end

    %% Internal Optimizations
    subgraph "Internal Optimizations"
        ICS["Internal Caching"]:::internal
        IJ["Internal JSON Handling"]:::internal
        INat["Internal Native Code"]:::internal
        RTUtil["Runtime & Utility Functions"]:::internal
    end

    %% Supporting Modules
    subgraph "Supporting Modules"
        Meta["Metadata & Error Handling"]:::meta
    end

    %% Data Flow Connections
    HW -->|"inbound"| convJ2T
    HW -->|"inbound"| convJ2P

    convJ2T -->|"converts"| TGen
    convJ2P -->|"converts"| PGen

    TGen -->|"processes"| convT2J
    PGen -->|"processes"| convP2J

    convT2J -->|"output"| HW
    convP2J -->|"output"| HW

    %% Metadata support
    Meta -->|"supports"| TBase

    %% Dotted connections to Internal Optimizations (support links)
    convJ2T -.-> ICS
    convJ2P -.-> ICS
    convT2J -.-> ICS
    convP2J -.-> ICS

    TGen -.-> INat
    TBase -.-> INat
    TAnno -.-> INat
    PGen -.-> INat
    PWire -.-> INat
    PBin -.-> INat

    convJ2T -.-> RTUtil
    convT2J -.-> RTUtil
    convJ2P -.-> RTUtil
    convP2J -.-> RTUtil
    TBase -.-> RTUtil
    PBin -.-> RTUtil

    TGen -.-> IJ
    PGen -.-> IJ

    %% Click Events
    click TGen "https://github.com/cloudwego/dynamicgo/tree/main/thrift/generic"
    click TBase "https://github.com/cloudwego/dynamicgo/tree/main/thrift/base"
    click TAnno "https://github.com/cloudwego/dynamicgo/tree/main/thrift/annotation"
    click PGen "https://github.com/cloudwego/dynamicgo/tree/main/proto/generic"
    click PWire "https://github.com/cloudwego/dynamicgo/tree/main/proto/protowire"
    click PBin "https://github.com/cloudwego/dynamicgo/tree/main/proto/binary"
    click convJ2T "https://github.com/cloudwego/dynamicgo/tree/main/conv/j2t"
    click convT2J "https://github.com/cloudwego/dynamicgo/tree/main/conv/t2j"
    click convJ2P "https://github.com/cloudwego/dynamicgo/tree/main/conv/j2p"
    click convP2J "https://github.com/cloudwego/dynamicgo/tree/main/conv/p2j"
    click HW "https://github.com/cloudwego/dynamicgo/tree/main/http"
    click ICS "https://github.com/cloudwego/dynamicgo/tree/main/internal/caching"
    click IJ "https://github.com/cloudwego/dynamicgo/tree/main/internal/json"
    click INat "https://github.com/cloudwego/dynamicgo/tree/main/internal/native"
    click RTUtil "https://github.com/cloudwego/dynamicgo/tree/main/internal/rt"
    click Meta "https://github.com/cloudwego/dynamicgo/tree/main/meta"

    %% Styles
    classDef http fill:#ffa500,stroke:#333,stroke-width:2px;
    classDef conversion fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef protocol fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef internal fill:#dda0dd,stroke:#333,stroke-width:2px;
    classDef meta fill:#ffc0cb,stroke:#333,stroke-width:2px;
```
