Git Product home page Git Product logo

enums's Introduction

enums: a Go enum generator

enums uses go generate functionality to produce text/template encoded go files within the same folder, that hold the enum like constant constructs.

in contrast to other approaches enums parses a special comment section to compose the enum type definition - instead of AST parsing to get the specs. enums will create a go file in the same folder sharing the package name and including all code lines to implement the enum. therefore the the enum's name, type and const definition should not be given in the code of the go file that calls the generator - otherwise a build error because of duplicate definition might occur.

enums deals with double values by commenting out lines that hinder building the output file. try out the example to learn more.

load & install

  $ go get -u github.com/AndreasBriese/enums

make shure enums is installed at a location (usually $GOBIN) that is included in $PATH - otherwise go generate migth fail with a cmd-not-found-error.

usage

for the enum(s) to be generated include a line //go:generate enums $GOFILE in your go file and in the same file give the enum(s) specifics in a commented out block starting with // [@enums and ending with // @] like the below:

//go:generate enums $GOFILE

// [@enums
// type colors int
// -Grau colors = iota + 1
// -Weiss
// -Gelb
// -Lila = Weiss
// @]

and call enums in the folder containing the go file by

  $ go generate <YOURFile.go>

in the same folder will result in a file "colors_enums.go" that holds this code:

type colors int

const (
	Grau colors = (iota + 1) * 5
	Weiss
	Gelb
	Lila = Weiss
)

var colors_Constants = [7]colors{
	Grau,
	Weiss,
	Gelb,
	Lila,
}

func (t colors) String() string {
	switch t {
	case Grau:
		return "Grau"
	case Weiss:
		return "Weiss"
	case Gelb:
		return "Gelb"
//	case Lila:
//		return "Lila"
	}
	return ""
}

colors has got a method String() satisfying the interface fmt.Stringer to print out the color names and an array colors_Constants that can be used to loop over the enum i.e. to search for a color constant.

note that eventually all information on the type will be public if given a Uppercase type name in the encoding section.

try

change into example folder and run

$ go generate 
generated enum type 'colors' in colors_enums.go
generated enum type 'values' in values_enums.go
$ go run *.go
List of colors constants:
colors_Constants[0] = 1 corresponds to the constant named "Grau"
colors_Constants[1] = 2 corresponds to the constant named "Weiss"
colors_Constants[2] = 3 corresponds to the constant named "Gelb"
colors_Constants[3] = 4 corresponds to the constant named "Grün"
colors_Constants[4] = 5 corresponds to the constant named "Rot"
colors_Constants[5] = 6 corresponds to the constant named "Blau"
colors_Constants[6] = 2 corresponds to the constant named "Weiss"
List of values constants:
values_Constants[0] = 3.141593 corresponds to the constant named "pi"
values_Constants[1] = 6.283185 corresponds to the constant named "pi2"
values_Constants[2] = 9.424778 corresponds to the constant named "pi3"
values_Constants[3] = 12.566371 corresponds to the constant named "pi4"
values_Constants[4] = 3.141593 corresponds to the constant named "pi"
values_Constants[5] = 31.415927 corresponds to the constant named "pi10"

License

MIT-License

enums's People

Contributors

andreasbriese avatar rr32btg avatar

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.