Skip to content

Generates lookup table and parse methods based on a basic golang enum

Notifications You must be signed in to change notification settings

marklonquist/go-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-Enum

Go-Enum is a tool to automagically generate String() and Parse() functions in generated files located next to the enum file.

Commands

goenum --path <PATH> (--debug)

If debug mode is enabled, it will print which enums files have received treatment and their path.

Example

For a file looking like the below code:

package relationstate

// x-enum
type RelationState int

const (
    Pending RelationState = iota
    Connected
)

The below code will me automagically be generated:

// Generated code. DO NOT EDIT.
package relationstate

import "errors"

func (kind RelationState) String() string {
    switch kind { 
    case 0:
        return "Pending" 
    case 1:
        return "Connected" 
    default:
        return ""
    }
}

func Parse(name string) (RelationState, error) {
    switch name { 
    case "Pending":
        return RelationState(0), nil 
    case "Connected":
        return RelationState(1), nil 
    default:
        return RelationState(0), errors.New("Enum for \"RelationState\" not found using name = " + name)
    }
}

About

Generates lookup table and parse methods based on a basic golang enum

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages