https://github.com/nikolaydubina/go-enum-encoding
Generate Go enum encoding
https://github.com/nikolaydubina/go-enum-encoding
code-generation encoding enum json serde
Last synced: about 1 month ago
JSON representation
Generate Go enum encoding
- Host: GitHub
- URL: https://github.com/nikolaydubina/go-enum-encoding
- Owner: nikolaydubina
- License: mit
- Created: 2024-03-20T02:47:21.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-14T06:37:26.000Z (3 months ago)
- Last Synced: 2025-03-31T05:33:10.438Z (3 months ago)
- Topics: code-generation, encoding, enum, json, serde
- Language: Go
- Homepage:
- Size: 1.83 MB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
- trackawesomelist - go-enum-encoding (⭐14) - Code generation for enum encoding from code comments. (Recently Updated / [Dec 24, 2024](/content/2024/12/24/README.md))
README
# go-enum-encoding
[](https://goreportcard.com/report/github.com/nikolaydubina/go-enum-encoding)
[](https://pkg.go.dev/github.com/nikolaydubina/go-enum-encoding)
[](https://codecov.io/gh/nikolaydubina/go-enum-encoding)
[](https://github.com/nikolaydubina/go-recipes)
[](https://securityscorecards.dev/viewer/?uri=github.com/nikolaydubina/go-enum-encoding)```bash
go install github.com/nikolaydubina/go-enum-encoding@latest
```* 200 LOC
* simple, fast, strict
* generate encoding/decoding, tests, benchmarks```go
type Color struct{ c uint8 }//go:generate go-enum-encoding -type=Color
var (
UndefinedColor = Color{} // json:""
Red = Color{1} // json:"red"
Green = Color{2} // json:"green"
Blue = Color{3} // json:"blue"
)
````iota` is ok too
```go
type Size uint8//go:generate go-enum-encoding -type=Size
const (
UndefinedSize Size = iota // json:""
Small // json:"small"
Large // json:"large"
XLarge // json:"xlarge"
)
```generated benchmarks
```bash
$ go test -bench=. -benchmem .
goos: darwin
goarch: arm64
pkg: github.com/nikolaydubina/go-enum-encoding/internal/testdata
cpu: Apple M3 Max
BenchmarkColor_UnmarshalText-16 752573839 1.374 ns/op 0 B/op 0 allocs/op
BenchmarkColor_AppendText-16 450123993 2.676 ns/op 0 B/op 0 allocs/op
BenchmarkColor_MarshalText-16 80059376 13.68 ns/op 8 B/op 1 allocs/op
BenchmarkImageSize_UnmarshalText-16 751743885 1.601 ns/op 0 B/op 0 allocs/op
BenchmarkImageSize_AppendText-16 500286883 2.402 ns/op 0 B/op 0 allocs/op
BenchmarkImageSize_MarshalText-16 81467318 16.46 ns/op 8 B/op 1 allocs/op
BenchmarkImageSize_String-16 856463289 1.330 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/nikolaydubina/go-enum-encoding/internal/testdata 8.561s
```## References
- http://github.com/zarldev/goenums - does much more advanced struct generation, generates all enum utilities besides encoding, does not generate tests, uses similar notation to trigger go:generate but with different comment directives (non-json field tags)
- http://github.com/nikolaydubina/go-enum-example