How licat233/genzero works
Project type and purpose
• This is an open-source CLI tool (“genzero”) for generating Go code (go-zero framework) from MySQL definitions.
• It scaffolds .api and .proto files, model code, API logic, RPC logic, and integrates with goctl.
File structure
• Top-level “cmd” folder: cobra‐style commands (root.go, apiModule.go, modelModule.go, etc.).
• “config”: configuration loader and types.
• “modules”: one subfolder per feature (api, logic, model, pb), each containing template definitions and generation logic.
• “tools” and “utils”: shared utilities for file I/O, string manipulation, YAML handling, goroutines.
• “sql”: SQL parsing logic for DDL input.
• “examples”: sample input files and generated output layouts.
• “global”: shared global variables.
• “upgrade”: self-upgrade command logic.
README for additional insights
• Core commands: init (generate YAML), api/pb/model (generate files), start (bulk generate from config), upgrade.
• Depends on external go-zero’s goctl tool for scaffolding.
• Uses a configuration YAML (genzero.yaml) or command flags as input.
Main components
a. CLI Frontend
• cmd/root.go and sub-modules in cmd/
• Parses flags and subcommands
b. Configuration Loader
• config/config.go, config/types.go, config/functions.go
• Reads genzero.yaml or flags
c. SQL Parser
• sql/ParseSqlFile.go, sql/ParseSqlDsn.go
• Parses DDL into internal table/schema representations
d. Module Manager
• Dispatches to one of four modules: api, model, logic (placeholder), pb
• Each module under modules/<name>/
• Templates in conf/
• Generation logic in internal/
e. Template Engine
• Renders templates (Go text/templates) with the parsed schema
f. goctl Integrator
• Shells out or invokes goctl commands for further scaffolding
g. File System I/O
• Reads input files (.sql, .api, .proto, YAML)
• Writes output code files (model/*.go, api/internal/*, rpc/*, etc.)
h. Examples & Upgrader
• examples/: demonstration flows
• upgrade/: self-update component
Relationships and interactions
• User → CLI Frontend: issues genzero command
• CLI Frontend → Configuration Loader: obtain settings
• CLI Frontend → SQL Parser (for model/pb/api modules)
• SQL Parser → internal schema object → Module Manager
• Module Manager → Template Engine × each module → code files
• Module Manager → goctl Integrator (where needed)
• All components read/write via File System I/O
Architectural patterns and design principles
• Modular design: each feature module isolated under modules/
• Separation of concerns: parsing, templating, file I/O, command handling
• Extensibility: adding a new module means creating modules/<new>/ with its own templates and logic
• Template-driven code generation
• CLI-driven flow with cobra for subcommands
Technologies, frameworks, and libraries
• Go language
• go-zero’s goctl (external binary)
• Cobra for CLI (cmd/)
• Go’s text/template engine
• YAML parsing (in tools/yaml.go)
• Native SQL parsing (custom in sql/)
Detailed capture
• Under Template Engine, annotate “go text/template + modules/<name>/conf”.
• Under goctl Integrator, note “invokes: goctl api|rpc|model ...”.
• File System I/O: “reads: *.sql, *.yaml, *.api, *.proto; writes: generated code directories”.