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 ¶
- Add `// +genx:enum` comment to your enumeration type.
- Define a custom `int` type and use the `@enum` annotation in its comment to configure the generator (e.g., `// @enum storage=text`).
- 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)
}
}
Output:
Index ¶
- type Enum
- func (e *Enum) ExtendAttributes(ctx context.Context, name string) s.Snippet
- func (e *Enum) ExtendKeys() []string
- func (e *Enum) IsValid() bool
- func (e *Enum) MappingAttributes(ctx context.Context, name string) s.Snippet
- func (e *Enum) MappingKeys() []string
- func (e *Enum) StringToValueCases(ctx context.Context) s.Snippet
- func (e *Enum) ValueToStringCases(ctx context.Context) s.Snippet
- func (e *Enum) ValueToTextCases(ctx context.Context) s.Snippet
- func (e *Enum) Values(ctx context.Context) s.Snippet
- type Enums
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 (*Enum) ExtendKeys ¶ added in v0.3.1
func (*Enum) MappingAttributes ¶ added in v0.3.1
func (*Enum) MappingKeys ¶ added in v0.3.1
func (*Enum) StringToValueCases ¶
StringToValueCases generates code snippet cases from string to const value
func (*Enum) ValueToStringCases ¶
ValueToStringCases generates code snippet cases from enum value to string
func (*Enum) ValueToTextCases ¶
ValueToTextCases generates code snippet cases from enum value to text