Skip to content

neoxelox/enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

enum Integration

📑 Golang Enumerators 📑

What

Enum is a package that provides simple enumerators for Go, with IDE autocompletion and any type support. It may not be the prettiest syntax, but it's so useful.

Install

go get github.com/neoxelox/enum

Usage

Create Enum

package main

import (
	"fmt"

	"github.com/neoxelox/enum"
)

type State string

type enumStates = struct {
	COMMITTED   State  // You can use any type you want, for example an int.
	IN_PROGRESS State
	DONE        State
	enum.Enum
}

var States = enum.New(&enumStates{
	COMMITTED:   "COMMITTED",
	IN_PROGRESS: "BLOCKED",
	DONE:        "DONE",
}).(*enumStates)

func main() {
	fmt.Println(States.DONE)
}

Creating a custom type, from a primitive type, for the enum fields (as in the example above), will provide lite type assertion, that is, States.COMMITTED == "COMMITTED" will evaluate to true. If you want full type assertion, you can create type State struct{ string }, and use that type as the type for the enum fields COMMITTED: State{"COMMITTED"}. Now you can't compare States.COMMITTED == "COMMITTED". However, you will need to create your own String, Marshalling, Text... methods, to deal with serialization correctly.

Check if Alias is in Enum

States.Is("COMMITTED")  // true
States.Is("COMPLETED")  // false
States.Is("BLOCKED")  // false

Get Enum Aliases

States.Aliases()  // [COMMITTED IN_PROGRESS DONE]

Check if Value is in Enum

States.In(State("COMMITTED"))  // true
States.In(State("BLOCKED"))  // true
States.In(State("IN_PROGRESS"))  // false
States.In(State("COMPLETED"))  // false

Get Enum Values

States.Values()  // [COMMITTED BLOCKED DONE]

See GoDev for further documentation.

Contribute

Feel free to contribute to this project : ) .

License

This project is licensed under the MIT License - read the LICENSE file for details.