ggenums

command module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 11 Imported by: 0

README ยถ

GGenums - Generate Golang Enums

Go Report Card GoDoc

GGenums is a code generation tool that makes working with enums in Go easier and safer. 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

go install github.com/Gandalf-Le-Dev/ggenums@latest

[!NOTE] Make sure you have the go/bin directory in your PATH:

export PATH=$PATH:$(go env GOPATH)/bin

Quick Start

  1. Define your enum using a comment:

    //enum:name=Status values=pending,active,in_progress,completed
    
  2. Run the generator:

    ggenums ./...
    
  3. Use your generated enum:

    status := StatusPending
    if status.IsValid() {
        fmt.Println(status) // Output: pending
    }
    

Example

package main

import (
    "encoding/json"
    "fmt"
)

//enum:name=Status values=pending,active,in_progress,completed
//enum:name=Role values=admin,user,guest

type 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:

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 file for details.

Documentation ยถ

The Go Gopher

There is no documentation for this package.

Directories ยถ

Path Synopsis
examples/basic/main.go
examples/basic/main.go
enums
Code generated by ggenums; DO NOT EDIT.
Code generated by ggenums; DO NOT EDIT.

Jump to

Keyboard shortcuts

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