Skip to content

khuong02/kd-gen

Repository files navigation

KD Gen

A powerful Go code generation tool for creating type-safe enums from YAML configuration files.

Overview

KD Gen is a CLI tool that generates Go enum types with useful helper methods from YAML configuration files. It helps you create type-safe enums with string representations, parsing functions, and more.

✨ Features

V1

  • Generate type-safe enums (int, string, uint64, …)
  • JSON support (MarshalJSON, UnmarshalJSON)
  • Database/sql integration (Scan, Value for DB storage)

V2

  • MCP Tools → expose enum generation as interactive tools:

Tools

Tool Category Description
list_methods Enum List all supported enum generation methods with descriptions.
show_config_structure Config Show the YAML structure for a valid enum configuration file.
example_config Config Generate an example enum config file for a given type (e.g., string, int).
validate_config Config Validate an enum YAML config against schema and supported types.
generate_enum Codegen Generate Go enum code from a provided YAML configuration.
explain_enum Docs Explain what a given enum config means and how it maps to generated code.
diff_config Utility Compare two enum YAML config files and show the differences.
format_config Utility Format and normalize enum YAML config files.

Go Version

This project is developed using Go 1.25.0. It should work with other recent Go versions as well.

Libraries Used

Installation

From Source

# Clone the repository
git clone https://github.com/khuong02/kd-gen.git
cd kd-gen

# Build the binary
go build -o ./build/kd-gen main.go

# Optional: Move to a directory in your PATH
mv ./build/kd-gen /usr/local/bin/

Using docker

# Build image
docker build -t kd-gen .

# Run
docker run --rm -v $(pwd):/app -w /app kd-gen \
  enum gen --package core --output ./example/enum/enum_gen.go --config ./example/enum/enum.yaml

Using Go Install

go install github.com/khuong02/kd-gen@latest

Using Homebrew

You can install KD Gen via Homebrew using a custom tap:

# Add the tap
brew tap khuong02/tap https://github.com/khuong02/homebrew-tap

# Install kd-gen
brew install kd-gen

## Usage

### Basic Usage

Create a YAML configuration file defining your enums:

```yaml
enums:
  - name: Lang
    type: string
    methods:
    - String|Parse|Normalize|JSON|SQL
    values:
      - name: LangEnglish
        display: "English"
        code: "en"
      - name: LangVietnamese
        display: "Vietnamese"
        code: "vi"
  - name: LogLevel
    type: int
    values:
      - name: Debug
      - name: Info
      - name: Warn
      - name: Error
      - name: Fatal

Then run the tool to generate the Go code:

kd-gen enum gen --package core --output ./example/enum/enum_gen.go --config ./example/enum/enum.yaml

Sample Run

# Create a config file
cat > enum.yaml << EOF
enums:
  - name: Status
    type: string
    methods:
    - String
    values:
      - name: StatusActive
        display: "active"
        code: 1
      - name: StatusInactive
        display: "inactive"
        code: 0
EOF

# Generate the enum code
kd-gen enum gen --package core --output status_enum.go --config enum.yaml

The generated file will contain:

  • Status type definition
  • Constants for each enum value
  • String() method for string representation
  • Parse function to convert strings to enum values
  • Map of codes
  • Slice of all values

Generated Code Features

For each enum defined in your YAML file, the tool generates:

  1. A type definition (string, int, etc.)
  2. Constants for each enum value
  3. A map of code values (if provided)
  4. A slice containing all enum values
  5. A String() method for string representation
  6. A Parse function to convert strings to enum values
  7. A Normalize() method for string enums

Configuration File Format

enums:
  - name: EnumName      # Name of the enum type
    methods:
    - string
    - parse
    - normalize
    - json
    - sql
    type: string|int    # Type of the enum (string, int, etc.)
    values:
      - name: ValueName     # Name of the enum constant
        display: "Display"  # String representation (optional)
        code: value         # Optional "Code" field → supports multiple types:

🔌 MCP Integration

This project can also be used as an MCP server so Claude Desktop can call its tools directly.

👉 See MCP_GUIDE.md for full setup instructions.


📚 References

Project Purpose
mcp-go Go implementation of the Model Context Protocol.
mcp-grafana Example MCP server integrating with Grafana.

License

MIT License

About

The kd-gen is a Go code generation tool designed to create type-safe enums from YAML configuration files. This tool automates the generation of Go enum types along with their associated helper methods, enhancing code safety and reducing boilerplate.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors