enum

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: May 2, 2021 License: MIT Imports: 1 Imported by: 0

README

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.

Documentation

Overview

Package enum implements an enumerator and functions to work with.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(enum enumerator) enumerator

New creates a new Enum.

Types

type Enum

type Enum struct {
	// contains filtered or unexported fields
}

Enum describes an Enumerator.

func (*Enum) Aliases

func (e *Enum) Aliases() []string

Aliases returns all the aliases of the enum.

func (*Enum) In

func (e *Enum) In(value interface{}) bool

In checks whether a value is present in the enum or not.

func (*Enum) Is

func (e *Enum) Is(alias string) bool

Is checks whether an alias is present in the enum or not.

func (*Enum) Values

func (e *Enum) Values() []interface{}

Values returns all the values of the enum.

Jump to

Keyboard shortcuts

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