arg

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2022 License: MIT Imports: 10 Imported by: 3

README

arg

Package implements functions to validate arguments.

Synopsis

func main() {
	var (
		host     string = "127.0.0.1"
		status   string = "connecting"
		requests int64  = 99
	)

	err := arg.Assert(
		arg.Strings.Assert(host, "host",
			arg.Strings.NonEmpty,
		),
		arg.Strings.Assert(status, "status",
			arg.Strings.NonEmpty,
			arg.Strings.In("connecting", "closed", "aborted"),
		),
		arg.Ints.Assert(requests, "requrests",
			arg.Ints.NonNegativeInteger,
		),
	)
	fmt.Println(err)
	// Output: <nil>
}

Documentation

Overview

Package arg implements functions to validate arguments.

Synopsis:

var (
	host     string = "127.0.0.1"
	status   string = "connecting"
	requests int64  = 99
)

err := arg.Assert(
	arg.Strings.Assert(host, "host",
		arg.Strings.NonEmpty,
	),
	arg.Strings.Assert(status, "status",
		arg.Strings.NonEmpty,
		arg.Strings.In("connecting", "closed", "aborted"),
	),
	arg.Ints.Assert(requests, "requrests",
		arg.Ints.NonNegativeInteger,
	),
)
fmt.Println(err)  // Output: <nil>

The Strings, Floats, Ints, Numbers, Values also offer `Assertor()`:

err := arg.Strings.Assertor(host, "host").
	Assert(
		arg.Strings.NonEmpty,
	)

is preferable to

err := arg.Strings.Assert(host, "host",
	arg.Strings.NonEmpty,
)
Example
package main

import (
	"fmt"

	"github.com/Bofry/arg"
)

func main() {
	var (
		host     string = "127.0.0.1"
		status   string = "connecting"
		requests int64  = 99
	)

	err := arg.Assert(
		arg.Strings.Assert(host, "host",
			arg.Strings.NonEmpty,
		),
		arg.Strings.Assert(status, "status",
			arg.Strings.NonEmpty,
			arg.Strings.In("connecting", "closed", "aborted"),
		),
		arg.Ints.Assert(requests, "requrests",
			arg.Ints.NonNegativeInteger,
		),
	)
	fmt.Println(err)
}
Output:
<nil>

Index

Examples

Constants

View Source
const (
	Strings = StringAssertion("")
	Floats  = FloatAssertion("")
	Ints    = IntAssertion("")
	UInts   = UIntAssertion("")
	Numbers = NumberAssertion("")
	Slices  = SliceAssertion("")
	Values  = ValueAssertion("")
	IPs     = IPAssertion("")
)
View Source
const (
	EmailPattern = `^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$`
)

Variables

This section is empty.

Functions

func Assert

func Assert(errs ...error) error

func ThrowError

func ThrowError(name, reason string) error

Types

type FloatAssertion

type FloatAssertion string

func (FloatAssertion) Assert

func (FloatAssertion) Assert(v float64, name string, validators ...FloatValidator) error

func (FloatAssertion) Assertor

func (FloatAssertion) Assertor(v float64, name string) *FloatAssertor

func (FloatAssertion) BetweenRange

func (FloatAssertion) BetweenRange(min, max float64) FloatValidator

BetweenRange checks if given number is between the specified minimum and maximum values (both inclusive).

func (FloatAssertion) Greater

func (FloatAssertion) Greater(comparand float64) FloatValidator

func (FloatAssertion) GreaterOrEqual

func (FloatAssertion) GreaterOrEqual(comparand float64) FloatValidator

func (FloatAssertion) Less

func (FloatAssertion) Less(comparand float64) FloatValidator

func (FloatAssertion) LessOrEqual

func (FloatAssertion) LessOrEqual(comparand float64) FloatValidator

func (FloatAssertion) Must

Example
package main

import (
	"fmt"
	"math"

	"github.com/Bofry/arg"
)

func main() {
	var v float64 = 4.9001
	err := arg.Floats.Assert(v, "v",
		arg.Floats.Must(
			func(v float64) bool {
				return math.Round(v) == 5
			}),
	)

	fmt.Println(err)
}
Output:
<nil>

func (FloatAssertion) NonNanNorInf

func (FloatAssertion) NonNanNorInf(v float64, name string) error

func (FloatAssertion) NonNegativeNumber

func (FloatAssertion) NonNegativeNumber(v float64, name string) error

func (FloatAssertion) NonZero

func (FloatAssertion) NonZero(v float64, name string) error

type FloatAssertor

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

func (*FloatAssertor) Assert

func (asr *FloatAssertor) Assert(validators ...FloatValidator) error

type FloatPredicate

type FloatPredicate func(v float64) bool

type FloatValidator

type FloatValidator func(v float64, name string) error

func (FloatValidator) AssertNumber

func (fn FloatValidator) AssertNumber(v Number, name string) error

func (FloatValidator) AssertValue

func (fn FloatValidator) AssertValue(v interface{}, name string) error

type IP

type IP = net.IP

type IPAssertion

type IPAssertion string

func (IPAssertion) Assert

func (IPAssertion) Assert(v net.IP, name string, validators ...IPValidator) error

func (IPAssertion) Assertor

func (IPAssertion) Assertor(v net.IP, name string) *IPAssertor

func (IPAssertion) BelongToAny

func (IPAssertion) BelongToAny(cidrs ...string) IPValidator

BelongTo checks whether the ip is belong to the specified networks.

func (IPAssertion) GlobalUnicast

func (IPAssertion) GlobalUnicast(v net.IP, name string) error

GlobalUnicast checks whether ip is a global unicast address.

The identification of global unicast addresses uses address type identification as defined in RFC 1122, RFC 4632 and RFC 4291 with the exception of IPv4 directed broadcast addresses. It returns true even if ip is in IPv4 private address space or local IPv6 unicast address space.

func (IPAssertion) InterfaceLocalMulticast

func (IPAssertion) InterfaceLocalMulticast(v net.IP, name string) error

InterfaceLocalMulticast checks whether ip is an interface-local multicast address.

func (IPAssertion) IsValid

func (IPAssertion) IsValid(v net.IP, name string) error

func (IPAssertion) LinkLocalMulticast

func (IPAssertion) LinkLocalMulticast(v net.IP, name string) error

LinkLocalMulticast checks whether ip is a link-local multicast address.

func (IPAssertion) LinkLocalUnicast

func (IPAssertion) LinkLocalUnicast(v net.IP, name string) error

LinkLocalUnicast checks whether ip is a link-local unicast address.

func (IPAssertion) Loopback

func (IPAssertion) Loopback(v net.IP, name string) error

Loopback checks whether ip is a loopback address.

func (IPAssertion) Multicast

func (IPAssertion) Multicast(v net.IP, name string) error

Multicast checks whether ip is a multicast address.

func (IPAssertion) Must

func (IPAssertion) NotBelongToAny

func (IPAssertion) NotBelongToAny(cidrs ...string) IPValidator

func (IPAssertion) NotGlobalUnicast

func (IPAssertion) NotGlobalUnicast(v net.IP, name string) error

func (IPAssertion) NotInterfaceLocalMulticast

func (IPAssertion) NotInterfaceLocalMulticast(v net.IP, name string) error

func (IPAssertion) NotLinkLocalMulticast

func (IPAssertion) NotLinkLocalMulticast(v net.IP, name string) error

func (IPAssertion) NotLinkLocalUnicast

func (IPAssertion) NotLinkLocalUnicast(v net.IP, name string) error

func (IPAssertion) NotLoopback

func (IPAssertion) NotLoopback(v net.IP, name string) error

func (IPAssertion) NotMulticast

func (IPAssertion) NotMulticast(v net.IP, name string) error

func (IPAssertion) NotPrivate

func (IPAssertion) NotPrivate(v net.IP, name string) error

func (IPAssertion) NotUnspecified

func (IPAssertion) NotUnspecified(v net.IP, name string) error

func (IPAssertion) Private

func (IPAssertion) Private(v net.IP, name string) error

Private checks whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses).

func (IPAssertion) Unspecified

func (IPAssertion) Unspecified(v net.IP, name string) error

Unspecified checks whether ip is an unspecified address, either the IPv4 address "0.0.0.0" or the IPv6 address "::".

type IPAssertor

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

func (*IPAssertor) Assert

func (asr *IPAssertor) Assert(validators ...IPValidator) error

type IPPredicate

type IPPredicate func(v net.IP) bool

type IPValidator

type IPValidator func(v net.IP, name string) error

func (IPValidator) AssertString

func (fn IPValidator) AssertString(v string, name string) error

func (IPValidator) AssertValue

func (fn IPValidator) AssertValue(v interface{}, name string) error

type IntAssertion

type IntAssertion string

func (IntAssertion) Assert

func (IntAssertion) Assert(v int64, name string, validators ...IntValidator) error

func (IntAssertion) Assertor

func (IntAssertion) Assertor(v int64, name string) *IntAssertor

func (IntAssertion) BetweenRange

func (IntAssertion) BetweenRange(min, max int64) IntValidator

BetweenRange checks if given integer is between the specified minimum and maximum values (both inclusive).

func (IntAssertion) GreaterOrEqual

func (IntAssertion) GreaterOrEqual(boundary int64) IntValidator

func (IntAssertion) In

func (IntAssertion) In(values ...int64) IntValidator

func (IntAssertion) LessOrEqual

func (IntAssertion) LessOrEqual(boundary int64) IntValidator

func (IntAssertion) Must

Example
package main

import (
	"fmt"

	"github.com/Bofry/arg"
)

func main() {
	var v int64 = 6
	err := arg.Ints.Assert(v, "v",
		arg.Ints.Must(
			func(v int64) bool {
				return (v & 0x01) == 0
			}),
	)

	fmt.Println(err)
}
Output:
<nil>

func (IntAssertion) NonNegativeInteger

func (IntAssertion) NonNegativeInteger(v int64, name string) error

func (IntAssertion) NonZero

func (IntAssertion) NonZero(v int64, name string) error

func (IntAssertion) NotIn

func (IntAssertion) NotIn(values ...int64) IntValidator

type IntAssertor

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

func (*IntAssertor) Assert

func (asr *IntAssertor) Assert(validators ...IntValidator) error

type IntPredicate

type IntPredicate func(v int64) bool

type IntValidator

type IntValidator func(v int64, name string) error

func (IntValidator) AssertNumber

func (fn IntValidator) AssertNumber(v Number, name string) error

func (IntValidator) AssertValue

func (fn IntValidator) AssertValue(v interface{}, name string) error

type InvalidArgumentError

type InvalidArgumentError struct {
	Name   string
	Reason string
	Err    error
}

func (*InvalidArgumentError) Error

func (e *InvalidArgumentError) Error() string

func (*InvalidArgumentError) Unwrap

func (e *InvalidArgumentError) Unwrap() error

Unwrap returns the underlying error.

type Number

type Number = json.Number

type NumberAssertion

type NumberAssertion string

func (NumberAssertion) Assert

func (NumberAssertion) Assert(v Number, name string, validators ...NumberValidator) error

func (NumberAssertion) Assertor

func (NumberAssertion) Assertor(v Number, name string) *NumberAssertor

func (NumberAssertion) BetweenRange

func (NumberAssertion) BetweenRange(min, max float64) NumberValidator

BetweenRange checks if given number is between the specified minimum and maximum values (both inclusive).

func (NumberAssertion) Greater

func (NumberAssertion) Greater(comparand float64) NumberValidator

func (NumberAssertion) GreaterOrEqual

func (NumberAssertion) GreaterOrEqual(comparand float64) NumberValidator

func (NumberAssertion) IsNumber

func (NumberAssertion) IsNumber(v Number, name string) error

func (NumberAssertion) Less

func (NumberAssertion) Less(comparand float64) NumberValidator

func (NumberAssertion) LessOrEqual

func (NumberAssertion) LessOrEqual(comparand float64) NumberValidator

func (NumberAssertion) Must

Example
package main

import (
	"fmt"

	"github.com/Bofry/arg"
)

func main() {
	var v arg.Number = "6"
	err := arg.Numbers.Assert(v, "v",
		arg.Numbers.Must(
			func(v arg.Number) bool {
				argv, err := v.Int64()
				if err != nil {
					return false
				}
				return (argv & 0x01) == 0
			}),
	)

	fmt.Println(err)
}
Output:
<nil>

func (NumberAssertion) MustFloat

func (NumberAssertion) MustInt

func (NumberAssertion) NonNanNorInf

func (NumberAssertion) NonNanNorInf(v Number, name string) error

func (NumberAssertion) NonNegativeNumber

func (NumberAssertion) NonNegativeNumber(v Number, name string) error

func (NumberAssertion) NonZero

func (NumberAssertion) NonZero(v Number, name string) error

func (NumberAssertion) NotIn

func (NumberAssertion) NotIn(values ...int64) NumberValidator

type NumberAssertor

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

func (*NumberAssertor) Assert

func (asr *NumberAssertor) Assert(validators ...NumberValidator) error

type NumberPredicate

type NumberPredicate func(v Number) bool

type NumberValidator

type NumberValidator func(v Number, name string) error

type SliceAssertion

type SliceAssertion string

func (SliceAssertion) NonEmpty

func (SliceAssertion) NonEmpty(v interface{}, name string) error

type StringAssertion

type StringAssertion string

func (StringAssertion) Assert

func (StringAssertion) Assert(v string, name string, validators ...StringValidator) error

func (StringAssertion) Assertor

func (StringAssertion) Assertor(v string, name string) *StringAssertor

func (StringAssertion) In

func (StringAssertion) In(values ...string) StringValidator

func (StringAssertion) MatchAny

func (StringAssertion) MatchAny(patterns ...string) StringValidator

MatchAny checks if given string match any one from specified patterns.

Example
package main

import (
	"fmt"

	"github.com/Bofry/arg"
)

func main() {
	var v string = "demo@mail.com"
	err := arg.Strings.Assert(v, "v",
		arg.Strings.MatchAny(
			arg.EmailPattern,
		),
	)

	fmt.Println(err)
}
Output:
<nil>

func (StringAssertion) MaxLength

func (StringAssertion) MaxLength(size int) StringValidator

func (StringAssertion) MinLength

func (StringAssertion) MinLength(size int) StringValidator

func (StringAssertion) Must

Must checks if the given string is evaluated to true by specified predicate.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/Bofry/arg"
)

func main() {
	var v string = "ENV_FOO"
	err := arg.Strings.Assert(v, "v",
		arg.Strings.Must(
			func(v string) bool {
				return strings.HasPrefix(v, "ENV_")
			}),
	)

	fmt.Println(err)
}
Output:
<nil>

func (StringAssertion) NonEmpty

func (StringAssertion) NonEmpty(v string, name string) error

type StringAssertor

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

func (*StringAssertor) Assert

func (asr *StringAssertor) Assert(validators ...StringValidator) error

type StringPredicate

type StringPredicate func(v string) bool

type StringValidator

type StringValidator func(v string, name string) error

func (StringValidator) AssertNumber

func (fn StringValidator) AssertNumber(v Number, name string) error

func (StringValidator) AssertValue

func (fn StringValidator) AssertValue(v interface{}, name string) error

type UIntAssertion

type UIntAssertion string

func (UIntAssertion) Assert

func (UIntAssertion) Assert(v uint64, name string, validators ...UIntValidator) error

func (UIntAssertion) Assertor

func (UIntAssertion) Assertor(v uint64, name string) *UIntAssertor

func (UIntAssertion) BetweenRange

func (UIntAssertion) BetweenRange(min, max uint64) UIntValidator

BetweenRange checks if given integer is between the specified minimum and maximum values (both inclusive).

func (UIntAssertion) GreaterOrEqual

func (UIntAssertion) GreaterOrEqual(boundary uint64) UIntValidator

func (UIntAssertion) In

func (UIntAssertion) In(values ...uint64) UIntValidator

func (UIntAssertion) LessOrEqual

func (UIntAssertion) LessOrEqual(boundary uint64) UIntValidator

func (UIntAssertion) Must

func (UIntAssertion) NonZero

func (UIntAssertion) NonZero(v uint64, name string) error

func (UIntAssertion) NotIn

func (UIntAssertion) NotIn(values ...uint64) UIntValidator

type UIntAssertor

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

func (*UIntAssertor) Assert

func (asr *UIntAssertor) Assert(validators ...UIntValidator) error

type UIntPredicate

type UIntPredicate func(v uint64) bool

type UIntValidator

type UIntValidator func(v uint64, name string) error

func (UIntValidator) AssertNumber

func (fn UIntValidator) AssertNumber(v Number, name string) error

func (UIntValidator) AssertValue

func (fn UIntValidator) AssertValue(v interface{}, name string) error

type ValueAssertion

type ValueAssertion string

func (ValueAssertion) Assert

func (ValueAssertion) Assert(v interface{}, name string, validators ...ValueValidator) error

func (ValueAssertion) Assertor

func (ValueAssertion) Assertor(v interface{}, name string) *ValueAssertor

func (ValueAssertion) Must

func (ValueAssertion) NotNil

func (ValueAssertion) NotNil(v interface{}, name string) error

type ValueAssertor

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

func (*ValueAssertor) Assert

func (asr *ValueAssertor) Assert(validators ...ValueValidator) error

type ValuePredicate

type ValuePredicate func(v interface{}) bool

type ValueValidator

type ValueValidator func(v interface{}, name string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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