Documentation ¶
Index ¶
- Variables
- func IsErr(err error) bool
- func IsErrorUndefinedEnum(err error) bool
- func IsUndefined(e Enum) bool
- func VersionString() string
- type Caser
- type Coder
- type Descriptioner
- type EmbeddedError
- type Enum
- type EnumTypeInfo
- type Error
- type ErrorTypeInfo
- type FlagUnmarshaler
- type HTTPError
- type HTTPResponder
- type Marshaler
- type Namespacer
- type PointerUnmarshaler
- type ProcessError
- type ProcessResponder
- type Sourcer
- type TypeDetails
- type Typer
- type Unmarshaler
- type Wrapped
- type YARPCError
- type YARPCResponder
Constants ¶
This section is empty.
Variables ¶
var ( // Version describes the version of the library. Version = `1.0.1` // Build describes the git revision for this build. // Getting read of this. Build = `` )
Functions ¶
func IsErrorUndefinedEnum ¶
IsErrorUndefinedEnum is used to check if an error is because an enum value was undefined.
func IsUndefined ¶
IsUndefined is used to check if an enum value is undefined.
func VersionString ¶
func VersionString() string
VersionString returns the renum library version, using semantic versioning (https://semver.org) decorating the Version with the Build if a build is provided.
Types ¶
type Caser ¶
type Caser interface { // SnakeCase should return enum names formatted as "snake_case" representations SnakeCase() string // PascalCase should return enum names formatted as "PascalCase" representations PascalCase() string // CamelCase should return enum names formatted as "camelCase" representations CamelCase() string // ScreamingCase should return enum names formatted as "SCREAMING_CASE" representations ScreamingCase() string // CommandCase should return enum names formatted as "command-case" representations CommandCase() string // TrainCase should return enum names formatted as "TRAIN-CASE" representations TrainCase() string // DottedCase should renum enum names formatted as "dotted.case" representations DottedCase() string }
Caser defines an interface for types (specifically enums) to be able to describe their name typically returned by the fmt.Stringer interface as various text casings. This is helpful in situations where there are character restrictions that are enforced.
type Coder ¶
type Coder interface {
Code() int
}
Coder allows an enum to retrieve it's builtin underlying numeric value.
type Descriptioner ¶
type Descriptioner interface {
Description() string
}
Descriptioner allows types to describe themselves in more detail when asked, without having to embed this information in otherwise unstructured ways like fmt.Errorf("foo info: %v", err).
type EmbeddedError ¶
type EmbeddedError Error
EmbeddedError is a type alias for renum.Error that allows the renum.Wrapped interface to directly embed the EmbeddedError into Wrapped without conflicting with the Go built-in error interface's Error() string method.
type Enum ¶
type Enum interface { // Coder requires enums to be able to describe their underlying integer representation. Coder // Namespacer requires enums to be uniquely identifiable with namespace and path values. Namespacer // Sourcer requires enums to be able to self describe aspects of the Go source and package // which they're located. This makes Enum's great for tracing and error handling. Sourcer // Typer requires enums to describe their type. Typer // Descriptioner requires enums to describe themselves in detail, upon request. Descriptioner // Caser requires enums to describe their names in multiple // string case semantics. Caser // Stringer implements fmt.Print handling fmt.Stringer // Marshaler requires that enums be able to support encoding/decoding for // a variety of common formats. The expectation is that if your Enum implements // Marshaler, that it will also implement the pointer recievers for Unmarshaler. // If you're generating your Enum with the Renum CLI, this will happen // automatically for you. Marshaler }
Enum forms the basis for a strongly typed enum class that allows for good cross-package interoperability. This creates enums that play nice with things like loggers and metrics emitters.
type EnumTypeInfo ¶
type EnumTypeInfo struct { Name string `json:"name,omitempty" mapstructure:"name,omitempty" yaml:"name,omitempty" toml:"name,omitempty"` Code int `json:"code,omitempty" mapstructure:"code,omitempty" yaml:"code,omitempty" toml:"code,omitempty"` Details TypeDetails `json:"details,omitempty" mapstructure:"details,omitempty" yaml:"details,omitempty" toml:"details,omitempty"` }
EnumTypeInfo is a type used to hold all the metadata associated with a given renum.Enum where the fields of this structure are associated directly with the return values from the renum.Enum interface. This acts as a convenience to help things like structured loggers or HTTP JSON responses to be have information extracted into a self contained object.
func ExtractEnumTypeInfo ¶
func ExtractEnumTypeInfo(e Enum) EnumTypeInfo
ExtractEnumTypeInfo is used to take a renum.Enum type and expand it's details into a more annotated structure. The primary purpose of this is to act as a helper to loggers who wish to expand interface methods of the renum.Enum type into a nested, flat structure.
type Error ¶
Error allows types to conform to a strongly defined interface, as well as act as enriched error builtins. The point of this is that as types that satisfy Error pass across package boundry, context and metadata is not lost.
type ErrorTypeInfo ¶
type ErrorTypeInfo struct { Name string `json:"name,omitempty" mapstructure:"name,omitempty" yaml:"name,omitempty" toml:"name,omitempty"` Code int `json:"code,omitempty" mapstructure:"code,omitempty" yaml:"code,omitempty" toml:"code,omitempty"` Details TypeDetails `json:"details,omitempty" mapstructure:"details,omitempty" yaml:"details,omitempty" toml:"details,omitempty"` Message string `json:"message,omitempty" mapstructure:"message,omitempty" yaml:"message,omitempty" toml:"message,omitempty"` }
ErrorTypeInfo is a type used to hold all the metadata associated with a given renum.Error where the fields of this structure are associated directly with the return values from the renum.Error interface. This acts as a convenience to help things like structured loggers or HTTP JSON responses to be have information extracted into a self contained object.
func ExtractErrorTypeInfo ¶
func ExtractErrorTypeInfo(e error) []ErrorTypeInfo
ExtractErrorTypeInfo is used to take a renum.Error type and expand it's details into a more annotated structure. The primary purpose of this is to act as a helper to loggers who wish to expand interface methods of the renum.Error type into a nested, flat structure.
func ExtractErrorsFromYARPCStatus ¶
func ExtractErrorsFromYARPCStatus(status *yarpcerrors.Status) ([]ErrorTypeInfo, bool)
ExtractErrorsFromYARPCStatus is a helper method to read in a YARPC Status has been transmitted across application boundries and attempts to unpack an error stack of the foreign services wrapped errors. Note that this should *not* be used to programmatically type check errors, but rather in presenting the remote error's contexts in ways that are easily formatted for a user.
type FlagUnmarshaler ¶
type FlagUnmarshaler interface { String() string Set(string) error Get() interface{} Type() string }
FlagUnmarshaler is used to enforce that enum types can be properly encoded and decoded into command line flags without custom implementations. This requires renum.Enums to conform to pflag.Value (github.com/spf13/pflag), as well as the standard library flag package.
type HTTPError ¶
type HTTPError interface { Error HTTPResponder }
HTTPError extends the Error interface to allow a type to additionally self-report an HTTP Response code in order to enrich a net/http handler's ability to respond with the proper status code when an error of this type is encountered.
type HTTPResponder ¶
type HTTPResponder interface {
ToHTTP() int
}
HTTPResponder allows a type to define a specified HTTP status code so that a handler can automatically act on it's behalf without having to maintain a separate mapping.
type Marshaler ¶
Marshaler defines the required methods for a renum.Enum type implementation to properly support serialization to various encoding formats (text, json, yaml, sql, flags). This allows for easy integration with existing data structures that are commonly serialized into these formats.
type Namespacer ¶
type Namespacer interface { Name() string // val ID() string // type_val Path() string // github.com.gen0cide.foo.type_val Namespace() string // github.com.gen0cide.foo }
Namespacer requires a type be able to produce information relating to the package or component it's defined by. This allows tracing of errors to propogate across package boundries without loosing the ability to easily identify the owner of a type.
type PointerUnmarshaler ¶
type PointerUnmarshaler interface {
PointerUnmarshal() Unmarshaler
}
PointerUnmarshaler defines how a concrete non-pointer value can conform to the Unmarshaler contract by returning a pointer to it's value receiver. This interface has no practical use and should not need to be used. It simply exists to allow the Go compiler to ensure compliance with the Unmarshaler interface by renum.Enum types.
type ProcessError ¶
type ProcessError interface { Error ProcessResponder }
ProcessError extends the Error interface to allow a type to additionally self-report a specific exit code it wishes the handler to exit the process with.
type ProcessResponder ¶
type ProcessResponder interface {
ToOSExit() int
}
ProcessResponder allows a type to define the exit code that a process should exit with upon encountering said type. This primarily targets error handling.
type Sourcer ¶
type Sourcer interface { PackageName() string // foo PackagePath() string // github.com/gen0cide/foo ExportType() string // foo.TypeVal ExportRef() string // github.com/gen0cide/foo.TypeVal }
Sourcer requires enums to be able to self describe aspects of the Go source and package which they're located. This makes Enum's great for tracing and error handling. This interface allows callers to retrieve additional context of the Enum value without having to take up excess memory space, since the enum is just a type alias to a builtin numeric.
type TypeDetails ¶
type TypeDetails struct { Namespace string `json:"namespace,omitempty" mapstructure:"namespace,omitempty" yaml:"namespace,omitempty" toml:"namespace,omitempty"` Path string `json:"path,omitempty" mapstructure:"path,omitempty" yaml:"path,omitempty" toml:"path,omitempty"` Kind string `json:"kind,omitempty" mapstructure:"kind,omitempty" yaml:"kind,omitempty" toml:"kind,omitempty"` Source string `json:"source,omitempty" mapstructure:"source,omitempty" yaml:"source,omitempty" toml:"source,omitempty"` ImportPath string `json:"import_path,omitempty" mapstructure:"import_path,omitempty" yaml:"import_path,omitempty" toml:"import_path,omitempty"` Description string `json:"description,omitempty" mapstructure:"description,omitempty" yaml:"description,omitempty" toml:"description,omitempty"` }
TypeDetails allows for detailed renum.Enum/renum.Error type information to be embedded in a nested structure for TypeInfo structs.
type Typer ¶
type Typer interface {
Type() string // Type
}
Typer can be implemented by types to allow their callers to get a string reference to the type that they are. Here's an example of what that means:
type Foo int // enum type alias const FooValA Foo = 1 // enum value assignment Foo(1).Type() = "Foo" // Type() returns the name of the type.
type Unmarshaler ¶
type Unmarshaler interface { encoding.TextUnmarshaler json.Unmarshaler yaml.Unmarshaler sql.Scanner FlagUnmarshaler }
Unmarshaler defines the required methods for a renum.Enum type implementation to properly support de-serialization out of various encoding foramts (text, json, yaml, sql, flags). This allows for easy integration with existing data structures that are commonly unmarshaled from these formats.
type Wrapped ¶
type Wrapped interface { EmbeddedError // Typed returns the renum.Err typed error for this wrapped error. Typed() Error // Cause implements github.com/pkg/errors.Causer interface Cause() error // Unwrap implements the golang.org/x/xerrors.Wrapper interface. Unwrap() error // Is implements the golang.org/x/xerrors.Is interface. Is(e error) bool // Format implements fmt.Formatter interface (old error handling) Format(f fmt.State, c rune) // FormatError implements golang.org/x/xerrors.Formatter interface (new error handling) FormatError(p xerrors.Printer) error // Errors implements the github.com/uber-go/multierr.errorGroup interface. Errors() []error // YARPCError implements the go.uber.org/yarpc/yarpcerrors interface for creating // custom YARPC errors. YARPCError() *yarpcerrors.Status }
Wrapped defines a type implementation that allows for wrapped Errors to be wrapped and unwrapped following Go convention (both old and new).
type YARPCError ¶
type YARPCError interface { Error YARPCResponder }
YARPCError extends the Error interface to allow a type to additionally self-report a YARPC error code in order to enrich the handler's ability to respond with the proper code when an error of this type is encountered.
type YARPCResponder ¶
type YARPCResponder interface { ToYARPC() yarpcerrors.Code YARPCError() *yarpcerrors.Status }
YARPCResponder allows a type to define a specified YARPC error code so that a handler can automatically act on it's behalf without having to maintain a separate mapping.