results

package
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

ajan/results

Overview

The results package provides a structured way to handle and represent operational results, including errors, within the application.

The documentation below provides an overview of the package, its types, functions, and usage examples. For more detailed information, refer to the source code and tests.

API

Result interface

Defines the contract for result types.

type Result interface {
	error
	Unwrap() error

	IsError() bool
	String() string
	Attributes() []slog.Attr
}

Methods:

  • Error() string: Returns the error message.
  • Unwrap() error: Returns the underlying error.
  • IsError() bool: Indicates if the result is an error.
  • String() string: Returns the string representation of the result.
  • Attributes() []slog.Attr: Returns the attributes associated with the result.
Define function

Creates a new Definition object.

// func Define(code string, message string, attributes ...slog.Attr) *Definition

var (
  resOk       = results.Define("OP001", "OK")
  resNotFound = results.Define("OP002", "Not Found")
  resFailure  = results.Define("OP003", "Fail")
)
Definition.New and Definitions.Wrap methods

Creates a new Result object from a definition.

Example 1:

var (
  resOk       = results.Define("OP001", "OK")
  resNotFound = results.Define("OP002", "Not Found")
  resFailure  = results.Define("OP003", "Fail")
)

// func (r *Definition) New(payload ...any) ResultImpl
// func (r *Definition) Wrap(err error, payload ...any) ResultImpl

func FileOp(filename string) results.Result {
  file, err := os.Open(filepath.Clean(filename))
	if err != nil {
		if os.IsNotExist(err) {
			return resNotFound.New()
		}

    return resFailure.Wrap(err)
  }

  ...

  return resOk.New()
}

Example 2:

var (
  resOk                = results.Define("PARSE001", "OK")
  resSyntaxError       = results.Define("PARSE002", "Syntax Error")
  resInvalidOperation  = results.Define("PARSE003", "Invalid Operation")
)

// func (r *Definition) New(payload ...any) ResultImpl
// func (r *Definition) Wrap(err error, payload ...any) ResultImpl

func Parse(...) results.Result {
  if ... {
    // Output: [PARSE002] Syntax Error: host/path missing / (pattern=..., method=...)
    return resSyntaxError.New("host/path missing /").
      WithAttribute(
        slog.String("pattern", str),
        slog.String("method", method),
      )
  }

  if ... {
    // Output: [PARSE003] Invalid Operation: method defined twice (pattern=..., method=...)
    return resSyntaxError.New("method defined twice").
      WithAttribute(
        slog.String("pattern", str),
        slog.String("method", method),
      )
  }

  ...

  // Output: [PARSE001] OK
  return resOk.New()
}

fmt.Println(Parse(...).String())

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Definition

type Definition struct {
	Code    string
	Message string

	Attributes []slog.Attr
}

func Define

func Define(code string, message string, attributes ...slog.Attr) *Definition

func (*Definition) New

func (r *Definition) New(payload ...any) Result

func (*Definition) Wrap

func (r *Definition) Wrap(err error, payload ...any) Result

type Result

type Result struct {
	Definition *Definition

	InnerError      error
	InnerPayload    any
	InnerAttributes []slog.Attr
}

func (Result) Attributes

func (r Result) Attributes() []slog.Attr

func (Result) Error

func (r Result) Error() string

func (Result) IsError

func (r Result) IsError() bool

func (Result) String

func (r Result) String() string

func (Result) Unwrap

func (r Result) Unwrap() error

func (Result) WithAttribute

func (r Result) WithAttribute(attributes ...slog.Attr) Result

func (Result) WithError

func (r Result) WithError(err error) Result

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳