done

package module
v1.0.18 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 4 Imported by: 21

README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

Done - Simple Error Handling in Go

Done allows you to focus on your business logic without repetitive if err != nil patterns.

when you write logic:

if err := run(); err != nil{
    panic(err)
}

then you can use done:

done.Done(run())

CHINESE README

中文说明


Features

  • Simple error handling: Handle errors quickly with simple functions.
  • Supports multiple results: Works well with functions that return a value and an error type.
  • Less repeated code: Replace repeated error checks with short and clear functions.

Installation

go get github.com/yyle88/done

How It Works

The Done package wraps error checking into easy-to-use assertions, enabling you to handle errors while focusing on the success path of code.


Core Types

Type Description
Ve[V any] Holds a value and an error. Provides methods like Done, Must, and Soft.
Vpe[V any] For pointer values, includes methods such as Sure, Nice, and Good.
Vce[V comparable] For comparable values, provides methods like Same, Diff, and Equals.

Key Functions

Function Description
Done Panics if an error exists.
Must Ensures the error is nil and returns the value.
Soft Logs a warning for errors but does not panic.
Fata Logs the error at a "fatal" level and panics.

Functionality Summary

Category Functions Description
Error Handling Done, Must, Soft Panics on error or handles it in a specific way.
Result Validation Sure, Nice, Some Ensures the result is not zero and returns it.
Result Validation Good, Fine, Safe Ensures the result is not zero without returning it.
Zero Value Checking Zero, None, Void Ensures the result is the zero value of its type.
Value Comparisons Same, Diff, Is, Equals Checks if values are the same, different, or match specific conditions.

Advanced Error Handling

Utility Description
Vce For comparable values, includes methods like Same, Diff, Is, and Equals.
Vse For string operations, includes methods like HasPrefix, HasSuffix, and Contains.
Vne For numeric operations, includes methods like Gt (greater than), Lt (less than), etc.

Example Usage

Standard Error Handling
package main

import (
	"fmt"
)

func main() {
	xyz, err := NewXyz()
	if err != nil {
		panic(err) // Handle errors manually
	}
	abc, err := xyz.Abc()
	if err != nil {
		panic(err)
	}
	uvw, err := abc.Uvw()
	if err != nil {
		panic(err)
	}
	fmt.Println(uvw.Message)
}
Using Done
package main

import (
	"fmt"
	"github.com/yyle88/done"
)

func main() {
	xyz := done.VCE(NewXyz()).Nice()
	abc := done.VCE(xyz.Abc()).Nice()
	uvw := done.VCE(abc.Uvw()).Nice()
	fmt.Println(uvw.Message)
}

This approach simplifies the code by chaining function calls with error handling assertions.


Chaining Operations

Without Done
package main

import (
	"fmt"
	"strconv"

	"github.com/pkg/errors"
)

func main() {
	stringNum, err := fetch()
	if err != nil {
		panic(err)
	}
	num, err := toInt(stringNum)
	if err != nil {
		panic(err)
	}
	if num <= 0 {
		panic(errors.New("num <= 0"))
	}
	fmt.Println(num)
}
With Done
package main

import (
	"fmt"
	"strconv"

	"github.com/yyle88/done"
)

func main() {
	num := done.VNE(toInt(done.VCE(fetch()).Nice())).Gt(0)
	fmt.Println(num)
}

By using Done, you eliminate repetitive error checks and enable inline assertions for conditions, resulting in cleaner and more maintainable code.


Conclusion

The Done package is a powerful tool for simplifying error handling, especially in informal or small-scale projects. It helps reduce boilerplate, makes error handling concise, and lets you focus on writing clean and efficient business logic.

Give it a try and share your feedback!


License

This project is licensed under the MIT License. See the LICENSE file for details.


Contribute and Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

Thank you for your support!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func C0 added in v1.0.16

func C0(err error)

func C1 added in v1.0.16

func C1[T1 comparable](v1 T1, err error) T1

func C2 added in v1.0.16

func C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)

func Done

func Done(err error)

Done 当err非空是就panic且打印调用栈信息

func Fata

func Fata(err error)

Fata 当出错时就是调用 log.Fatal 退出系统,当不出错时就不做任何事情 FaFaFaFaFa 哈哈哈哈哈

func Fine

func Fine[V comparable](v V)

Fine means the value is not zero value

func Full added in v1.0.16

func Full[V *any](v any)

func Good

func Good[V comparable](v V)

Good means the value is not zero value, then return value

func Must

func Must(err error)

Must 当err非空是就panic且打印调用栈信息

func Nice

func Nice[V comparable](v V) V

Nice means the value is not zero value, then return value

func None

func None[V comparable](v V)

None means the value is a zero value

func Null added in v1.0.16

func Null[V *any](v any)

func P0 added in v1.0.16

func P0(err error)

func P1 added in v1.0.16

func P1[T1 any](v1 *T1, err error) *T1

func P2 added in v1.0.16

func P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)

func Safe

func Safe[V comparable](v V)

Safe means the value is not zero value

func Soft

func Soft(err error)

Soft 当err非空是就打印warning日志且打印调用栈信息

func Sure

func Sure[V comparable](v V) V

Sure means the value is not zero value, then return value

func V0 added in v1.0.16

func V0(err error)

func V1 added in v1.0.16

func V1[T1 any](v1 T1, err error) T1

func V2 added in v1.0.16

func V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)

func Zero

func Zero[V comparable](v V)

Zero means the value is a zero value, num is 0, string is "", PTR is none.

Types

type Vae

type Vae[V any] struct {
	V []V
	E error
	*Ve[[]V]
}

func VAE

func VAE[V any](val []V, err error) *Vae[V]

VAE accept two params. one is slice and one is error interface So A means Array type.

func (*Vae[V]) Fine

func (a *Vae[V]) Fine()

func (*Vae[V]) Good

func (a *Vae[V]) Good()

func (*Vae[V]) Have

func (a *Vae[V]) Have()

func (*Vae[V]) Len

func (a *Vae[V]) Len(n int)

func (*Vae[V]) Length

func (a *Vae[V]) Length(n int)

func (*Vae[V]) Nice

func (a *Vae[V]) Nice() []V

func (*Vae[V]) None

func (a *Vae[V]) None()

func (*Vae[V]) Safe

func (a *Vae[V]) Safe()

func (*Vae[V]) Size

func (a *Vae[V]) Size(n int)

func (*Vae[V]) Some

func (a *Vae[V]) Some() []V

func (*Vae[V]) Sure

func (a *Vae[V]) Sure() []V

func (*Vae[V]) Void

func (a *Vae[V]) Void()

func (*Vae[V]) Zero

func (a *Vae[V]) Zero()

type Vbe

type Vbe struct {
	V bool
	E error
	*Vce[bool]
}

func VBE

func VBE(val bool, err error) *Vbe

func (*Vbe) FALSE

func (a *Vbe) FALSE()

func (*Vbe) NO

func (a *Vbe) NO()

func (*Vbe) No

func (a *Vbe) No()

func (*Vbe) Not

func (a *Vbe) Not()

func (*Vbe) OK

func (a *Vbe) OK()

func (*Vbe) TRUE

func (a *Vbe) TRUE()

func (*Vbe) YES

func (a *Vbe) YES()

func (*Vbe) Yes

func (a *Vbe) Yes()

type Vce

type Vce[V comparable] struct {
	V V
	E error
	*Ve[V]
}

func VCE

func VCE[V comparable](val V, err error) *Vce[V]

VCE accept two params. one is comparable and one is error interface

func (*Vce[V]) Diff

func (a *Vce[V]) Diff(v V)

func (*Vce[V]) Different

func (a *Vce[V]) Different(v V)

Different 这个单词这么长真是万恶啊

func (*Vce[V]) Equals

func (a *Vce[V]) Equals(v V)

func (*Vce[V]) Fine

func (a *Vce[V]) Fine()

func (*Vce[V]) Good

func (a *Vce[V]) Good()

func (*Vce[V]) Is

func (a *Vce[V]) Is(v V)

func (*Vce[V]) Nice

func (a *Vce[V]) Nice() V

func (*Vce[V]) None

func (a *Vce[V]) None()

func (*Vce[V]) Safe

func (a *Vce[V]) Safe()

func (*Vce[V]) Same

func (a *Vce[V]) Same(v V)

func (*Vce[V]) Sure

func (a *Vce[V]) Sure() V

func (*Vce[V]) Zero

func (a *Vce[V]) Zero()

type Ve

type Ve[V any] struct {
	V V
	E error
}

func VE

func VE[V any](val V, err error) *Ve[V]

VE accept two params. one is any and one is error interface

func (*Ve[V]) Done

func (a *Ve[V]) Done() V

func (*Ve[V]) Must

func (a *Ve[V]) Must() V

func (*Ve[V]) Omit

func (a *Ve[V]) Omit() V

func (*Ve[V]) Skip

func (a *Ve[V]) Skip() V

func (*Ve[V]) Soft

func (a *Ve[V]) Soft() V

type Vme

type Vme[K comparable, V any] struct {
	V map[K]V
	E error
	*Ve[map[K]V]
}

func VME

func VME[K comparable, V any](val map[K]V, err error) *Vme[K, V]

VME accept two params. one is map and one is error interface So M means Map type.

func (*Vme[K, V]) Fine

func (a *Vme[K, V]) Fine()

func (*Vme[K, V]) Good

func (a *Vme[K, V]) Good()

func (*Vme[K, V]) Have

func (a *Vme[K, V]) Have()

func (*Vme[K, V]) Len

func (a *Vme[K, V]) Len(n int)

func (*Vme[K, V]) Length

func (a *Vme[K, V]) Length(n int)

func (*Vme[K, V]) Nice

func (a *Vme[K, V]) Nice() map[K]V

func (*Vme[K, V]) None

func (a *Vme[K, V]) None()

func (*Vme[K, V]) Safe

func (a *Vme[K, V]) Safe()

func (*Vme[K, V]) Size

func (a *Vme[K, V]) Size(n int)

func (*Vme[K, V]) Some

func (a *Vme[K, V]) Some() map[K]V

func (*Vme[K, V]) Sure

func (a *Vme[K, V]) Sure() map[K]V

func (*Vme[K, V]) Void

func (a *Vme[K, V]) Void()

func (*Vme[K, V]) Zero

func (a *Vme[K, V]) Zero()

type Vne

type Vne[V numType] struct {
	V V
	E error
	*Vce[V]
}

func VNE

func VNE[V numType](val V, err error) *Vne[V]

func (*Vne[V]) Gt

func (a *Vne[V]) Gt(base V) V

func (*Vne[V]) Gte

func (a *Vne[V]) Gte(base V) V

func (*Vne[V]) Lt

func (a *Vne[V]) Lt(base V) V

func (*Vne[V]) Lte

func (a *Vne[V]) Lte(base V) V

type Vpe

type Vpe[V any] struct {
	V       *V
	E       error
	*Ve[*V] //这里最好不要继承自 vce-comparable 这个类型,因为指针比较地址相等或者不想等的意义不太大,因此不要提供这些无意义的功能
}

func VPE

func VPE[V any](val *V, err error) *Vpe[V]

VPE accept two params. one is POINTER and one is ERROR interface

func (*Vpe[V]) Fine

func (a *Vpe[V]) Fine()

func (*Vpe[V]) Full added in v1.0.16

func (a *Vpe[V]) Full()

func (*Vpe[V]) Good

func (a *Vpe[V]) Good()

func (*Vpe[V]) Nice

func (a *Vpe[V]) Nice() *V

func (*Vpe[V]) None

func (a *Vpe[V]) None()

func (*Vpe[V]) Null added in v1.0.16

func (a *Vpe[V]) Null()

func (*Vpe[V]) Safe

func (a *Vpe[V]) Safe()

func (*Vpe[V]) Sure

func (a *Vpe[V]) Sure() *V

func (*Vpe[V]) Zero

func (a *Vpe[V]) Zero()

type Vse

type Vse struct {
	V string
	E error
	*Vce[string]
}

func VSE

func VSE(val string, err error) *Vse

func (*Vse) Contains

func (a *Vse) Contains(sub string)

func (*Vse) Equals

func (a *Vse) Equals(s string)

func (*Vse) HasPrefix

func (a *Vse) HasPrefix(prefix string)

func (*Vse) HasSuffix

func (a *Vse) HasSuffix(suffix string)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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