How cloudwego/dynamicgo works
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.