goenumcodegen

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: MIT Imports: 13 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:

$ go install github.com/ejfrick/go-enum-codegen/cmd/go-enum-codegen@latest
$ go-enum-codegen -version

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

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

Usage

Usage of go-enum-codegen:
  -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 [./examples]

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) ParsePackage

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

func (*Generator) Printf

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

func (*Generator) WritePreamble

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

type Opt

type Opt func(g *Generator)

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
go-enum-codegen command
examples

Jump to

Keyboard shortcuts

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