gomu

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2017 License: MIT Imports: 12 Imported by: 1

README

gomu

GoDoc Build Status Coverage Status MIT License

gomu is a library for handling of nullable values and unassigned parameter.

Installation

Make sure that Go is installed on your computer. Type the following command in your terminal:

go get gopkg.in/hapoon/gomu.v1

After it the package is ready to use.

Add following line in your *.go file:

import "gopkg.in/hapoon/gomu.v1"

Usage

String

Nullable string.

If JSON value is null, String.Null is true. If JSON key is not assigned, String.Valid is false.

Int

Nullable int64.

If JSON value is null, Int.Null is true. If JSON key is not assigned, Int.Valid is false.

Bool

Nullable bool.

If JSON value is null, Bool.Null is true. If JSON key is not assigned, Bool.Valid is false.

Time

Nullable Time.

If JSON value is null, Time.Null is true. If JSON key is not assigned, Time.Valid is false.

Validate
type exampleStruct struct {
    Name String `valid:"stringlength(1|10)"`
    URL String `valid:"requrl"`
}
example := exampleStruct{
    Name: "sample name",
    URL:  "https://sample.com/top/",
}
result, err := gomu.Validate(example)
if !result {
    if err != nil {
        println("error: " + err.Error())
    }
}

License

MIT License

Documentation

Index

Constants

View Source
const (
	IP           string = `` /* 659-byte string literal not displayed */
	URLSchema    string = `((ftp|tcp|udp|wss?|https?):\/\/)`
	URLUsername  string = `(\S+(:\S*)?@)`
	URLIP        string = `([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))`
	URLSubdomain string = `((www\.)|([a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*))`
	URLPort      string = `(:(\d{1,5}))`
	URLPath      string = `((\/|\?|#)[^\s]*)`
	URL          string = `^` + URLSchema + `?` + URLUsername + `?` + `((` + URLIP + `|(\[` + IP + `\])|(([a-zA-Z0-9]([a-zA-Z0-9-]+)?[a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*)|(` + URLSubdomain + `?))?(([a-zA-Z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-zA-Z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-zA-Z\x{00a1}-\x{ffff}]{1,}))?))` + URLPort + `?` + URLPath + `?$`
)

Variables

View Source
var CustomTypeTagMap = &customTypeTagMap{validators: make(map[string]CustomTypeValidator)}

CustomTypeTagMap is a map of functions that can be used as tags for Validate function.

View Source
var ParamTagMap = map[string]ParamValidator{
	"length":       ByteLength,
	"stringlength": StringLength,
}

ParamTagMap is a map of functions accept variants parameters.

View Source
var ParamTagRegexMap = map[string]*regexp.Regexp{
	"length":       regexp.MustCompile("^length\\((\\d+)\\|(\\d+)\\)$"),
	"stringlength": regexp.MustCompile("^stringlength\\((\\d+)\\|(\\d+)\\)$"),
}

ParamTagRegexMap maps param tags to their respective regexes.

View Source
var TagMap = map[string]Validator{
	"url":    IsURL,
	"requrl": IsRequestURL,
	"requri": IsRequestURI,
}

TagMap is a map of functions, that can be used as tags for Validate function.

Functions

func ByteLength

func ByteLength(str string, params ...string) (result bool)

ByteLength check string's length.

func IsRequestURI added in v1.1.0

func IsRequestURI(rawurl string) bool

IsRequestURI check if the string rawurl, assuming it was recieved in an HTTP request, is an absolute URI or an absolute path.

func IsRequestURL added in v1.1.0

func IsRequestURL(rawurl string) bool

IsRequestURL check if the string rawurl, assuming it was recieved in an HTTP request, is a valid URL confirm to RFC 3986.

func IsURL added in v1.1.0

func IsURL(str string) bool

IsURL check if the string is an URL.

func StringLength

func StringLength(str string, params ...string) (result bool)

StringLength check string's length(including multi byte strings)

func ToInt

func ToInt(str string) (result int64, err error)

ToInt convert the input string to an integer, or 0 if the input is not an integer.

func Validate

func Validate(s interface{}) (result bool, err error)

Validate use tags for fields. result will be equal to `false` if there are any errors.

Types

type Bool

type Bool struct {
	Bool  bool
	Null  bool
	Valid bool
}

Bool is a nullable bool. If key is assigned and value is not null, Bool is specified value, Null is false, Valid is true. If value is null, Null is true. If key is not assigned, Valid is false.

func BoolFrom

func BoolFrom(b bool) Bool

BoolFrom creates a new Bool that will always be valid

func BoolFromPtr

func BoolFromPtr(b *bool) Bool

BoolFromPtr creates a new Bool that will be null if b is nil

func NewBool

func NewBool(b bool, n bool, valid bool) Bool

NewBool creates a new Bool.

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Bool) MarshalText

func (b Bool) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (Bool) Ptr

func (b Bool) Ptr() *bool

Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null or not valid.

func (*Bool) Scan

func (b *Bool) Scan(value interface{}) (err error)

Scan implements database/sql.Scanner.

func (*Bool) SetValid

func (b *Bool) SetValid(v bool)

SetValid changes Bool value and also sets Valid to be true

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements json.Unmarshaler.

func (*Bool) UnmarshalText

func (b *Bool) UnmarshalText(text []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler.

func (Bool) Value

func (b Bool) Value() (driver.Value, error)

Value implements database/sql.Valuer.

type CustomTypeValidator

type CustomTypeValidator func(i interface{}, o interface{}) bool

CustomTypeValidator is a wrapper for validator functions that returns bool and accept any type. The second parameter should be the context (in the case of validating a struct: the whole object being validated)

type Error

type Error struct {
	Name                     string
	Err                      error
	CustomErrorMessageExists bool
}

Error encapsulates a name, an error and whether there is a custom error message or not.

func (Error) Error

func (e Error) Error() string

type Errors

type Errors []error

Errors is an array of multiple errors and conforms to the error interface.

func (Errors) Error

func (e Errors) Error() (errStr string)

func (Errors) Errors

func (e Errors) Errors() []error

Errors returns itself.

type Int

type Int struct {
	Int64 int64
	Null  bool
	Valid bool
}

Int is a nullable int.

func IntFrom

func IntFrom(i int64) Int

IntFrom creates a new Int that will never be blank.

func IntFromPtr

func IntFromPtr(i *int64) Int

IntFromPtr creates a new Int that be null if i is nil.

func NewInt

func NewInt(i int64, n bool, valid bool) Int

NewInt creates a new Int.

func (Int) MarshalJSON

func (i Int) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Int) MarshalText

func (i Int) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Int) Ptr

func (i Int) Ptr() *int64

Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null or not valid.

func (*Int) Scan

func (i *Int) Scan(value interface{}) (err error)

Scan implements database/sql.Scanner.

func (*Int) SetValid

func (i *Int) SetValid(n int64)

SetValid changes this Int value and also sets Valid to be true.

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements json.Unmarshaler.

func (*Int) UnmarshalText

func (i *Int) UnmarshalText(text []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler.

type ParamValidator

type ParamValidator func(str string, params ...string) bool

ParamValidator is a wrapper for validator functions.

type String

type String struct {
	String string
	Null   bool
	Valid  bool
}

String is a nullable string.

func NewString

func NewString(s string, n bool, valid bool) String

NewString creates a new String.

func StringFrom

func StringFrom(s string) String

StringFrom creates a new String that will never be blank.

func StringFromPtr

func StringFromPtr(s *string) String

StringFromPtr creates a new String that be null if s is nil.

func (String) MarshalJSON

func (s String) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (String) MarshalText

func (s String) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (String) Ptr

func (s String) Ptr() *string

Ptr returns a pointer to this String's value, or a nil pointer if this String is null or not valid.

func (*String) Scan

func (s *String) Scan(value interface{}) (err error)

Scan implements database/sql.Scanner.

func (*String) SetValid

func (s *String) SetValid(v string)

SetValid changes this String's value and also sets Valid to be true.

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements json.Unmarshaler.

func (*String) UnmarshalText

func (s *String) UnmarshalText(text []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler.

func (String) Value

func (s String) Value() (driver.Value, error)

Value implements database/sql.Vauler.

type Time

type Time struct {
	Time  time.Time
	Null  bool
	Valid bool
}

Time is a nullable Time.

func NewTime

func NewTime(t time.Time, null bool, valid bool) Time

NewTime creates a new Time.

func TimeFrom

func TimeFrom(t time.Time) Time

TimeFrom creates a new Time that will always be valid.

func TimeFromPtr

func TimeFromPtr(t *time.Time) Time

TimeFromPtr creates a new Time that will be null if t is nil.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler. It will encode null if this time is null.

func (Time) MarshalText

func (t Time) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Time) Ptr

func (t Time) Ptr() *time.Time

Ptr returns a pointer to this Time value, or a nil pointer if this Time is null or not valid.

func (*Time) Scan

func (t *Time) Scan(value interface{}) (err error)

Scan implements database/sql.Scanner.

func (*Time) SetValid

func (t *Time) SetValid(v time.Time)

SetValid changes this Time value and sets it to be non-null.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements encoding/json.Unmarshaler. It supports string, object, and null input.

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(text []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler.

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements database/sql.Valuer.

type Validator

type Validator func(str string) bool

Validator is a wrapper for functions that return bool and accept string.

Jump to

Keyboard shortcuts

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