-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move validation to its own file
- Loading branch information
tauslim
committed
Feb 26, 2024
1 parent
24fbde8
commit a65ca09
Showing
2 changed files
with
76 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |