Skip to content

Commit

Permalink
refactor: move validation to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
tauslim committed Feb 26, 2024
1 parent 24fbde8 commit a65ca09
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 71 deletions.
71 changes: 0 additions & 71 deletions converter.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,9 @@
package gorql

import (
"errors"
"fmt"
"math"
"reflect"
"time"
)

func errorType(v interface{}, expected string) error {
actual := "nil"
if v != nil {
actual = reflect.TypeOf(v).Kind().String()
}
return fmt.Errorf("expect <%s>, got <%s>", expected, actual)
}

// validate that the underlined element of given interface is a boolean.
func validateBool(v interface{}) error {
if _, ok := v.(bool); !ok {
return errorType(v, "bool")
}
return nil
}

// validate that the underlined element of given interface is a string.
func validateString(v interface{}) error {
if _, ok := v.(string); !ok {
return errorType(v, "string")
}
return nil
}

// validate that the underlined element of given interface is a float.
func validateFloat(v interface{}) error {
if _, ok := v.(float64); !ok {
return errorType(v, "float64")
}
return nil
}

// validate that the underlined element of given interface is an int.
func validateInt(v interface{}) error {
n, ok := v.(float64)
if !ok {
return errorType(v, "int")
}
if math.Trunc(n) != n {
return errors.New("not an integer")
}
return nil
}

// validate that the underlined element of given interface is an int and greater than 0.
func validateUInt(v interface{}) error {
if err := validateInt(v); err != nil {
return err
}
if v.(float64) < 0 {
return errors.New("not an unsigned integer")
}
return nil
}

// validate that the underlined element of this interface is a "datetime" string.
func validateTime(layout string) func(interface{}) error {
return func(v interface{}) error {
s, ok := v.(string)
if !ok {
return errorType(v, "string")
}
_, err := time.Parse(layout, s)
return err
}
}

// convert float to int.
func convertInt(v interface{}) interface{} {
return int(v.(float64))
Expand Down
76 changes: 76 additions & 0 deletions validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gorql

import (
"errors"
"fmt"
"math"
"reflect"
"time"
)

func errorType(v interface{}, expected string) error {
actual := "nil"
if v != nil {
actual = reflect.TypeOf(v).Kind().String()
}
return fmt.Errorf("expect <%s>, got <%s>", expected, actual)
}

// validate that the underlined element of given interface is a boolean.
func validateBool(v interface{}) error {
if _, ok := v.(bool); !ok {
return errorType(v, "bool")
}
return nil
}

// validate that the underlined element of given interface is a string.
func validateString(v interface{}) error {
if _, ok := v.(string); !ok {
return errorType(v, "string")
}
return nil
}

// validate that the underlined element of given interface is a float.
func validateFloat(v interface{}) error {
if _, ok := v.(float64); !ok {
return errorType(v, "float64")
}
return nil
}

// validate that the underlined element of given interface is an int.
func validateInt(v interface{}) error {
n, ok := v.(float64)
if !ok {
return errorType(v, "int")
}
if math.Trunc(n) != n {
return errors.New("not an integer")
}
return nil
}

// validate that the underlined element of given interface is an int and greater than 0.
func validateUInt(v interface{}) error {
if err := validateInt(v); err != nil {
return err
}
if v.(float64) < 0 {
return errors.New("not an unsigned integer")
}
return nil
}

// validate that the underlined element of this interface is a "datetime" string.
func validateTime(layout string) func(interface{}) error {
return func(v interface{}) error {
s, ok := v.(string)
if !ok {
return errorType(v, "string")
}
_, err := time.Parse(layout, s)
return err
}
}

0 comments on commit a65ca09

Please sign in to comment.