Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lggomez/go-enum
Typesafe enum generation in golang
https://github.com/lggomez/go-enum
enum generator go golang scaffolder scaffolding types
Last synced: 2 days ago
JSON representation
Typesafe enum generation in golang
- Host: GitHub
- URL: https://github.com/lggomez/go-enum
- Owner: lggomez
- License: mit
- Created: 2020-06-30T02:25:10.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-17T20:43:51.000Z (almost 3 years ago)
- Last Synced: 2024-11-25T14:51:39.773Z (2 months ago)
- Topics: enum, generator, go, golang, scaffolder, scaffolding, types
- Language: Go
- Homepage:
- Size: 172 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![Go](https://github.com/lggomez/go-enum/workflows/Go/badge.svg?branch=master)
[![Build Status](https://travis-ci.org/lggomez/go-enum.svg?branch=master)](https://travis-ci.org/lggomez/go-enum)
[![codecov](https://codecov.io/gh/lggomez/go-enum/branch/master/graph/badge.svg)](https://codecov.io/gh/lggomez/go-enum)
[![GoDoc](https://godoc.org/github.com/lggomez/go-enum?status.svg)](https://pkg.go.dev/github.com/lggomez/go-enum?tab=doc)
[![Release](https://img.shields.io/github/release/lggomez/go-enum.svg?style=flat-square)](https://github.com/lggomez/go-enum/releases)# go-enum - Typesafe enum generation in golang
## Update 2022
Go 1.18 is out, with generics support: https://go.dev/blog/why-generics
Having said that, I haven't yet explored the syntax and semantics to work on a new way around to the problem of enumerations but still, I'd like to consider this package deprecated (as someone else will probable figure that out in a nice generic enum package :shipit:)
## Intro
This package provides a scaffolding mechanism to generate enum types based on string values, in contrast with the _iota_ based integer constants enum pattern commonly used in Go
Use this package if you wish to have type support for:
* Traversable enumerations
* Enum fields compatible with several (un)marshal interfaces:
* Stringer
* json.Marshaler, json.Unmarshaler
* text.Marshaler, text.Unmarshaler
* json.Marshaler, json.Unmarshaler
* gob.GobEncoder, gob.GobDecoder
* driver.Valuer, sql.Scanner
* bson.Marshaler, bson.Unmarshaler (from [go.mongodb.org/mongo-driver/bson](https://godoc.org/go.mongodb.org/mongo-driver/bson) package)
* Ability to perform type-safe comparisons at runtime against strings and instances of the same enum typeFor more information on the enumeration type API, see the [example](https://pkg.go.dev/github.com/lggomez/go-enum@v0.4.0/example/enum?tab=doc#SpecialThing):
## Usage
This generator uses the `go generate` command, and as such, its pattern must be used in order to generate code
As shown in the example package, 2 files are needed:
#### example.go
```go
package example//go:generate go run gen_enums.go
```#### gen_enums.go
```go
// +build ignorepackage main
import (
"fmt"
"os""github.com/lggomez/go-enum/generator"
)// This example generates the 'Ghost', 'SpecialThing' and 'CountriesIso31661' enums inside of an 'enum' subpackage
// To generate them on the current package instead, just use the current directory path (".")
func main() {
generator.GenerateEnumTypes(
generator.Options{
PackageDirectoryPath: fmt.Sprintf(".%venum", string(os.PathSeparator)),
PackageImportPath: "github.com/lggomez/go-enum/example/enum",
ValueIdentifierCasing: generator.CamelCase,
OmitGeneratedNotice: false,
OmitTests: false,
OmitNameSanitization: false,
OmitSourceFormatting: false,
},
generator.StringEnumDefinition{
Name: "Ghost",
Values: []string{"Blinky", "Pinky", "Inky", "Clyde"},
},
generator.StringEnumDefinition{
Name: "SpecialThing",
Values: []string{"Foo", "Bar", "Baz", "Quux"},
},
)
generator.GenerateEnumTypes(
generator.Options{
PackageDirectoryPath: fmt.Sprintf(".%venum", string(os.PathSeparator)),
PackageImportPath: "github.com/lggomez/go-enum/example/enum",
ValueIdentifierCasing: generator.UpperCase,
OmitGeneratedNotice: false,
OmitTests: false,
OmitNameSanitization: false,
OmitSourceFormatting: false,
},
generator.StringEnumDefinition{
Name: "CountriesISO3166-1",
Values: []string{"Ca", "Uy", "Us", "Ar"},
},
)
}```
With both files present in the same directory/package, execute the `go generate` command on that directory
Based on this example, it will generate an `enum` subpackage with the `Ghost` and `Thing` enum types, along with its iterators `EnumGhost` and `EnumThing`