structcheck

package module
v0.0.0-...-3ea3ab3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2014 License: MIT Imports: 9 Imported by: 2

README

structcheck

Checks that constraints on structs are met. Constraints are read as a comma-delimited list on the 'checks' annotation. Validate constraints by running structcheck.Validate().

See structcheck.Check in godoc for the list of possible constraints.

Example usage:

package main

import (
  "bytes"
  "encoding/json"
  "fmt"
  "github.com/Manbeardo/structcheck"
)

type MyJsonObjectType struct {
  NestedObject struct{
    A *int `checks:"NotNil"`
    B *int `checks:"NotNil"`
  }
}

var badJson = []byte(`{
  "NestedObject":{
    "A":1
  }
}`)

func main() {
  var o MyJsonObjectType
  json.NewDecoder(bytes.NewBuffer(badJson)).Decode(&o)
  err := structcheck.Validate(o)
  if err != nil {
    fmt.Println(err.Error())
  }
}

Prints:

The following field(s) failed checks: 
    MyJsonObjectType.NestedObject.B: NotNil: (*int)(nil)

Documentation

Overview

Package structcheck validates struct constraints.

Checks that constraints on structs are met. Constraints are read as a comma-delimited list on the "checks" tag. Validate constraints by running structcheck.Validate().

See the Checks map for the full list of built-in checks.

Example:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "github.com/Manbeardo/structcheck"
)

type MyJsonObjectType struct {
    NestedObject struct{
        Number  int   `checks:"Positive"`
        Pointer *int  `checks:"NotNil"`
        Slice   []int `checks:"NotEmpty"`
    }
}

var badJson = []byte(`{"NestedObject":{"Number":-1}}`)

func main() {
    var o MyJsonObjectType
    json.NewDecoder(bytes.NewBuffer(badJson)).Decode(&o)
    err := structcheck.Validate(o)
    if err != nil {
        fmt.Println(err.Error())
    }
}

Prints:

The following field(s) failed checks:
    MyJsonObjectType.NestedObject.Number:  Positive: (int)(-1)
    MyJsonObjectType.NestedObject.Pointer: NotNil:   (*int)(nil)
    MyJsonObjectType.NestedObject.Slice:   NotEmpty: []int(nil)

Index

Constants

This section is empty.

Variables

View Source
var DefaultChecks = map[string]Check{
	"NotNil": func(v reflect.Value) bool {
		return !(Nilable.Check(v) && v.IsNil())
	},
	"Nil": func(v reflect.Value) bool {
		return !(Nilable.Check(v) && !v.IsNil())
	},
	"Positive": func(v reflect.Value) bool {
		return CheckSign(v, SignPositive)
	},
	"Negative": func(v reflect.Value) bool {
		return CheckSign(v, SignNegative)
	},
	"NoSign": func(v reflect.Value) bool {
		return CheckSign(v, SignNone)
	},
	"NotEmpty": func(v reflect.Value) bool {
		return !(Container.Check(v) && v.Len() == 0)
	},
	"Empty": func(v reflect.Value) bool {
		return !(Container.Check(v) && v.Len() != 0)
	},
	"Nilable": func(v reflect.Value) bool {
		return Nilable.Check(v)
	},
	"Numeric": func(v reflect.Value) bool {
		return Numeric.Check(v)
	},
	"Container": func(v reflect.Value) bool {
		return Container.Check(v)
	},
}

Functions

func CheckFieldExists

func CheckFieldExists(i interface{}, fieldName string) bool

func CheckFieldsExist

func CheckFieldsExist(i interface{}, fieldNames []string) error

func CheckFieldsNotNil

func CheckFieldsNotNil(i interface{}, fieldNames []string) error

checks that the named fields and their parents exist and are not null

func CheckNoNils

func CheckNoNils(i interface{}) error

checks that no fields in the struct are nil

func CheckSign

func CheckSign(v reflect.Value, s Sign) bool

func CustomValidate

func CustomValidate(i interface{}, checkFinder CheckFinder) error

runs Validate with a custom set of checks

func Validate

func Validate(i interface{}) error

drills down (follows pointer and interface indirection) to a struct and recursively runs checks on all fields.

Types

type ByFieldOrder

type ByFieldOrder []Field

sorts fields by their natural (in-struct) order

func (ByFieldOrder) Len

func (a ByFieldOrder) Len() int

func (ByFieldOrder) Less

func (a ByFieldOrder) Less(i, j int) bool

func (ByFieldOrder) Swap

func (a ByFieldOrder) Swap(i, j int)

type Check

type Check func(v reflect.Value) bool

returns true if the check is met

type CheckFinder

type CheckFinder func(v metaValue) ([]Check, []string, error)

func BuildFixedCheckFinder

func BuildFixedCheckFinder(checkNames []string, checkSet map[string]Check) (CheckFinder, error)

Builds a CheckFinder that returns the same set of checks for all fields. checkSet is read at build-time, not check-time.

func BuildStringyCheckFinder

func BuildStringyCheckFinder(field2checks map[string][]string, checkSet map[string]Check) (CheckFinder, error)

Builds a CheckFinder that runs the named checks on the named fields. checkSet and field2checks are copied and verified at build-time

func BuildTagCheckFinder

func BuildTagCheckFinder(checkSet map[string]Check) CheckFinder

type ErrorChecksFailed

type ErrorChecksFailed struct {
	Field2Checks map[Field][]string
}

returned when checks fail on fields

func (ErrorChecksFailed) Error

func (e ErrorChecksFailed) Error() string

type ErrorIllegalCheck

type ErrorIllegalCheck struct {
	Reason string
	// contains filtered or unexported fields
}

returned when an illegal (likely misspelled) check is encountered

func (ErrorIllegalCheck) Error

func (e ErrorIllegalCheck) Error() string

type ErrorInvalidKind

type ErrorInvalidKind struct {
	reflect.Type
}

returned when the top level object does not drill down to a struct.

func (ErrorInvalidKind) Error

func (e ErrorInvalidKind) Error() string

type ErrorNilValue

type ErrorNilValue struct{}

returned when a top level nil is received

func (ErrorNilValue) Error

func (e ErrorNilValue) Error() string

type Field

type Field struct {
	Name   string // qualified field name (e.g. RootType.Field1.Field2)
	Value  string // stringified field value
	Number string // the index in the field tree (e.g. 0.1 for the second field of the first field of the root)
}

type KindClass

type KindClass map[reflect.Kind]interface{}

func (KindClass) Check

func (class KindClass) Check(v reflect.Value) bool

true if v is a member of this class

type Sign

type Sign int
const (
	SignNone Sign = iota
	SignPositive
	SignNegative
)

Jump to

Keyboard shortcuts

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