go-create-enum

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Go Enum Generator

Go 枚举生成器

Generate type-safe Go enums following GO modular architechture conventions.
遵循Go模块化架构规范生成类型安全的Go枚举。

Pattern

规范

Place enums in
internal/modules/<module>/enum/<name>_enum.go
.
Each enum file contains:
  1. String constants for each enum value
  2. Validation map for O(1) lookups
  3. Enum struct type
  4. Constructor with validation (
    New<Type>Enum
    )
  5. String()
    method
  6. Private validation function (
    validate<Type>
    )
将枚举放置在
internal/modules/<module>/enum/<name>_enum.go
中。
每个枚举文件包含:
  1. 每个枚举值的字符串常量
  2. 用于O(1)查找的验证映射
  3. 枚举结构体类型
  4. 带验证的构造函数(
    New<Type>Enum
  5. String()
    方法
  6. 私有验证函数(
    validate<Type>

Example Structure

示例结构

For an enum named "ContactType" with values "email" and "webhook":
go
package enum

import "github.com/cristiano-pacheco/pingo/internal/modules/monitor/errs"

const (
	ContactTypeEmail   = "email"
	ContactTypeWebhook = "webhook"
)

var validContactTypes = map[string]struct{}{
	ContactTypeEmail:   {},
	ContactTypeWebhook: {},
}

type ContactTypeEnum struct {
	value string
}

func NewContactTypeEnum(value string) (ContactTypeEnum, error) {
	if err := validateContactType(value); err != nil {
		return ContactTypeEnum{}, err
	}
	return ContactTypeEnum{value: value}, nil
}

func (e ContactTypeEnum) String() string {
	return e.value
}

func validateContactType(contactType string) error {
	if _, ok := validContactTypes[contactType]; !ok {
		return errs.ErrInvalidContactType
	}
	return nil
}
对于名为"ContactType"、值为"email"和"webhook"的枚举:
go
package enum

import "github.com/cristiano-pacheco/pingo/internal/modules/monitor/errs"

const (
	ContactTypeEmail   = "email"
	ContactTypeWebhook = "webhook"
)

var validContactTypes = map[string]struct{}{
	ContactTypeEmail:   {},
	ContactTypeWebhook: {},
}

type ContactTypeEnum struct {
	value string
}

func NewContactTypeEnum(value string) (ContactTypeEnum, error) {
	if err := validateContactType(value); err != nil {
		return ContactTypeEnum{}, err
	}
	return ContactTypeEnum{value: value}, nil
}

func (e ContactTypeEnum) String() string {
	return e.value
}

func validateContactType(contactType string) error {
	if _, ok := validContactTypes[contactType]; !ok {
		return errs.ErrInvalidContactType
	}
	return nil
}

Generation Steps

生成步骤

  1. Identify enum details:
    • Enum name (e.g., "ContactType", "Status", "Priority")
    • Possible values (e.g., ["email", "webhook"], ["active", "inactive"])
    • Target module (e.g., "monitor", "auth")
  2. Create error constant:
    • Add error to
      internal/modules/<module>/errs/errs.go
    • Format:
      ErrInvalid<EnumName> = errors.New("invalid <enum_name>")
    • Example:
      ErrInvalidContactType = errors.New("invalid contact type")
  3. Generate enum file:
    • Filename:
      <snake_case_enum_name>_enum.go
    • Package:
      enum
    • Import module errs package
    • Follow structure above with all components
  1. 识别枚举细节:
    • 枚举名称(例如:"ContactType"、"Status"、"Priority")
    • 可能的值(例如:["email", "webhook"]、["active", "inactive"])
    • 目标模块(例如:"monitor"、"auth")
  2. 创建错误常量:
    • 将错误添加到
      internal/modules/<module>/errs/errs.go
    • 格式:
      ErrInvalid<EnumName> = errors.New("invalid <enum_name>")
    • 示例:
      ErrInvalidContactType = errors.New("invalid contact type")
  3. 生成枚举文件:
    • 文件名:
      <snake_case_enum_name>_enum.go
    • 包名:
      enum
    • 导入模块的errs包
    • 遵循上述所有组件的结构

Naming Conventions

命名规范

  • File:
    <snake_case>_enum.go
    (e.g.,
    contact_type_enum.go
    )
  • Constants:
    <EnumName><Value>
    (e.g.,
    ContactTypeEmail
    )
  • Validation map:
    valid<EnumName>s
    (lowercase, plural)
  • Struct:
    <EnumName>Enum
    (e.g.,
    ContactTypeEnum
    )
  • Constructor:
    New<EnumName>Enum
  • Validator:
    validate<EnumName>
    (private, singular, takes string param)
  • Error:
    ErrInvalid<EnumName>
    in module's
    errs
    package
  • 文件
    <snake_case>_enum.go
    (例如:
    contact_type_enum.go
  • 常量
    <EnumName><Value>
    (例如:
    ContactTypeEmail
  • 验证映射
    valid<EnumName>s
    (小写、复数)
  • 结构体
    <EnumName>Enum
    (例如:
    ContactTypeEnum
  • 构造函数
    New<EnumName>Enum
  • 验证器
    validate<EnumName>
    (私有、单数、接收字符串参数)
  • 错误:模块
    errs
    包中的
    ErrInvalid<EnumName>

Implementation Checklist

实现检查清单

  • Add
    ErrInvalid<EnumName>
    to
    internal/modules/<module>/errs/errors.go
  • Create
    internal/modules/<module>/enum/<name>_enum.go
  • Define all constant values
  • Create validation map with all values
  • Define enum struct type (private
    value string
    field)
  • Implement
    New<EnumName>Enum(value string) (<EnumName>Enum, error)
    constructor
  • Implement
    String() string
    method
  • Implement
    validate<EnumName>(value string) error
    private function
  • internal/modules/<module>/errs/errors.go
    添加
    ErrInvalid<EnumName>
  • 创建
    internal/modules/<module>/enum/<name>_enum.go
  • 定义所有常量值
  • 创建包含所有值的验证映射
  • 定义枚举结构体类型(私有
    value string
    字段)
  • 实现
    New<EnumName>Enum(value string) (<EnumName>Enum, error)
    构造函数
  • 实现
    String() string
    方法
  • 实现
    validate<EnumName>(value string) error
    私有函数

Usage Pattern

使用模式

Other code creates enums via the constructor (validation happens internally):
go
// Create typed enum (validation is automatic)
contactType, err := enum.NewContactTypeEnum(input)
if err != nil {
    return err
}
fmt.Println(contactType.String()) // "email"

// Use constants directly when value is known at compile time
const defaultType = enum.ContactTypeEmail
其他代码通过构造函数创建枚举(验证在内部完成):
go
// 创建类型化枚举(自动验证)
contactType, err := enum.NewContactTypeEnum(input)
if err != nil {
    return err
}
fmt.Println(contactType.String()) // "email"

// 当编译时已知值时直接使用常量
const defaultType = enum.ContactTypeEmail