enumx

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 21 Imported by: 2

Documentation

Overview

Package enumx provides a generator for generating enumeration related implementations.

How to Integrate

import _ "github.com/xoctopus/genx/devpkg/enumx"

ctx := genx.NewContext(&genx.Args{
	Entrypoint: []string{entry},
})
_ = ctx.Execute(context.Background(), genx.Get()...)

Code Definition Conventions

  1. Add `// +genx:enum` comment to your enumeration type.
  2. Define a custom `int` type and use the `@enum` annotation in its comment to configure the generator (e.g., `// @enum storage=text`).
  3. Define constants for your type following the naming conventions below.

Assuming your type is named `Status`, its prefix will be `STATUS` (UpperSnakeCase).

  • Unknown Value: Must be named exactly as `<PREFIX>_UNKNOWN`. Example: `STATUS_UNKNOWN`

  • Specific Values: Must be named as `<PREFIX>__<NAME>` (using a double underscore `__`). Example: `STATUS__ENABLED`

The text description is extracted from the constant's comment. If no comment is provided, the `<NAME>` is used as the fallback text.

Example:

// Status
// +genx:enum
type Status int8

const (
	STATUS_UNKNOWN Status = iota
	// STATUS__ENABLED 关闭
	STATUS__ENABLED
	// STATUS__DISABLED 开启
	STATUS__DISABLED
)
Example
package main

import (
	"context"
	"os"
	"path/filepath"

	"github.com/xoctopus/x/misc/must"

	"github.com/xoctopus/genx/pkg/genx"
)

func main() {
	cwd := must.NoErrorV(os.Getwd())

	entry := filepath.Join(cwd, "..", "testdata", "enumx")

	ctx := genx.NewContext(&genx.Args{
		Entrypoint: []string{entry},
	})

	if err := ctx.Execute(context.Background(), genx.Get()...); err != nil {
		panic(err)
	}

}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enum

type Enum struct {
	// contains filtered or unexported fields
}

func (*Enum) ExtendAttributes added in v0.3.1

func (e *Enum) ExtendAttributes(ctx context.Context, name string) s.Snippet

func (*Enum) ExtendKeys added in v0.3.1

func (e *Enum) ExtendKeys() []string

func (*Enum) IsValid

func (e *Enum) IsValid() bool

func (*Enum) MappingAttributes added in v0.3.1

func (e *Enum) MappingAttributes(ctx context.Context, name string) s.Snippet

func (*Enum) MappingKeys added in v0.3.1

func (e *Enum) MappingKeys() []string

func (*Enum) StringToValueCases

func (e *Enum) StringToValueCases(ctx context.Context) s.Snippet

StringToValueCases generates code snippet cases from string to const value

func (*Enum) ValueToStringCases

func (e *Enum) ValueToStringCases(ctx context.Context) s.Snippet

ValueToStringCases generates code snippet cases from enum value to string

func (*Enum) ValueToTextCases

func (e *Enum) ValueToTextCases(ctx context.Context) s.Snippet

ValueToTextCases generates code snippet cases from enum value to text

func (*Enum) Values

func (e *Enum) Values(ctx context.Context) s.Snippet

Values generates code snippet of const value list

type Enums

type Enums struct {
	// contains filtered or unexported fields
}

func NewEnums

func NewEnums(g genx.Context) *Enums

func (*Enums) Resolve

func (es *Enums) Resolve(t types.Type) (*Enum, bool)

Jump to

Keyboard shortcuts

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