goenumcodegen

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 14 Imported by: 0

README

go-enum-codegen

go-enum-codegen is a command-line tool that generates methods to satisfy json.Marshaler, json.Unmarshaler, sql.Scanner, & driver.Valuer interfaces for common enum patterns.

With the default settings and given a type MyEnum that is a string or integer type, go-enum-codegen will create a new self-contained Go source file implementing:

// myenum.gen.go
// Code generated by "go-enum-codegen -type MyEnum"; DO NOT EDIT

package mypackage

func (t MyEnum) MarshalJSON() ([]byte, error) {
	...
}

func (t *MyEnum) UnmarshalJSON(data []byte) error {
	...
}

func (t MyEnum) Value() (driver.Value, error) {
	...
}

func (t *MyEnum) Scan(v any) error  {
	...
}

Installation

go-enum-codegen can be installed using the recommended tools.go pattern so it can be managed alongside the rest of your module.

To do so, create a file called tools/tools.go at the root of your module with the following contents:

//go:build tools
// +build tools

package main

import (
	_ "github.com/ejfrick/go-enum-codegen/cmd/go-enum-codegen"
)

Then, you can invoke go-enum-codegen like so:

//go:generate go run github.com/ejfrick/go-enum-codegen/cmd/go-enum-codegen -type MyEnum

go-enum-codegen can alternatively be installed as a binary:

# via homebrew
# coming soon!
# via go install
$ go install github.com/ejfrick/go-enum-codegen/cmd/go-enum-codegen@latest
$ go-enum-codegen -version
# from source
$ git clone github.com/ejfrick/go-enum-codegen
$ cd go-enum-codegen && make build && cp build/go-enum-codegen /somewhere/in/your/path

When installed as a binary, it would be invoked as follows:

//go:generate go-enum-codegen -type MyEnum

Usage

Usage of go-enum-codegen:
  -all-func
        generate a function that returns all values of the enum; default false
  -debug
        output debug information about the tool
  -e    same as -error-on-unknown
  -error-on-unknown
        whether to return an error if scanning or unmarshalling an unknown value; automatically set to true when iota is first set to "_" or there is no enum equal to the empty value of its underlying type; otherwise default is false and an unknown value will be assigned to the enum with the empty value of its underlying type
  -h    same as -help.
  -help
        show this help and exit
  -json
        generate only json.Marshaler and json.Unmarshaler methods; default false
  -output string
        output file name; default srcdir/<type>.gen.go
  -sql
        generate only sql.Scanner and driver.Value methods; default false
  -stringer
        use the String() method of the enum instead of the underlying integer value; default false
  -tags string
        comma-separated list of build tags to apply
  -type string
        comma-separated list of type names; must be set
  -version
        show version and exit

Examples

You can find examples for several different scenarios in the examples directory

To-Do

  • Homebrew formula
  • More tests
  • More docs

Feature Requests, Bug Reports, Contributing, Etc.

Please open an issue (or a pull request!)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetReceiver

func GetReceiver(obj *types.Named) string

func IsStringer

func IsStringer(obj *types.Named) bool

func WriteMultiCaseStatement

func WriteMultiCaseStatement(values []Value, receiver string) string

func WriteReadSingleCaseStatement

func WriteReadSingleCaseStatement(values []Value, receiver string, assgnVar string, typeName string, kind ValueType) string

Types

type File

type File struct {
	// contains filtered or unexported fields
}

func (*File) GenDecl

func (f *File) GenDecl(node ast.Node) bool

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

func NewGenerator

func NewGenerator(opts ...Opt) *Generator

func (*Generator) Format

func (g *Generator) Format() ([]byte, error)

func (*Generator) Generate

func (g *Generator) Generate(typeName string) error

func (*Generator) GenerateWithJen added in v0.0.4

func (g *Generator) GenerateWithJen(typeName string, args []string) error

func (*Generator) Output added in v0.0.4

func (g *Generator) Output() []byte

func (*Generator) ParsePackage

func (g *Generator) ParsePackage(patterns []string, tags []string) error

func (*Generator) Printf

func (g *Generator) Printf(format string, args ...interface{})

func (*Generator) WritePreambleAndImports added in v0.0.3

func (g *Generator) WritePreambleAndImports(args []string)

type Opt

type Opt func(g *Generator)

func WithAllFunc added in v0.0.4

func WithAllFunc() Opt

func WithDebug added in v0.0.2

func WithDebug() Opt

func WithErrorOnUnknown

func WithErrorOnUnknown() Opt

func WithOnlyJsonMethods

func WithOnlyJsonMethods() Opt

func WithOnlySQLMethods

func WithOnlySQLMethods() Opt

func WithUseStringer

func WithUseStringer() Opt

type Package

type Package struct {
	// contains filtered or unexported fields
}

type Value

type Value struct {
	Name       string
	ValType    ValueType
	StrVal     string
	IsStringer bool
	RecvName   string
}

type ValueType

type ValueType string
const (
	TypeString   ValueType = "string"
	TypeSigned   ValueType = "int"
	TypeUnsigned ValueType = "uint"
)

Directories

Path Synopsis
cmd
examples
all-func
Code generated by "go-enum-codegen -type MyStringEnum -all-func"; DO NOT EDIT.
Code generated by "go-enum-codegen -type MyStringEnum -all-func"; DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL