Skip to content

Commit

Permalink
Fix styling on go files.
Browse files Browse the repository at this point in the history
There were a couple of big issues.
1) Structs that were returned from public methods were not visible.
2) When a var is set in the same line as it is declared you should use type inference.
	not: var silly int = 42
	but: var silly = 42
3) Errors should be named ErrSomethingBad, not SomethingBadError
  • Loading branch information
plbogen2 committed Jan 19, 2018
1 parent 85dcd83 commit a870d5d
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 63 deletions.
16 changes: 8 additions & 8 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"
)

type dateRule struct {
type DateRule struct {
layout string
min, max time.Time
message string
Expand All @@ -27,40 +27,40 @@ type dateRule struct {
// the specified date range.
//
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func Date(layout string) *dateRule {
return &dateRule{
func Date(layout string) *DateRule {
return &DateRule{
layout: layout,
message: "must be a valid date",
rangeMessage: "the data is out of range",
}
}

// Error sets the error message that is used when the value being validated is not a valid date.
func (r *dateRule) Error(message string) *dateRule {
func (r *DateRule) Error(message string) *DateRule {
r.message = message
return r
}

// RangeError sets the error message that is used when the value being validated is out of the specified Min/Max date range.
func (r *dateRule) RangeError(message string) *dateRule {
func (r *DateRule) RangeError(message string) *DateRule {
r.rangeMessage = message
return r
}

// Min sets the minimum date range. A zero value means skipping the minimum range validation.
func (r *dateRule) Min(min time.Time) *dateRule {
func (r *DateRule) Min(min time.Time) *DateRule {
r.min = min
return r
}

// Max sets the maximum date range. A zero value means skipping the maximum range validation.
func (r *dateRule) Max(max time.Time) *dateRule {
func (r *DateRule) Max(max time.Time) *DateRule {
r.max = max
return r
}

// Validate checks if the given value is a valid date.
func (r *dateRule) Validate(value interface{}) error {
func (r *DateRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ func Example_five() {
fmt.Println(err)
// Output:
// Level: cannot be blank; Name: cannot be blank.
}
}
10 changes: 5 additions & 5 deletions in.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import "errors"
// In returns a validation rule that checks if a value can be found in the given list of values.
// Note that the value being checked and the possible range of values must be of the same type.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func In(values ...interface{}) *inRule {
return &inRule{
func In(values ...interface{}) *InRule {
return &InRule{
elements: values,
message: "must be a valid value",
}
}

type inRule struct {
type InRule struct {
elements []interface{}
message string
}

// Validate checks if the given value is valid or not.
func (r *inRule) Validate(value interface{}) error {
func (r *InRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand All @@ -37,7 +37,7 @@ func (r *inRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (r *inRule) Error(message string) *inRule {
func (r *InRule) Error(message string) *InRule {
r.message = message
return r
}
4 changes: 2 additions & 2 deletions in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestIn(t *testing.T) {
var v int = 1
var v = 1
var v2 *int
tests := []struct {
tag string
Expand All @@ -36,7 +36,7 @@ func TestIn(t *testing.T) {
}
}

func Test_inRule_Error(t *testing.T) {
func Test_InRule_Error(t *testing.T) {
r := In(1, 2, 3)
assert.Equal(t, "must be a valid value", r.message)
r.Error("123")
Expand Down
12 changes: 6 additions & 6 deletions length.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// If max is 0, it means there is no upper bound for the length.
// This rule should only be used for validating strings, slices, maps, and arrays.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func Length(min, max int) *lengthRule {
func Length(min, max int) *LengthRule {
message := "the value must be empty"
if min == 0 && max > 0 {
message = fmt.Sprintf("the length must be no more than %v", max)
Expand All @@ -23,7 +23,7 @@ func Length(min, max int) *lengthRule {
} else if min > 0 && max > 0 {
message = fmt.Sprintf("the length must be between %v and %v", min, max)
}
return &lengthRule{
return &LengthRule{
min: min,
max: max,
message: message,
Expand All @@ -35,20 +35,20 @@ func Length(min, max int) *lengthRule {
// This rule should only be used for validating strings, slices, maps, and arrays.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
// If the value being validated is not a string, the rule works the same as Length.
func RuneLength(min, max int) *lengthRule {
func RuneLength(min, max int) *LengthRule {
r := Length(min, max)
r.rune = true
return r
}

type lengthRule struct {
type LengthRule struct {
min, max int
message string
rune bool
}

// Validate checks if the given value is valid or not.
func (v *lengthRule) Validate(value interface{}) error {
func (v *LengthRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand All @@ -71,7 +71,7 @@ func (v *lengthRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (v *lengthRule) Error(message string) *lengthRule {
func (v *LengthRule) Error(message string) *LengthRule {
v.message = message
return v
}
2 changes: 1 addition & 1 deletion length_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestRuneLength(t *testing.T) {
}
}

func Test_lengthRule_Error(t *testing.T) {
func Test_LengthRule_Error(t *testing.T) {
r := Length(10, 20)
assert.Equal(t, "the length must be between 10 and 20", r.message)

Expand Down
10 changes: 5 additions & 5 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
// Match returns a validation rule that checks if a value matches the specified regular expression.
// This rule should only be used for validating strings and byte slices, or a validation error will be reported.
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func Match(re *regexp.Regexp) *matchRule {
return &matchRule{
func Match(re *regexp.Regexp) *MatchRule {
return &MatchRule{
re: re,
message: "must be in a valid format",
}
}

type matchRule struct {
type MatchRule struct {
re *regexp.Regexp
message string
}

// Validate checks if the given value is valid or not.
func (v *matchRule) Validate(value interface{}) error {
func (v *MatchRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil {
return nil
Expand All @@ -41,7 +41,7 @@ func (v *matchRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (v *matchRule) Error(message string) *matchRule {
func (v *MatchRule) Error(message string) *MatchRule {
v.message = message
return v
}
2 changes: 1 addition & 1 deletion match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestMatch(t *testing.T) {
}
}

func Test_matchRule_Error(t *testing.T) {
func Test_MatchRule_Error(t *testing.T) {
r := Match(regexp.MustCompile("[a-z]+"))
assert.Equal(t, "must be in a valid format", r.message)
r.Error("123")
Expand Down
24 changes: 12 additions & 12 deletions minmax.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

type thresholdRule struct {
type ThresholdRule struct {
threshold interface{}
operator int
message string
Expand All @@ -29,8 +29,8 @@ const (
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Min(min interface{}) *thresholdRule {
return &thresholdRule{
func Min(min interface{}) *ThresholdRule {
return &ThresholdRule{
threshold: min,
operator: greaterEqualThan,
message: fmt.Sprintf("must be no less than %v", min),
Expand All @@ -42,16 +42,16 @@ func Min(min interface{}) *thresholdRule {
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Max(max interface{}) *thresholdRule {
return &thresholdRule{
func Max(max interface{}) *ThresholdRule {
return &ThresholdRule{
threshold: max,
operator: lessEqualThan,
message: fmt.Sprintf("must be no greater than %v", max),
}
}

// Exclusive sets the comparison to exclude the boundary value.
func (r *thresholdRule) Exclusive() *thresholdRule {
func (r *ThresholdRule) Exclusive() *ThresholdRule {
if r.operator == greaterEqualThan {
r.operator = greaterThan
r.message = fmt.Sprintf("must be greater than %v", r.threshold)
Expand All @@ -63,7 +63,7 @@ func (r *thresholdRule) Exclusive() *thresholdRule {
}

// Validate checks if the given value is valid or not.
func (r *thresholdRule) Validate(value interface{}) error {
func (r *ThresholdRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
Expand Down Expand Up @@ -119,12 +119,12 @@ func (r *thresholdRule) Validate(value interface{}) error {
}

// Error sets the error message for the rule.
func (r *thresholdRule) Error(message string) *thresholdRule {
func (r *ThresholdRule) Error(message string) *ThresholdRule {
r.message = message
return r
}

func (r *thresholdRule) compareInt(threshold, value int64) bool {
func (r *ThresholdRule) compareInt(threshold, value int64) bool {
switch r.operator {
case greaterThan:
return value > threshold
Expand All @@ -137,7 +137,7 @@ func (r *thresholdRule) compareInt(threshold, value int64) bool {
}
}

func (r *thresholdRule) compareUint(threshold, value uint64) bool {
func (r *ThresholdRule) compareUint(threshold, value uint64) bool {
switch r.operator {
case greaterThan:
return value > threshold
Expand All @@ -150,7 +150,7 @@ func (r *thresholdRule) compareUint(threshold, value uint64) bool {
}
}

func (r *thresholdRule) compareFloat(threshold, value float64) bool {
func (r *ThresholdRule) compareFloat(threshold, value float64) bool {
switch r.operator {
case greaterThan:
return value > threshold
Expand All @@ -163,7 +163,7 @@ func (r *thresholdRule) compareFloat(threshold, value float64) bool {
}
}

func (r *thresholdRule) compareTime(threshold, value time.Time) bool {
func (r *ThresholdRule) compareTime(threshold, value time.Time) bool {
switch r.operator {
case greaterThan:
return value.After(threshold)
Expand Down
2 changes: 1 addition & 1 deletion minmax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package validation

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"time"
)

func TestMin(t *testing.T) {
Expand Down
28 changes: 14 additions & 14 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
)

var (
// StructPointerError is the error that a struct being validated is not specified as a pointer.
StructPointerError = errors.New("only a pointer to a struct can be validated")
// ErrStructPointer is the error that a struct being validated is not specified as a pointer.
ErrStructPointer = errors.New("only a pointer to a struct can be validated")
)

type (
// FieldPointerError is the error that a field is not specified as a pointer.
FieldPointerError int
// ErrFieldPointer is the error that a field is not specified as a pointer.
ErrFieldPointer int

// FieldNotFoundError is the error that a field cannot be found in the struct.
FieldNotFoundError int
// ErrFieldNotFound is the error that a field cannot be found in the struct.
ErrFieldNotFound int

// FieldRules represents a rule set associated with a struct field.
FieldRules struct {
Expand All @@ -30,13 +30,13 @@ type (
}
)

// Error returns the error string of FieldPointerError.
func (e FieldPointerError) Error() string {
// Error returns the error string of ErrFieldPointer.
func (e ErrFieldPointer) Error() string {
return fmt.Sprintf("field #%v must be specified as a pointer", int(e))
}

// Error returns the error string of FieldNotFoundError.
func (e FieldNotFoundError) Error() string {
// Error returns the error string of ErrFieldNotFound.
func (e ErrFieldNotFound) Error() string {
return fmt.Sprintf("field #%v cannot be found in the struct", int(e))
}

Expand All @@ -62,7 +62,7 @@ func ValidateStruct(structPtr interface{}, fields ...*FieldRules) error {
value := reflect.ValueOf(structPtr)
if value.Kind() != reflect.Ptr || !value.IsNil() && value.Elem().Kind() != reflect.Struct {
// must be a pointer to a struct
return NewInternalError(StructPointerError)
return NewInternalError(ErrStructPointer)
}
if value.IsNil() {
// treat a nil struct pointer as valid
Expand All @@ -75,14 +75,14 @@ func ValidateStruct(structPtr interface{}, fields ...*FieldRules) error {
for i, fr := range fields {
fv := reflect.ValueOf(fr.fieldPtr)
if fv.Kind() != reflect.Ptr {
return NewInternalError(FieldPointerError(i))
return NewInternalError(ErrFieldPointer(i))
}
ft := findStructField(value, fv)
if ft == nil {
return NewInternalError(FieldNotFoundError(i))
return NewInternalError(ErrFieldNotFound(i))
}
if err := Validate(fv.Elem().Interface(), fr.rules...); err != nil {
if ie, ok := err.(InternalError); ok && ie.InternalError() != nil{
if ie, ok := err.(InternalError); ok && ie.InternalError() != nil {
return err
}
if ft.Anonymous {
Expand Down
Loading

0 comments on commit a870d5d

Please sign in to comment.