An open API service indexing awesome lists of open source software.

https://github.com/rvflash/genum

An enum generator for go based on CSV data
https://github.com/rvflash/genum

Last synced: 2 months ago
JSON representation

An enum generator for go based on CSV data

Awesome Lists containing this project

README

        

# Genum

[![GoDoc](https://godoc.org/github.com/rvflash/genum?status.svg)](https://godoc.org/github.com/rvflash/genum)
[![Build Status](https://github.com/rvflash/genum/workflows/build/badge.svg)](https://github.com/rvflash/genum/actions?workflow=build)
[![Code Coverage](https://codecov.io/gh/rvflash/genum/branch/main/graph/badge.svg)](https://codecov.io/gh/rvflash/genum)
[![Go Report Card](https://goreportcard.com/badge/github.com/rvflash/genum)](https://goreportcard.com/report/github.com/rvflash/genum)

`genum` parses comma-separated values (CSV) from a file, or the standard input by default to automate
the generation of a Go file, where data becoming constant names and/or values of an enum type.

Given a name of a (signed or unsigned) integer, a float or a string type T and a package name,
`genum` will create a new self-contained Go source file, named by default `.go` with only constant values.

Each CSV line is a new constant, where the first column is the name, and/or the value, depending on the settings,
and the second if provided, is the value.

## Features

* No check is made and records may have a variable number of fields. So we do not need to specify all values,
even names. `_` is used as default constant name.
* Support of the following types: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32,
float64 and string.
* Iota to simplify the code but values from the CVS force the iota to adapt.
* Bitmask to use one integer to hold multiple flags and provide bitwise operations.
* The `String` method can return more than the name of the constant, using the `string_formater`
you can format a value based on the enum name, value and type.
* Add comment on any constant declaration with its value.

See the [examples](examples/) for more use cases.

Using `genum` flags, you can add implementation of the following interfaces:

* fmt.Stringer with customized output, see formatting options to format the return based on enum type, name or value:
```go
func (e T) String() string
```
* encoding.TextMarshaler / encoding.TextUnmarshaler
```go
func (e T) MarshalText() (text []byte, err error)
func (e *T) UnmarshalText(text []byte) error
```
* json.Marshaler / json.Unmarshaler
```go
func (e T) MarshalJSON() ([]byte, error)
func (e *T) UnmarshalJSON([]byte) error
```
* xml.Marshaler / xml.Unmarshaler
```go
func (e T) MarshalXML(e *Encoder, start StartElement) error
func (e *T) UnmarshalXML(d *Decoder, start StartElement) error
```

Or methods:

* `IsValid` checks the validity of a constant.
```go
func (e T) IsValid() bool
```

Or with bitmask enabled, these methods provide bitwise operations:

```go
// Has returns in success if this bitmask has this flag.
func (e T) Has(e2 T) bool
// Set sets this bitflag.
func (e *T) Set(e2 T) bool
// Switch only changes the bitflag if necessary. It returns true if the requested action has been done.
func (e *T) Switch(e2 T, on bool) (done bool)
// Toggle toggles this bitflag.
func (e *T) Toggle(e2 T)
// Unset clears this bitflag.
func (e *T) Unset(e2 T)
```

Typically, this process would be run using the `go generate ./...` command, like this:

```go
//go:generate genum -pkg ${GOPACKAGE} -type hi values.csv
```

## Demo

The command `echo -e "hello\nbonjour\nguten morgen\nhola" | genum -pkg say -name hi` will generate the file `./hi.go`:

```go
// Code generated by "genum -pkg say -type hi"; DO NOT EDIT.

package say

// Hi is an enum.
type Hi int

// List of known Hi enums.
const (
Hello Hi = iota
Bonjour
GutenMorgen
Hola
)

```

### Installation

```shell
GO111MODULE=on go install github.com/rvflash/genum@latest
```

## Usage

```shell
genum [flags] [file]
```

The `genum` command generates a Go file based on the CSV values given in input (file path or standard input).
It supports the following flags:

* `-pkg`: package name
* `-name`: enum type name (default "Enum")
* `-type`: enum base type (default "int")
* `-iota`: declare sequentially growing numeric constants (default true)
* `-output`: output file name; default dst_dir/.go
* `-stringer`: implement the fmt.Stringer interface
* `-stringer_format` format used as returned value by the fmt.Stringer method (default only the enum name: "%[1]s"):
[1] represents the enum name
[2] represents the enum value
[3] represents the enum type
* `-noprefix`: trim the type name from the generated constant names
* `-prefix`: add the type name as prefix of each generated constant names
* `-json`: implement the json.Marshaler and json.Unmarshaler interfaces
* `-text`: implement the encoding.TextMarshaler and encoding.TextUnmarshaler interfaces
* `-xml`: implement the xml.Marshaler and xml.Unmarshaler interfaces
* `-comment`: add in comment the values of generated constants
* `-validator`: add a method "IsValid" to verify the set up of the constant
* `-bitmask`: use one integer to hold multiple flags, provide bitwise operations and overwrite the enum base type with unsigned integer type (size in bits based on the number of values)