codex

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 18 Imported by: 0

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

  1. Add `// +genx:code` on the error code type.
  2. Define constants with `<PREFIX>_UNDEFINED` and `<PREFIX>__<NAME>` (see genx skill generator-codex.spec.md).
  3. 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().
  4. 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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveStatusAnnoValue added in v0.4.0

func ResolveStatusAnnoValue(anno string) (string, bool)

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

func (e *Error) CodeHintCases(ctx context.Context) s.Snippet

CodeHintCases generates code snippet for hint cases of constants annotated by `@code hint`

func (*Error) CodeIdentifierCases added in v0.4.1

func (e *Error) CodeIdentifierCases(ctx context.Context) s.Snippet

CodeIdentifierCases generates code snippet for identifier cases: ${DOMAIN}__${SUFFIX}

func (*Error) CodeMessageCases

func (e *Error) CodeMessageCases(ctx context.Context) s.Snippet

CodeMessageCases generates code snippet for error message cases

func (*Error) CodeValues added in v0.4.1

func (e *Error) CodeValues(ctx context.Context) s.Snippet

CodeValues generates code snippet of code value list, excluding undefined

func (*Error) IsValid

func (e *Error) IsValid() bool

func (*Error) StatusDescriberCases added in v0.4.0

func (e *Error) StatusDescriberCases(ctx context.Context) s.Snippet

type Errors

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

func NewErrors

func NewErrors(g genx.Context) *Errors

func (*Errors) Resolve

func (es *Errors) Resolve(t types.Type) (*Error, bool)

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)

Jump to

Keyboard shortcuts

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