Linting, README update, drone CI recipe update.

This commit is contained in:
Stanislav Nikitin 2019-12-03 21:28:26 +05:00
parent 169b9499fe
commit 50fa32a8ab
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
5 changed files with 17 additions and 26 deletions

View File

@ -33,8 +33,6 @@ steps:
- name: test-race - name: test-race
image: golang:1.13.4-alpine image: golang:1.13.4-alpine
environment:
CGO_ENABLED: 0
commands: commands:
- go test -race -test.v ./... - go test -race -test.v ./...
depends_on: depends_on:
@ -51,8 +49,6 @@ steps:
- name: benchmark-race - name: benchmark-race
image: golang:1.13.4-alpine image: golang:1.13.4-alpine
environment:
CGO_ENABLED: 0
commands: commands:
- go test -benchmem -run=^$ go.dev.pztrn.name/valiwork -race -bench . - go test -benchmem -run=^$ go.dev.pztrn.name/valiwork -race -bench .
depends_on: depends_on:

View File

@ -1,5 +1,7 @@
# ValiWork - validation framework # ValiWork - validation framework
[![Build Status](https://ci.dev.pztrn.name/api/badges/pztrn/valiwork/status.svg)](https://ci.dev.pztrn.name/pztrn/valiwork) [![Discord](https://img.shields.io/discord/632359730089689128)](https://discord.gg/CvUnEpM) ![Keybase XLM](https://img.shields.io/keybase/xlm/pztrn)
ValiWork is a validation framework that provides sane API and ability to write own validators that returns arbitrary things. It is goroutine-safe and fast. ValiWork is a validation framework that provides sane API and ability to write own validators that returns arbitrary things. It is goroutine-safe and fast.
## Default validators ## Default validators
@ -12,6 +14,8 @@ valiwork.InitializeDefaultValidators()
Default validators will return ``error``. Default validators will return ``error``.
*There are no default validators ATM. Feel free to submit PR with them!*
## Validators registering and namespacing ## Validators registering and namespacing
Default validators using "T_N" scheme, where ``T`` is data type (string, int, int64, etc.) and ``N`` is a validator name (which can be a generic string). Please, use same naming scheme. Example good validators names: Default validators using "T_N" scheme, where ``T`` is data type (string, int, int64, etc.) and ``N`` is a validator name (which can be a generic string). Please, use same naming scheme. Example good validators names:

View File

@ -20,7 +20,7 @@ func main() {
//stringToValidate := " I am pretty b@d $tring" //stringToValidate := " I am pretty b@d $tring"
valiwork.RegisterValidator(stringValidatorName, stringValidator) _ = valiwork.RegisterValidator(stringValidatorName, stringValidator)
} }
func stringValidator(thing interface{}, optional ...interface{}) []interface{} { func stringValidator(thing interface{}, optional ...interface{}) []interface{} {

View File

@ -3,7 +3,6 @@ package valiwork
import ( import (
// stdlib // stdlib
"log" "log"
"sync"
// local // local
"go.dev.pztrn.name/valiwork/validators" "go.dev.pztrn.name/valiwork/validators"
@ -11,7 +10,6 @@ import (
var ( var (
registeredValidators map[string]validators.ValidatorFunc registeredValidators map[string]validators.ValidatorFunc
rvMutex sync.RWMutex
) )
// nolint // nolint
@ -29,9 +27,7 @@ func RegisterValidator(validatorName string, validator validators.ValidatorFunc)
log.Println("Trying to register validator: '" + validatorName + "'...") log.Println("Trying to register validator: '" + validatorName + "'...")
} }
//rvMutex.RLock()
_, found := registeredValidators[validatorName] _, found := registeredValidators[validatorName]
//rvMutex.RUnlock()
if found { if found {
if DEBUG { if DEBUG {
@ -41,9 +37,7 @@ func RegisterValidator(validatorName string, validator validators.ValidatorFunc)
return ErrValidatorAlreadyRegistered return ErrValidatorAlreadyRegistered
} }
//rvMutex.Lock()
registeredValidators[validatorName] = validator registeredValidators[validatorName] = validator
//rvMutex.Unlock()
return nil return nil
} }
@ -55,9 +49,7 @@ func UnregisterValidator(validatorName string) error {
log.Println("Trying to unregister validator '" + validatorName + "'...") log.Println("Trying to unregister validator '" + validatorName + "'...")
} }
//rvMutex.RLock()
_, found := registeredValidators[validatorName] _, found := registeredValidators[validatorName]
//rvMutex.RUnlock()
if !found { if !found {
if DEBUG { if DEBUG {
@ -67,9 +59,7 @@ func UnregisterValidator(validatorName string) error {
return ErrValidatorNotRegistered return ErrValidatorNotRegistered
} }
//rvMutex.Lock()
delete(registeredValidators, validatorName) delete(registeredValidators, validatorName)
//rvMutex.Unlock()
return nil return nil
} }
@ -79,9 +69,7 @@ func UnregisterValidator(validatorName string) error {
func Validate(validatorName string, thing interface{}, optional ...interface{}) []interface{} { func Validate(validatorName string, thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{} var errs []interface{}
//rvMutex.RLock()
validator, found := registeredValidators[validatorName] validator, found := registeredValidators[validatorName]
//rvMutex.RUnlock()
if !found { if !found {
errs = append(errs, ErrValidatorNotRegistered) errs = append(errs, ErrValidatorNotRegistered)

View File

@ -42,6 +42,7 @@ func TestRegisterValidator(t *testing.T) {
for _, testCase := range testCases { for _, testCase := range testCases {
err := RegisterValidator(testCase.ValidatorName, testCase.ValidatorFunc) err := RegisterValidator(testCase.ValidatorName, testCase.ValidatorFunc)
if !testCase.ShouldFail { if !testCase.ShouldFail {
require.Nil(t, err) require.Nil(t, err)
} else { } else {
@ -88,7 +89,7 @@ func TestValidate(t *testing.T) {
testString := " I am test string" testString := " I am test string"
RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} { _ = RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{} var errs []interface{}
stringToValidate, ok := thing.(string) stringToValidate, ok := thing.(string)
@ -98,7 +99,7 @@ func TestValidate(t *testing.T) {
} }
if strings.HasPrefix(stringToValidate, " ") { if strings.HasPrefix(stringToValidate, " ") {
errs = append(errs, errors.New("string starts with whitespace, invalid!")) errs = append(errs, errors.New("string starts with whitespace, invalid"))
} }
return errs return errs
@ -116,7 +117,7 @@ func BenchmarkValidate(b *testing.B) {
testString := " I am test $tring" testString := " I am test $tring"
RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} { _ = RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{} var errs []interface{}
stringToValidate, ok := thing.(string) stringToValidate, ok := thing.(string)
@ -126,11 +127,11 @@ func BenchmarkValidate(b *testing.B) {
} }
if strings.HasPrefix(stringToValidate, " ") { if strings.HasPrefix(stringToValidate, " ") {
errs = append(errs, errors.New("string starts with whitespace, invalid!")) errs = append(errs, errors.New("string starts with whitespace, invalid"))
} }
if strings.Contains(stringToValidate, "$") { if strings.Contains(stringToValidate, "$") {
errs = append(errs, errors.New("string starts with whitespace, invalid!")) errs = append(errs, errors.New("string starts with whitespace, invalid"))
} }
return errs return errs
@ -150,7 +151,7 @@ func BenchmarkValidateAsync(b *testing.B) {
testString := " I am test $tring" testString := " I am test $tring"
RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} { _ = RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{} var errs []interface{}
stringToValidate, ok := thing.(string) stringToValidate, ok := thing.(string)
@ -160,11 +161,11 @@ func BenchmarkValidateAsync(b *testing.B) {
} }
if strings.HasPrefix(stringToValidate, " ") { if strings.HasPrefix(stringToValidate, " ") {
errs = append(errs, errors.New("string starts with whitespace, invalid!")) errs = append(errs, errors.New("string starts with whitespace, invalid"))
} }
if strings.Contains(stringToValidate, "$") { if strings.Contains(stringToValidate, "$") {
errs = append(errs, errors.New("string starts with whitespace, invalid!")) errs = append(errs, errors.New("string starts with whitespace, invalid"))
} }
return errs return errs
@ -214,7 +215,7 @@ func TestUnregisterValidator(t *testing.T) {
func TestUnregisterValidatorNotRegisteredValidator(t *testing.T) { func TestUnregisterValidatorNotRegisteredValidator(t *testing.T) {
initializeValidatorsStorage() initializeValidatorsStorage()
err := UnregisterValidator("this is definetely not registered thing") err := UnregisterValidator("this is definitely not registered thing")
require.NotNil(t, err) require.NotNil(t, err)
} }
@ -232,6 +233,7 @@ func BenchmarkUnregisterValidator(b *testing.B) {
} }
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = UnregisterValidator("string_test_validator_" + strconv.Itoa(i)) _ = UnregisterValidator("string_test_validator_" + strconv.Itoa(i))
} }
@ -253,6 +255,7 @@ func BenchmarkUnregisterValidatorAsync(b *testing.B) {
var w sync.WaitGroup var w sync.WaitGroup
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
w.Add(1) w.Add(1)