Giter Site home page Giter Site logo

go-ultra-enum's Introduction

go-ultra-enum

go-ultra-enum is an enum generator for Go. It is inspired by the powerful enum types found in Java. go-ultra-enum has the following capabilities

  • Reference and compare enums using values
  • Support multi value type, such as int, bool.

Install

From a github release

go get -u github.com/drinks5/go-ultra-enum

Example

To define an enum, create a struct with the suffix Enum. You can define a display value in the struct tag. Adding a hyphen will assign the field name to the display value.

You can then generate the enum as follows.

//go:generate go-ultra-enum
type ColorEnum struct {
	Red   int `enum:"1"`
	Blue  int `enum:"2"`
	Grenn int `enum:"3"`
}

type CountryEnum struct {
	China    int64 `enum:"1"`
	America  int64 `enum:"2"`
	Sinapore int64 `enum:"3"`
}

// generate an enum with default display values. The display values are set to the field names, e.g. `On` and `Off`
type StatusEnum struct {
	On  bool `enum:"true"`
	Off bool `enum:"false"`
}

// generate an enum with display values and descriptions.
type SushiEnum struct {
	Maki    string `enum:"MAKI,Rice and filling wrapped in seaweed"`
	Temaki  string `enum:"TEMAKI,Hand rolled into a cone shape"`
	Sashimi string `enum:"SASHIMI,Fish or shellfish served alone without rice"`
}

When a description is defined the json is serialized as follows

Color.Red.MarshalJson()
{
  "sushi": {
    "name": "SASHIMI",
    "description": "Fish or shellfish served alone without rice"
  }
}

Consumer api

The generated code would yield the following consumer api

Get an enum value

Color.Red

Get the display value

var name string = a.Name // "RED"

Get all display values

var names []string = Color.Names() // []string{"RED", "BLUE"}

Get all values

var values []Color = Color.Values() // []string{Red, Blue}

UnMarshal name

c := Color.UnMarshalName("RED)

UnMarshal value

v := Color.UnMarshalValue(1)

Developing

go build main.go
go generate
go test .

OR

make test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.