gassert

package module
v0.0.0-...-36530a1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 5 Imported by: 0

README

gassert

gassert is Go package that provides function like assert of Python or C++.

With gassert, you can check validation of parameters or values more easily and cleaner.

Usage

  • Basic

Since Golang don't provide assert function by default, if you want to check validation of params, you should write like below.

func MyFunc(n int, s string, arr []string, p *MyStruct) {
    if n == 0 || len(s) == 0 || arr == nil || p == nil {
        // error
    }
}

With gassert, you can write that:

func MyFunc(n int, s string, arr []string, p *MyStruct) {
  	// raise panic if any of them has zero-value
	gassert.Zeros(n, s, arr, p)
}

And you can check other conditions besides zero-value.

func MyFunc(n int, s string, arr []string, p *MyStruct) {
  	gassert.NumLess(n, 100)
  	gassert.StrLenGreater(s, 10)
  	gassert.SliceLenEquals(arr, 0)
  	gassert.Zeros(p)
}
  • Multiple

If you want to check multiple condition on one line, you can write below:

func MyFunc(n int, s string, arr []string, p *MyStruct) {
  	// raise panic if any of conditions is true
  	gassert.New().NumLess(n, 5).StrLenGreater(s, 10).SliceLenEquals(arr, 0).Zeros(0).Panic()
  
  	if err := gassert.New().NumLess(n, 5).StrLenGreater(s, 10).SliceLenEquals(arr, 0).Zeros(0).Err(); err != nil {
    	// handle error
  	}
}

When you write like this, SHOULD start with New() and end with Panic() , PanicDetails(), Err(), ErrDetails.

  • Panic() : raise panic if any of condition is true

  • PanicDetail(): raise panic if any of condition is true with information

  • Err(): return error if any of condition is true

  • ErrDetails(): return error if any of condition is true with information

  • Number

There is difference from general comparison statement in number. In Go, you can compare in only same types.

But gassert compares its values no matter what types. For example, you can write this:

func MyFunc(n int) {
  	gassert.NumLess(n, 12.34)
}

It may lose type-safety, but i thougth it is better idea for convenience.

  • Thread-Safety

gassert is thread-safety because it makes internal event object every calls. And the object is managed by sync.Pool, so it is economical in memory usage.

  • Complex condition

If you want to check complex condition which doesn't exists in gassert. You can just use Go().

func MyFunc(n int, s string) {
  	gassert.Go((n-5) % 10 == 10 || len(s)*2 > 10)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrLenEquals

func ArrLenEquals(s interface{}, n int)

func ArrLenGreater

func ArrLenGreater(s interface{}, n int)

func ArrLenGreaterOrEquals

func ArrLenGreaterOrEquals(s interface{}, n int)

func ArrLenLess

func ArrLenLess(s interface{}, n int)

func ArrLenLessOrEquals

func ArrLenLessOrEquals(s interface{}, n int)

func ArrLenNotEquals

func ArrLenNotEquals(s interface{}, n int)

func Equals

func Equals(x, y interface{})

func Go

func Go(condition bool)

func MapLenEquals

func MapLenEquals(s interface{}, n int)

func MapLenGreater

func MapLenGreater(s interface{}, n int)

func MapLenGreaterOrEquals

func MapLenGreaterOrEquals(s interface{}, n int)

func MapLenLess

func MapLenLess(s interface{}, n int)

func MapLenLessOrEquals

func MapLenLessOrEquals(s interface{}, n int)

func MapLenNotEquals

func MapLenNotEquals(s interface{}, n int)

func NotEquals

func NotEquals(x, y interface{})

func NumGreater

func NumGreater(x, y interface{})

func NumGreaterOrEquals

func NumGreaterOrEquals(x, y interface{})

func NumLess

func NumLess(x, y interface{})

func NumLessOrEquals

func NumLessOrEquals(x, y interface{})

func SliceCapEquals

func SliceCapEquals(s interface{}, n int)

func SliceCapGreater

func SliceCapGreater(s interface{}, n int)

func SliceCapGreaterOrEquals

func SliceCapGreaterOrEquals(s interface{}, n int)

func SliceCapLess

func SliceCapLess(s interface{}, n int)

func SliceCapLessOrEquals

func SliceCapLessOrEquals(s interface{}, n int)

func SliceCapNotEquals

func SliceCapNotEquals(s interface{}, n int)

func SliceLenEquals

func SliceLenEquals(s interface{}, n int)

func SliceLenGreater

func SliceLenGreater(s interface{}, n int)

func SliceLenGreaterOrEquals

func SliceLenGreaterOrEquals(s interface{}, n int)

func SliceLenLess

func SliceLenLess(s interface{}, n int)

func SliceLenLessOrEquals

func SliceLenLessOrEquals(s interface{}, n int)

func SliceLenNotEquals

func SliceLenNotEquals(s interface{}, n int)

func StrLenEquals

func StrLenEquals(s string, n int)

func StrLenGreater

func StrLenGreater(s string, n int)

func StrLenGreaterOrEquals

func StrLenGreaterOrEquals(s string, n int)

func StrLenLess

func StrLenLess(s string, n int)

func StrLenLessOrEquals

func StrLenLessOrEquals(s string, n int)

func StrLenNotEquals

func StrLenNotEquals(s string, n int)

func Zeros

func Zeros(xs ...interface{})

Types

type Errors

type Errors []error

func (Errors) Error

func (errs Errors) Error() string

type Event

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

func New

func New() *Event

func (*Event) ArrLenEquals

func (e *Event) ArrLenEquals(x interface{}, n int) *Event

func (*Event) ArrLenGreater

func (e *Event) ArrLenGreater(x interface{}, n int) *Event

func (*Event) ArrLenGreaterOrEquals

func (e *Event) ArrLenGreaterOrEquals(x interface{}, n int) *Event

func (*Event) ArrLenLess

func (e *Event) ArrLenLess(x interface{}, n int) *Event

func (*Event) ArrLenLessOrEquals

func (e *Event) ArrLenLessOrEquals(x interface{}, n int) *Event

func (*Event) ArrLenNotEquals

func (e *Event) ArrLenNotEquals(x interface{}, n int) *Event

func (*Event) DeepEqual

func (e *Event) DeepEqual(x, y interface{}) *Event

func (*Event) Equals

func (e *Event) Equals(x, y interface{}) *Event

func (*Event) Err

func (e *Event) Err() error

func (*Event) ErrDetails

func (e *Event) ErrDetails() error

func (*Event) MapLenEquals

func (e *Event) MapLenEquals(x interface{}, n int) *Event

func (*Event) MapLenGreater

func (e *Event) MapLenGreater(x interface{}, n int) *Event

func (*Event) MapLenGreaterOrEquals

func (e *Event) MapLenGreaterOrEquals(x interface{}, n int) *Event

func (*Event) MapLenLess

func (e *Event) MapLenLess(x interface{}, n int) *Event

func (*Event) MapLenLessOrEquals

func (e *Event) MapLenLessOrEquals(x interface{}, n int) *Event

func (*Event) MapLenNotEquals

func (e *Event) MapLenNotEquals(x interface{}, n int) *Event

func (*Event) NotEquals

func (e *Event) NotEquals(x, y interface{}) *Event

func (*Event) NumGreater

func (e *Event) NumGreater(x, y interface{}) *Event

func (*Event) NumGreaterOrEquals

func (e *Event) NumGreaterOrEquals(x, y interface{}) *Event

func (*Event) NumLess

func (e *Event) NumLess(x, y interface{}) *Event

func (*Event) NumLessOrEquals

func (e *Event) NumLessOrEquals(x, y interface{}) *Event

func (*Event) Panic

func (e *Event) Panic()

func (*Event) PanicDetails

func (e *Event) PanicDetails()

func (*Event) SliceCapEquals

func (e *Event) SliceCapEquals(x interface{}, n int) *Event

func (*Event) SliceCapGreater

func (e *Event) SliceCapGreater(x interface{}, n int) *Event

func (*Event) SliceCapGreaterOrEquals

func (e *Event) SliceCapGreaterOrEquals(x interface{}, n int) *Event

func (*Event) SliceCapLess

func (e *Event) SliceCapLess(x interface{}, n int) *Event

func (*Event) SliceCapLessOrEquals

func (e *Event) SliceCapLessOrEquals(x interface{}, n int) *Event

func (*Event) SliceCapNotEquals

func (e *Event) SliceCapNotEquals(x interface{}, n int) *Event

func (*Event) SliceLenEquals

func (e *Event) SliceLenEquals(x interface{}, n int) *Event

func (*Event) SliceLenGreater

func (e *Event) SliceLenGreater(x interface{}, n int) *Event

func (*Event) SliceLenGreaterOrEquals

func (e *Event) SliceLenGreaterOrEquals(x interface{}, n int) *Event

func (*Event) SliceLenLess

func (e *Event) SliceLenLess(x interface{}, n int) *Event

func (*Event) SliceLenLessOrEquals

func (e *Event) SliceLenLessOrEquals(x interface{}, n int) *Event

func (*Event) SliceLenNotEquals

func (e *Event) SliceLenNotEquals(x interface{}, n int) *Event

func (*Event) StrLenEquals

func (e *Event) StrLenEquals(s string, n int) *Event

func (*Event) StrLenGreater

func (e *Event) StrLenGreater(s string, n int) *Event

func (*Event) StrLenGreaterOrEquals

func (e *Event) StrLenGreaterOrEquals(s string, n int) *Event

func (*Event) StrLenLess

func (e *Event) StrLenLess(s string, n int) *Event

func (*Event) StrLenLessOrEquals

func (e *Event) StrLenLessOrEquals(s string, n int) *Event

func (*Event) StrLenNotEquals

func (e *Event) StrLenNotEquals(s string, n int) *Event

func (*Event) Zeros

func (e *Event) Zeros(xs ...interface{}) *Event

Directories

Path Synopsis
examples
basic command

Jump to

Keyboard shortcuts

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