This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
codegen is a Go-based code generation tool that generates code from Protocol Buffer (protobuf) definitions. It parses .proto files and generates Go code with enhanced struct tags (gorm, validate, role), implementation scaffolding, database tables, cache layers, i18n support, and more.
Documentation: https://lazygophers.pages.dev/ Template Repository: https://github.com/lazygophers/template
-
codegen/ - Core code generation logic
pbparse.go- Protobuf file parsing using github.com/emicklei/protogenerate_pb_file.go- Generates .pb.go files via protocgenerate_struct_tag.go- Adds struct tags (gorm, validate, role) to generated structsgenerate_impl.go- Generates service implementation boilerplategenerate_table.go,generate_cache.go,generate_conf.go- State layer generatorsgenerate_add_rpc.go- Batch RPC endpoint generation
-
cli/ - CLI interface using cobra
root.go- Root command initializationgen/gen.go- Main generation command group with subcommands (pb, impl, table, cache, etc.)i18n/- i18n translation commands
-
i18n/ - Internationalization framework
translater.go- Translation providers (Google Free, DeepL Free)tran.go- Translation orchestration with cachingstorage.go- BoltDB-based translation cachelocalize.go- Multi-format serialization (JSON, YAML, TOML)
-
state/ - Configuration and state management
config.go- Main configuration structureload.go- Configuration loading from multiple sourceslazy_config.go- Project-specific .lazygophers config
- Parse
.protofile →PbPackagestructure - Generate
.pb.gousing protoc - Parse generated
.pb.goto add custom tags - Generate implementation files (impl, routes, clients)
- Generate state files (tables, cache, config) if enabled
# Build using goreleaser
make build
# Output: ./dist/cli_<platform>_<arch>/codegengo test ./...make lint
# Requires: golangci-lint# Generate translations from source file
make tran
# Or directly:
codegen i18n tran --generate-const=true -s ./state/localize/zh.yaml# Generate .pb.go file
codegen gen pb -i path/to/file.proto
# Generate all (pb + impl + state)
codegen gen all -i path/to/file.proto
# Generate specific components
codegen gen impl -i path/to/file.proto
codegen gen table -i path/to/file.proto
codegen gen cache -i path/to/file.protoConfiguration is loaded from (in order):
- Environment variable
LAZYGO_CODEGEN_CONFIG_FILE - System config directory:
~/Library/Application Support/lazygophers/codegen.cfg.yaml(macOS) - Project-level
.lazygophersfile - Command-line flags
Key configuration fields:
protoc-path- Path to protoc binaryprotogen-go-path- Path to protoc-gen-go pluginoutput-path- Base directory for generated codeproto-files- Additional proto import pathsdefault-tag- Default struct tags (gorm, validate)state.table/cache/i18n- Enable/disable state generation
See example.codegen.cfg.yaml for full configuration options.
The tool recognizes special comment tags in .proto files:
message User {
// @gorm: primary_key
// @role: admin:wr;user:r
// @validate: required
string id = 1;
}These are parsed and converted to Go struct tags in the generated code.
When adding new generators:
- Create
generate_<name>.goincodegen/ - Add CLI command in
cli/gen/gen_<name>.go - Register command in
cli/gen/gen.go:initGen() - Add localized strings in
state/localize/
# Build the tool
make build
# Test on example proto
./dist/cli_darwin_arm64/codegen gen pb -i ../example/example.protoThe codebase has Windows path handling fixes (see commit 329f560, 451b9c0). When working with file paths:
- Always use
filepath.ToSlash()for cross-platform paths - Use
filepath.Join()instead of manual path construction - Test on Windows when modifying path handling
The i18n translation system supports:
- Multiple translation providers (Google Free, DeepL Free)
- Translation caching with BoltDB to avoid re-translating unchanged text
- Automatic translation of nested YAML/JSON/TOML structures
- Batch translation with configurable overwrite policies
When translating:
- Source file: typically
state/localize/zh.yaml - Cache file:
.i18n_tran_cache.db(configurable) - Supports 100+ languages
-
Proto file location: The tool auto-detects proto files by walking up the directory tree. Use
-iflag if auto-detection fails. -
Module prefix: If
go_packagein .proto doesn't match your actual module path, use--go-module-prefixflag or configurego-module-prefixin config. -
Overwrite protection: By default, impl files won't be overwritten. Use
--overwriteflag with caution. -
Lazy config: The tool looks for
.lazygophersfile in the project root (determined by go_package). This configures project-specific generation options.
- Memory Management: When users say "remember xxx" or repeatedly emphasize certain rules, behaviors, etc., automatically update relevant memory banks and documentation
- Code Analysis: Before coding, analyze at least 3 existing implementations or patterns to identify reusable interfaces and constraints
- Dependency Mapping: Draw dependencies and integration points, confirming input/output protocols, configuration, and environment requirements
- Code Consistency: Understand existing testing frameworks, naming conventions, and formatting rules to ensure output aligns with the codebase
- Documentation Priority: Use context7 to query programming library documentation first, avoid over-reliance on web searches or assumptions
- Best Practice Learning: Use github.search_code to search open-source implementation examples and learn best practices
- Complete Implementation: Absolutely prohibit MVP, minimal implementations, or placeholders; full functionality and data paths must be completed before submission
- Code Cleanup: Must improve all MVP, minimal implementations, and placeholders to complete, concrete code implementations
- Active Cleanup: Must proactively delete outdated, duplicate, or escape-style code to maintain clean implementations
- Style Consistency: Must always adhere to programming language standard code styles and project-specific style specifications
- Breaking Changes: Do not maintain backward compatibility for breaking changes, while providing migration steps or rollback plans
- Disruptive Strategy: Must always adopt disruptive breaking change strategy, absolutely no backward compatibility
- Quality Assurance: Must follow best practices to ensure code quality and maintainability
- Language-Specific Patterns: Absolutely prohibit using Java-style coding standards to write code or documentation in other languages
- Performance Evaluation: Must evaluate time complexity, memory usage, and I/O impact during design to avoid unnecessary consumption
- Optimization Awareness: After identifying potential bottlenecks, provide monitoring or optimization suggestions to ensure sustainable iteration
- Dependency Assessment: Prohibit introducing unevaluated expensive dependencies or blocking operations
- Must find at least 3 similar features or components to understand their design and reuse patterns
- Must identify common patterns and conventions in the project and apply them in new implementations
- Must prioritize using existing libraries, tools, or helper functions
- Must follow existing testing arrangements and沿用 assertion and fixture structures
- Must use the project's existing build system, no private addition of scripts
- Must use the project's established testing framework and execution methods
- Must use the project's formatting/static checking settings
- If there is indeed a need for new tools, must provide sufficient justification and obtain documented approval
Absolutely Prohibited:
- Making assumptions without evidence; all conclusions must cite existing code or documentation
Must Be Done:
- Complete detailed planning and documentation before implementing complex tasks
- Generate task decomposition for cross-module or more than 5 subtasks work
- Maintain TODO lists for complex tasks and update progress in a timely manner
- Verify planning documents are confirmed before starting development
- Maintain small-step delivery, ensuring each commit is in a usable state
- Synchronously update planning documents and progress records during execution
- Proactively learn the advantages and disadvantages of existing implementations and reuse or improve them
- Must pause operations after three consecutive failures and reassess strategy
- Each level must be self-consistent in mastering its own abstract scope, prohibiting cross-level content mixing
- Must reference materials from other levels rather than copy-pasting, maintaining unique information sources
- Each level must describe the system from the corresponding perspective, avoiding overreaching details
- Prohibit stacking implementation details in high-level documents, ensuring clear architecture and implementation boundaries