Skip to content

yyle88/goenum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

GOENUM

Go enum code generation that enables different business domains to share common enum names like OK, ERROR, PENDING without naming conflicts through namespace isolation.


CHINESE README

ไธญๆ–‡่ฏดๆ˜Ž

Features

๐Ÿ”’ Namespace Isolation - Each domain has its own enum space, preventing naming conflicts โšก Enum Collection - Generated Enums() returns collection with Lookup, List, Get methods ๐ŸŽฏ Clean Code - Intuitive syntax that matches business logic patterns โœ… Compile Protection - Catch enum misuse at build time, not runtime ๐ŸŒ Multi-Language - Generate enums using various language characters

Installation

go get github.com/yyle88/goenum

Usage

Go lacks true enum namespaces. Different domains can't share common value names like OK, ERROR, PENDING.

Before: Verbose Prefixes Required

type PackageStatus string
const (
    PackagePending   PackageStatus = "PENDING"
    PackageConfirmed PackageStatus = "CONFIRMED"
    PackageShipped   PackageStatus = "SHIPPED"
    PackageDelivered PackageStatus = "DELIVERED"
)

type PaymentStatus string
const (
    PaymentPending PaymentStatus = "PENDING"
    PaymentFailed  PaymentStatus = "FAILED"
    PaymentSuccess PaymentStatus = "SUCCESS"
    PaymentRefund  PaymentStatus = "REFUND"
)
// Verbose switch statements with long prefixes
func processPackage(status string) {
    switch PackageStatus(status) {
    case PackagePending:
        // handle pending
    case PackageConfirmed:
        // handle confirmed
    case PackageShipped:
        // handle shipped
    case PackageDelivered:
        // handle delivered
    }
}
func processPayment(status string) {
    switch PaymentStatus(status) {
    case PaymentPending:
        // handle pending
    case PaymentFailed:
        // handle failed
    case PaymentSuccess:
        // handle success
    case PaymentRefund:
        // handle refund
    }
}

With GOENUM: Clean Namespace Methods

// Each domain gets its own clean namespace
func processPackage(status string) {
    switch PackageStatusEnum(status) {
    case PackageStatus.Pending():
        // handle pending
    case PackageStatus.Confirmed():
        // handle confirmed
    case PackageStatus.Shipped():
        // handle shipped
    case PackageStatus.Delivered():
        // handle delivered
    }
}
func processPayment(status string) {
    switch PaymentStatusEnum(status) {
    case PaymentStatus.Pending():
        // handle pending
    case PaymentStatus.Failed():
        // handle failed
    case PaymentStatus.Success():
        // handle success
    case PaymentStatus.Refund():
        // handle refund
    }
}

Enum Validation with Enums()

Each generated enum type has an Enums() method returning a collection that supports validation and lookup:

// Validate enum value
if _, ok := PackageStatus.Enums().Lookup(status); !ok {
    return errors.New("invalid package status")
}

// Get all valid enum values
allStatuses := PackageStatus.Enums().List()

Multi-Language Support

GOENUM supports enum generation in multiple languages:

// Chinese enum usage example
func processTask(status string) {
    taskStatus := TaskStatusEnum(status)
    switch taskStatus {
    case TaskStatus.Cๅพ…ๅค„็†():
        // handle pending task
    case TaskStatus.Cๅทฒ็กฎ่ฎค():
        // handle confirmed task
    case TaskStatus.C่ฟ›่กŒไธญ():
        // handle active task
    case TaskStatus.CๅทฒๅฎŒๆˆ():
        // handle completed task
    }
}
// Traditional Chinese enum example
func processPermission(status string) {
    permStatus := PermissionStatusEnum(status)
    switch permStatus {
    case PermissionStatus.C้–‹ๅ•Ÿ():
        // handle enabled permission
    case PermissionStatus.C้—œ้–‰():
        // handle disabled permission
    }
}
// Japanese enum example
func processConnection(status string) {
    connStatus := ConnectionStatusEnum(status)
    switch connStatus {
    case ConnectionStatus.CๆŽฅ็ถš():
        // handle connected
    case ConnectionStatus.Cๅˆ‡ๆ–ญ():
        // handle disconnected
    case ConnectionStatus.Cๅพ…ๆฉŸ():
        // handle waiting
    }
}
// Korean enum example
func processGame(status string) {
    gameStatus := GameStatusEnum(status)
    switch gameStatus {
    case GameStatus.C์‹œ์ž‘():
        // handle game start
    case GameStatus.C์ข…๋ฃŒ():
        // handle game end
    case GameStatus.C์ผ์‹œ์ •์ง€():
        // handle game pause
    }
}

Examples: See examples


Related Projects

  • enum - Go enum collection management with metadata support and map-based lookup
  • goenum - Go enum code generation with namespace isolation (this package)
  • protoenum - Protocol Buffers enum code generation with type-safe operations

๐Ÿ“„ License

MIT License - see LICENSE.


๐Ÿ’ฌ Contact & Feedback

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • ๐Ÿ› Mistake reports? Open an issue on GitHub with reproduction steps
  • ๐Ÿ’ก Fresh ideas? Create an issue to discuss
  • ๐Ÿ“– Documentation confusing? Report it so we can improve
  • ๐Ÿš€ Need new features? Share the use cases to help us understand requirements
  • โšก Performance issue? Help us optimize through reporting slow operations
  • ๐Ÿ”ง Configuration problem? Ask questions about complex setups
  • ๐Ÿ“ข Follow project progress? Watch the repo to get new releases and features
  • ๐ŸŒŸ Success stories? Share how this package improved the workflow
  • ๐Ÿ’ฌ Feedback? We welcome suggestions and comments

๐Ÿ”ง Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


๐ŸŒŸ Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • โญ Give GitHub stars if this project helps you
  • ๐Ÿค Share with teammates and (golang) programming friends
  • ๐Ÿ“ Write tech blogs about development tools and workflows - we provide content writing support
  • ๐ŸŒŸ Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰


GitHub Stars

Stargazers

About

Generates enum code, filling the gap of Go enums lacking namespace.

Resources

License

Stars

Watchers

Forks

Packages

No packages published