# licat233/genzero architecture

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

- Interactive diagram: https://gitdiagram.com/licat233/genzero
- Repository: https://github.com/licat233/genzero

## Overview

##### 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”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User initiates CLI
    User["User"]:::external
    User -->|"issues command"| CLIFront["CLI Frontend"]:::frontend

    %% CLI Frontend
    subgraph "CLI Frontend"
        Root["root.go"]:::frontend
        APICmd["apiModule.go"]:::frontend
        ModelCmd["modelModule.go"]:::frontend
        PBCmd["pbModule.go"]:::frontend
        LogicCmd["logicModule.go"]:::frontend
        ConfigCmd["configure.go"]:::frontend
    end
    CLIFront --> Root
    CLIFront --> APICmd
    CLIFront --> ModelCmd
    CLIFront --> PBCmd
    CLIFront --> LogicCmd
    CLIFront --> ConfigCmd

    %% Config Loader and Global Vars
    subgraph "Configuration Loader"
        ConfigGo["config.go"]:::core
        TypesGo["types.go"]:::core
        FuncsGo["functions.go"]:::core
        VarGo["var.go"]:::core
    end
    CLIFront -->|"loads config"| ConfigGo
    CLIFront --> VarGo

    GlobalVar["global.go"]:::core
    ConfigGo --> GlobalVar

    %% SQL Parser
    subgraph "SQL Parser"
        ParseFile["ParseSqlFile.go"]:::core
        ParseDsn["ParseSqlDsn.go"]:::core
        CommonSQL["common.go"]:::core
        ConfSQL["conf.go"]:::core
        TableSQL["table.go"]:::core
        EnumSQL["enum.go"]:::core
    end
    CLIFront -->|"parses SQL"| ParseFile
    CLIFront --> ParseDsn

    %% Module Manager
    ModuleMgr["Module Manager"]:::core
    CLIFront --> ModuleMgr
    ParseFile --> ModuleMgr
    ParseDsn --> ModuleMgr
    ModuleMgr --> ConfigGo

    click Root "https://github.com/licat233/genzero/blob/main/cmd/root.go"
    click APICmd "https://github.com/licat233/genzero/blob/main/cmd/apiModule.go"
    click ModelCmd "https://github.com/licat233/genzero/blob/main/cmd/modelModule.go"
    click PBCmd "https://github.com/licat233/genzero/blob/main/cmd/pbModule.go"
    click LogicCmd "https://github.com/licat233/genzero/blob/main/cmd/logicModule.go"
    click ConfigCmd "https://github.com/licat233/genzero/blob/main/cmd/configure.go"
    click ConfigGo "https://github.com/licat233/genzero/blob/main/config/config.go"
    click TypesGo "https://github.com/licat233/genzero/blob/main/config/types.go"
    click FuncsGo "https://github.com/licat233/genzero/blob/main/config/functions.go"
    click VarGo "https://github.com/licat233/genzero/blob/main/config/var.go"
    click GlobalVar "https://github.com/licat233/genzero/blob/main/global/global.go"
    click ParseFile "https://github.com/licat233/genzero/blob/main/sql/ParseSqlFile.go"
    click ParseDsn "https://github.com/licat233/genzero/blob/main/sql/ParseSqlDsn.go"
    click CommonSQL "https://github.com/licat233/genzero/blob/main/sql/common.go"
    click ConfSQL "https://github.com/licat233/genzero/blob/main/sql/conf.go"
    click TableSQL "https://github.com/licat233/genzero/blob/main/sql/table.go"
    click EnumSQL "https://github.com/licat233/genzero/blob/main/sql/enum.go"
    click ModuleMgr "https://github.com/licat233/genzero/blob/main/modules/module.go"

    %% Modules under Module Manager
    subgraph "Modules"
        API["API Module"]:::core
        Model["Model Module"]:::core
        PB["RPC (pb) Module"]:::core
        Logic["Logic Module"]:::core
    end
    ModuleMgr --> API
    ModuleMgr --> Model
    ModuleMgr --> PB
    ModuleMgr --> Logic

    %% API Module details
    subgraph "API Module"
        API_go["api.go"]:::core
        API_tpl["api.tpl"]:::core
        API_conf["conf.go"]:::core
        API_tplgo["tpl.go"]:::core
        API_int["internal/"]:::core
    end
    API --> API_go
    API --> API_tpl
    API --> API_conf
    API --> API_tplgo
    API --> API_int

    click API_go "https://github.com/licat233/genzero/blob/main/modules/api/api.go"
    click API_tpl "https://github.com/licat233/genzero/blob/main/modules/api/conf/api.tpl"
    click API_conf "https://github.com/licat233/genzero/blob/main/modules/api/conf/conf.go"
    click API_tplgo "https://github.com/licat233/genzero/blob/main/modules/api/conf/tpl.go"
    click API_int "https://github.com/licat233/genzero/tree/main/modules/api/internal/"

    %% Model Module details
    subgraph "Model Module"
        Model_go["model.go"]:::core
        Model_tpl["model.tpl"]:::core
        Model_conf["conf.go"]:::core
        Model_tplgo["tpl.go"]:::core
        Model_int["internal/"]:::core
    end
    Model --> Model_go
    Model --> Model_tpl
    Model --> Model_conf
    Model --> Model_tplgo
    Model --> Model_int

    click Model_go "https://github.com/licat233/genzero/blob/main/modules/model/model.go"
    click Model_tpl "https://github.com/licat233/genzero/blob/main/modules/model/conf/model.tpl"
    click Model_conf "https://github.com/licat233/genzero/blob/main/modules/model/conf/conf.go"
    click Model_tplgo "https://github.com/licat233/genzero/blob/main/modules/model/conf/tpl.go"
    click Model_int "https://github.com/licat233/genzero/tree/main/modules/model/internal/"

    %% RPC Module details
    subgraph "RPC (pb) Module"
        PB_go["pb.go"]:::core
        PB_tpl["proto.tpl"]:::core
        PB_conf["conf.go"]:::core
        PB_tplgo["tpl.go"]:::core
        PB_int["internal/"]:::core
    end
    PB --> PB_go
    PB --> PB_tpl
    PB --> PB_conf
    PB --> PB_tplgo
    PB --> PB_int

    click PB_go "https://github.com/licat233/genzero/blob/main/modules/pb/pb.go"
    click PB_tpl "https://github.com/licat233/genzero/blob/main/modules/pb/conf/proto.tpl"
    click PB_conf "https://github.com/licat233/genzero/blob/main/modules/pb/conf/conf.go"
    click PB_tplgo "https://github.com/licat233/genzero/blob/main/modules/pb/conf/tpl.go"
    click PB_int "https://github.com/licat233/genzero/tree/main/modules/pb/internal/"

    %% Logic Module details
    subgraph "Logic Module"
        Logic_go["logic.go"]:::core
        Logic_conf["conf.go"]:::core
        Logic_apiint["apilogic/"]:::core
        Logic_rpcint["rpclogic/"]:::core
    end
    Logic --> Logic_go
    Logic --> Logic_conf
    Logic --> Logic_apiint
    Logic --> Logic_rpcint

    click Logic_go "https://github.com/licat233/genzero/blob/main/modules/logic/logic.go"
    click Logic_conf "https://github.com/licat233/genzero/blob/main/modules/logic/conf/conf.go"
    click Logic_apiint "https://github.com/licat233/genzero/tree/main/modules/logic/internal/apilogic/"
    click Logic_rpcint "https://github.com/licat233/genzero/tree/main/modules/logic/internal/rpclogic/"

    %% Template Engine and goctl Integrator
    Template["Template Engine"]:::core
    Goctl["goctl Integrator"]:::external
    ModuleMgr --> Template
    ModuleMgr --> Goctl

    click Template "https://github.com/licat233/genzero/blob/main/modules/api/conf/api.tpl"
    click Template "https://github.com/licat233/genzero/blob/main/modules/model/conf/model.tpl"
    click Template "https://github.com/licat233/genzero/blob/main/modules/pb/conf/proto.tpl"

    %% File System and Utilities
    FileSys["File System I/O"]:::fs
    Template --> FileSys
    Goctl --> FileSys
    ModuleMgr --> FileSys

    click FileSys "https://github.com/licat233/genzero/blob/main/tools/file.go"
    click FileSys "https://github.com/licat233/genzero/blob/main/modules/utils/file.go"

    subgraph "Utilities"
        YAML["YAML Utils"]:::util
        StrFmt["String & Format Utils"]:::util
        Goroutine["Goroutine Utils"]:::util
        Shared["Shared Utils"]:::util
    end
    FileSys --> YAML
    FileSys --> StrFmt
    FileSys --> Goroutine
    FileSys --> Shared

    click YAML "https://github.com/licat233/genzero/blob/main/tools/yaml.go"
    click StrFmt "https://github.com/licat233/genzero/blob/main/tools/strings.go"
    click StrFmt "https://github.com/licat233/genzero/blob/main/tools/fmt.go"
    click StrFmt "https://github.com/licat233/genzero/blob/main/tools/format.go"
    click Goroutine "https://github.com/licat233/genzero/blob/main/tools/goroutine.go"
    click Shared "https://github.com/licat233/genzero/blob/main/modules/utils/content.go"
    click Shared "https://github.com/licat233/genzero/blob/main/modules/utils/slice.go"
    click Shared "https://github.com/licat233/genzero/blob/main/modules/utils/utils.go"

    %% Examples and Upgrader
    Examples["Examples"]:::external
    Upgrader["Upgrader"]:::core
    CLIFront --> Examples
    CLIFront --> Upgrader

    click Examples "https://github.com/licat233/genzero/tree/main/examples/"
    click Upgrader "https://github.com/licat233/genzero/blob/main/upgrade/upgrade.go"

    %% Main Entry
    Main["main.go"]:::frontend
    User --> Main
    click Main "https://github.com/licat233/genzero/blob/main/main.go"

    %% Styles
    classDef frontend fill:#D6E9FE,stroke:#3A8FD0,color:#0B3C5D
    classDef core fill:#E8F1F2,stroke:#5A6F79,color:#1D2D44
    classDef external fill:#FDE2E4,stroke:#E12D39,color:#6C1D2E
    classDef util fill:#FEF5E7,stroke:#F5C156,color:#8C6D1F
    classDef fs fill:#E2F0CB,stroke:#6CA965,color:#2B4F19
```
