Help us shape the future of Go! Take the 2026 Go Developer Survey

checks

package module
v0.0.0-...-ef18f20 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2019 License: MIT Imports: 5 Imported by: 0

README

Build Status Coverage Status GoDoc

go-checks

Go package for field checks

Install

go get github.com/arteev/go-checks

Usage

package main

import (
	"fmt"

	"github.com/arteev/go-checks"
)

type Config struct {
	Enabled  bool
	Listen   string `check:"required"`
	LogLevel string `check:"required,expect:info;debug;error;"`
	Timeout  int    `check:"deprecated"`

	ValueForFunc string `check:"call:ValueCheck"`
	ValueRegexp  string `check:"re:[a-z]+"`
}

func (c Config) Check() error {
	if !c.Enabled {
		return checks.ErrSkip
	}
	//special check
	return nil
}

func (c Config) ValueCheck(name string, s string) error {
	// special check for specific fields
	if name != "ValueForFunc" || s == "valid" {
		return nil
	}
	return fmt.Errorf("Not valid value: %q", s)
}

func main() {
	//all errors
	v := &Config{
		Enabled:     true,
		LogLevel:    "warn",
		Timeout:     10,
		ValueRegexp: "123",
	}
	errs := checks.CheckAll(v)
	if len(errs) != 0 {
		fmt.Println("Errors:", errs)
	}

	//first error
	err := checks.Check(v)
	if err != nil {
		fmt.Println("Error:", err)
	}

	//skip checks
	v.Enabled = false
	err = checks.Check(v)
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("Skip checks")
	}

	//with deprecated
	v.Enabled = true
	checker := checks.New(checks.ModeAll, checks.WarningType)
	errs = checker.Check(v)
	if len(errs) != 0 {
		fmt.Println("Errors:", errs)
	}

	//normal
	v = &Config{
		Enabled:      true,
		Listen:       ":8080",
		LogLevel:     "debug",
		ValueForFunc: "valid",
		ValueRegexp:  "correct",
	}
	errs = checks.CheckAll(v)
	if len(errs) == 0 {
		fmt.Println("OK")
	} else {
		fmt.Println("Errors:", errs)
	}
}

Output

Errors: [value required: Listen unexpected value: LogLevel warn Not valid value: "" no matches: ValueRegexp re:[a-z]+]
Error: value required: Listen
Skip checks
Errors: [deprecated parameter: Timeout Not valid value: ""]
OK

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrValueRequired        = errors.New("value required")
	ErrValueUnexpected      = errors.New("unexpected value")
	ErrDeprecated           = errors.New("deprecated parameter")
	ErrWrongSignatureMethod = errors.New("wrong signature method")
	ErrNoMatch              = errors.New("no matches")
	ErrBadSyntax            = errors.New("bad syntax")
	ErrSkip                 = errors.New("skip")
)

Errors

Functions

func Check

func Check(v interface{}) error

Check check structure

func CheckAll

func CheckAll(v interface{}) []error

CheckAll check all fields of the structure

func Filter

func Filter(errors []error, typ Type) []error

Filter returns filtred slice error by Type

Types

type Checker

type Checker interface {
	Check() error
}

Checker is the interface that wraps the basic Check method.

type ErrorCheck

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

ErrorCheck check

func (ErrorCheck) Error

func (e ErrorCheck) Error() string

func (ErrorCheck) GetType

func (e ErrorCheck) GetType() Type

GetType returns error type

type ErrorCheckResult

type ErrorCheckResult struct {
	ErrorCheck
	FieldName string
	Value     interface{}
}

ErrorCheckResult implements concrete check result

func (ErrorCheckResult) Error

func (e ErrorCheckResult) Error() string

type Iterator

type Iterator interface {
	HasNext() bool
	Next() Value
}

Iterator is the interface iterator

type Mode

type Mode int

Mode check mode

const (
	ModeFirst Mode = iota
	ModeAll
)

Known check modes

type SimpeChecker

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

SimpeChecker implements simple checks: required, expect, deprecated

func New

func New(m Mode, e Type) *SimpeChecker

New returns new checker

func (*SimpeChecker) Check

func (c *SimpeChecker) Check(v interface{}) []error

Check checks value

type Type

type Type int

Type errors

const (
	ErrorType Type = 1 << iota
	WarningType

	ErrorAll Type = ErrorType | WarningType
)

Type errors

type Value

type Value interface {
	Name() string
	Tag() reflect.StructTag
	Value() reflect.Value
	Parent() Value
	Struct() *reflect.StructField
}

Value is the interface Value of structs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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