Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gandalf-le-dev/ggenums
Golang Generate Enums
https://github.com/gandalf-le-dev/ggenums
enums golang
Last synced: 3 days ago
JSON representation
Golang Generate Enums
- Host: GitHub
- URL: https://github.com/gandalf-le-dev/ggenums
- Owner: Gandalf-Le-Dev
- Created: 2024-02-27T20:54:32.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-11-22T14:28:15.000Z (about 2 months ago)
- Last Synced: 2024-11-22T15:31:29.792Z (about 2 months ago)
- Topics: enums, golang
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# GGenums - Generate Golang Enums
[![Go Report Card](https://goreportcard.com/badge/github.com/Gandalf-Le-Dev/ggenums)](https://goreportcard.com/report/github.com/Gandalf-Le-Dev/ggenums)
[![GoDoc](https://godoc.org/github.com/Gandalf-Le-Dev/ggenums?status.svg)](https://godoc.org/github.com/Gandalf-Le-Dev/ggenums)GGenums is a code generation tool that ease use of enums in Go. It automatically generates type-safe enums with built-in string conversion, JSON marshaling/unmarshaling, and validation.
## Features
- 🚀 Simple enum declaration using comments
- 🔒 Type-safe enum values
- 📦 JSON marshaling/unmarshaling support
- ✅ Built-in validation
- 🔄 String conversion methods
- 📝 Easy to read generated code## Installation
```bash
go install github.com/Gandalf-Le-Dev/ggenums@latest
```> [!NOTE]
> Make sure you have the go/bin directory in your PATH:
>
> ```bash
> export PATH=$PATH:$(go env GOPATH)/bin
> ```## Quick Start
1. Define your enum using a comment:
```go
//enum:name=Status values=pending,active,in_progress,completed
```2. Run the generator:
```bash
ggenums ./...
```3. Use your generated enum:
See the example below or [here](./example)
## Example
```go
package mainimport (
"encoding/json"
"fmt"
)//enum:name=Status values=pending,active,in_progress,completed
//enum:name=Role values=admin,user,guesttype User struct {
ID string `json:"id"`
Status StatusEnum `json:"status"`
Role RoleEnum `json:"role"`
}func main() {
var err error
// JSON marshaling
user := User{
ID: "123",
Status: StatusPending,
Role: RoleAdmin,
}jsonData, _ := json.Marshal(user)
fmt.Printf("JSON: %s\n", jsonData)// JSON unmarshaling
var parsed User
json.Unmarshal(jsonData, &parsed)
fmt.Printf("Parsed: %+v\n", parsed)// Parsing from string
status, _ := ParseStatus("in_progress")
fmt.Printf("Parsed status: %s\n", status)// Validation
err = status.Validate()
if err != nil {
fmt.Printf("Validation error: %v\n", err)
} else {
fmt.Printf("Status '%s' is valid\n", status)
}
// Comparison
fmt.Printf("Is status in progress? %v\n", status == StatusInProgress)// Error handling
_, err = ParseStatus("error_status")
fmt.Printf("Invalid status error: %v\n", err)
}
```Output:
```bash
JSON: {"id":"123","status":"pending","role":"admin"}
Parsed: {ID:123 Status:pending Role:admin}
Parsed status: in_progress
Status 'in_progress' is valid
Is status in progress? true
Invalid status error: invalid Status: error_status
```## Generated Code Features
The generated code includes:
- Type-safe enum constants
- String conversion methods
- JSON marshaling/unmarshaling
- Validation methods
- Parsing functions
- Slice of all enum values## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.