pickyjson

package
v0.0.0-...-589bc69 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package pickyjson is useful for extracting simple json types with certain constraints. For example, extracting a string but only of a certain type, or with a default value if not set

TODO example

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTooLong   = common.ExpectedErr{Code: 400, Err: "too long"}
	ErrTooShort  = common.ExpectedErr{Code: 400, Err: "too short"}
	ErrMalformed = common.ExpectedErr{Code: 400, Err: "malformed"}
	ErrTooBig    = common.ExpectedErr{Code: 400, Err: "too big"}
	ErrTooSmall  = common.ExpectedErr{Code: 400, Err: "too small"}
)

Various errors which may be returned by this package

View Source
var (
	ErrFieldRequiredf = func(f string) error {
		return common.ExpectedErrf(400, "field %s required", f)
	}
)

Functions which return errors based on the related field names

Functions

func CheckRequired

func CheckRequired(i interface{}) error

CheckRequired takes in a struct and looks through it to ensure all required parameters were actually filled in post-unmarshal. It will look through all struct recursively (although it won't traverse slices/maps at the moment)

Types

type Int64

type Int64 struct {
	// Maximum and minimum values that the integer may be
	//
	// By default (both 0) this will only allow for positive integers. Min can
	// be set to math.MinInt64 to allow for negative integers. If one is set the
	// other is assumed to be set as well
	Max, Min int64

	// A function the integer will be passed to. It returns whether or not the
	// integer is valid
	Func func(int64) bool

	// The place the value will be filled into if it passes all constraints.
	// This can be pre-filled with a default value
	Int64 int64

	// Whether or not this must be filled in, if specified for a field in a
	// struct
	Require bool
	// contains filtered or unexported fields
}

Int64 is a wrapper for a normal go int64, but with extra constraints. If a constraint is not specified it will not be applied

func (*Int64) MarshalJSON

func (i *Int64) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface, marshalling the value of the Int64 field

func (Int64) Required

func (i Int64) Required() Int64

Required is a convenience method which returns an exact copy of the Int64 with Require set to true

func (*Int64) String

func (i *Int64) String() string

String implementation for fmt.Stringer

func (*Int64) UnmarshalJSON

func (i *Int64) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface, unmarshalling the given encoded json into the Int64 field. If the value doesn't fit within any of the constraints an error will be returned

type Str

type Str struct {
	// Maximum and minimum lengths that the string may be. MinLength can be used
	// essentially require the Str to be set if it's a field in a struct
	MaxLength, MinLength int

	// A function the string will be passed to, useful for more complicated
	// checks. It returns whether or not the string is valid
	Func func(string) bool

	// A function which can spit out a new form of the string value (assuming it
	// passes all other constraints)
	Map func(string) (string, error)

	// The place the value will be filled into if it passes all constraints.
	// This can be pre-filled with a default value
	Str string
}

Str is a wrapper for a normal go string, but with extra constraints. If a constraint is not specified it will not be applied

func (*Str) MarshalJSON

func (s *Str) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface, marshalling the value of the Str field

func (Str) Required

func (s Str) Required() Str

Required is a convenience method which returns an exact copy of the String being called on except with a MinLength of 1 (if MinLength wasn't already set)

func (*Str) String

func (s *Str) String() string

String implementation for fmt.Stringer

func (*Str) UnmarshalJSON

func (s *Str) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface, unmarshalling the given encoded json into the Str field. If the value doesn't fit within any of the constraints an error will be returned

Jump to

Keyboard shortcuts

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