Skip to content

Repository files navigation

Kokomo

Kokomo is a high-performance Go code generator inspired by Java's project-lombok. It leverages Go's AST and comment groups to inject boilerplate-reducing code directly into your project, keeping your source files clean and focused on business logic.

Overview

In Go, boilerplate like getters, setters, constructors, and stringers can often clutter structs. Kokomo solves this by using comment-based directives that function similarly to Java annotations. These directives are processed at development time to generate additional Go code.

The Directive Syntax

Directives follow the pattern: // kokomo:<plugin> [<flag>...] [<arg>...]

These directives are typically placed in a comment group immediately preceding the AST node (such as a struct) they apply to.

Key Features

  • Issue Tracking: Issues are managed through git-bug.

  • Workflow: All changes must be submitted via Pull Request.

  • Boilerplate Reduction: Automatically generate common methods like Getters, Setters, Builders, and Stringers.

  • Package-Wide Generation: Unless --target or --output is specified, Kokomo scans the directory hierarchy and generates a single kokomo_gen.go (or kokomo_gen_test.go for tests) per package.

  • Plugin Architecture: Built using cobra.Command, providing a robust CLI and built-in help. Plugins are registered at compile time but can be extended by users.

  • AST-Aware: Plugins receive the full directive and the associated AST node, allowing for deep inspection and precise code generation.

  • Robust Code Generation: Uses the jen package to ensure that all generated code is syntactically valid and properly formatted.

  • Field Tag Integration: Many plugins can leverage Go struct tags to further refine their generation logic.

Planned Plugins (Lombok-Inspired)

Kokomo aims to provide a suite of plugins that map directly to the most useful features of project-lombok:

Plugin Description Go Equivalent/Use Case
getter Generates getter methods for struct fields. Exporting unexported fields via methods.
setter Generates setter methods for struct fields. Encapsulating field updates.
stringer Generates a String() method. Implementation of the fmt.Stringer interface.
equals Generates equality comparison methods. Deep or shallow equality checks.
hash Generates hash methods. Support for use in map keys or hash sets.
constructor Generates various constructor functions. NewStructName, NewStructWithArgs, etc.
builder Generates a fluent Builder pattern. Complex object construction.
data A composite plugin. Combines Getter, Setter, Stringer, Equals, and Hash.
value Generates immutable-style structures. Read-only structs with minimal methods.
required_args Generates constructors with mandatory fields. Used with data or for value types.
with Generates "with" methods for immutable types. Creates deep copies with modified fields.
singleton Generates a thread-safe singleton. Lazy initialization using sync.Once.

Future Considerations

We are evaluating the following plugins for future iterations:

  • delegate: To facilitate method proxying to internal fields.
  • singular: To provide specialized collection handling within builders.

Usage

For detailed contribution guidelines, development workflows, and testing standards, please refer to CONTRIBUTING.md.

# Generate code for the current directory and all subdirectories
kokomo generate .

# Generate code for a specific package into a specific file
kokomo generate ./pkg/user --target user.go --output user_gen.go

# Dry run: output generated code to stdout
kokomo generate ./pkg/user --target user.go --output -

Note: Using --target on a file will cause the generated code to overwrite that file entirely.

Example directive in code:

// kokomo:getter
// kokomo:stringer
type User struct {
    id   int
    name string
}

About

Go generation plugins for common patterns

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages