Documentation
¶
Overview ¶
Package codex provides a generator for generating error code related implementations.
How to Integrate Generator ¶
import _ "github.com/xoctopus/genx/devpkg/codex"
ctx := genx.NewContext(&genx.Args{
Entrypoint: []string{entry},
})
_ = ctx.Execute(context.Background(), genx.Get("code")...)
Code Definition Conventions ¶
- Add `// +genx:code` on the error code type.
- Define constants with `<PREFIX>_UNDEFINED` and `<PREFIX>__<NAME>` (see genx skill generator-codex.spec.md).
- Optional HTTP mapping: on each constant doc, `// @code api=StatusBadRequest` or `// @code api=http=444`. Invalid api values log a warning and omit that constant from StatusCode(). Unannotated codes map to StatusCode() 0; types with no api omit StatusCode().
- Optional hint: on each constant doc, `// @code hint=...` uses the given text, `// @code hint` uses the doc title. Unannotated codes map to CodeHint() ""; types with no hint omit CodeHint().
Generated Methods ¶
- Message() string: `[<domain>:<value>] <doc title or NAME>`.
- StatusCode() int: only if any constant has `@code api`.
- CodeIdentifier() string: `<domain>__<NAME>`, `<domain>__UNDEFINED` for the undefined constant.
- CodeHint() string: only if any constant has `@code hint`.
- CodeValues() []any: all constants except the undefined one, implementing codex.CanBeCode.
Domain ¶
Default domain is UPPER(package)::UPPER(TypeName). Optional type doc override: `// @code domain=MYDOMAIN`. It is used by both Message() and CodeIdentifier().
Example:
// +genx:code // @code domain=USER type Code int8 const ( CODE_UNDEFINED Code = iota // CODE__NOT_FOUND user not found // @code api=StatusNotFound // @code hint CODE__NOT_FOUND // CODE__INVALID_NAME invalid name // @code api=StatusBadRequest // @code hint=name must be 8-16 letters or digits CODE__INVALID_NAME )
Example ¶
cwd := must.NoErrorV(os.Getwd())
entry := filepath.Join(cwd, "..", "testdata", "codex")
ctx := genx.NewContext(&genx.Args{
Entrypoint: []string{entry},
})
must.NoError(ctx.Execute(context.Background(), genx.Get()...))
Index ¶
- func ResolveStatusAnnoValue(anno string) (string, bool)
- type Error
- func (e *Error) CodeHintCases(ctx context.Context) s.Snippet
- func (e *Error) CodeIdentifierCases(ctx context.Context) s.Snippet
- func (e *Error) CodeMessageCases(ctx context.Context) s.Snippet
- func (e *Error) CodeValues(ctx context.Context) s.Snippet
- func (e *Error) IsValid() bool
- func (e *Error) StatusDescriberCases(ctx context.Context) s.Snippet
- type Errors
- type HTTPStatusRegistry
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ResolveStatusAnnoValue ¶ added in v0.4.0
ResolveStatusAnnoValue resolves annotation of api eg:
@code api=StatusInternalServerError @code api=http=500
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func (*Error) CodeHintCases ¶ added in v0.4.1
CodeHintCases generates code snippet for hint cases of constants annotated by `@code hint`
func (*Error) CodeIdentifierCases ¶ added in v0.4.1
CodeIdentifierCases generates code snippet for identifier cases: ${DOMAIN}__${SUFFIX}
func (*Error) CodeMessageCases ¶
CodeMessageCases generates code snippet for error message cases
func (*Error) CodeValues ¶ added in v0.4.1
CodeValues generates code snippet of code value list, excluding undefined
type HTTPStatusRegistry ¶ added in v0.4.0
type HTTPStatusRegistry struct {
// contains filtered or unexported fields
}
HTTPStatusRegistry maps net/http status constant names (StatusOK) to numeric codes. Populated once from go/types so we stay aligned with the toolchain's net/http.
func (HTTPStatusRegistry) StatusCodeOf ¶ added in v0.4.0
func (r HTTPStatusRegistry) StatusCodeOf(name string) (int, bool)