Skip to content

Commit

Permalink
fix bug and delete String of Int and Float (#205)
Browse files Browse the repository at this point in the history
* fix bug and delete String of Int and Float

* fix bug and delete String of Int and Float

* add string

* add changes

* change module name

* change all module name

* Add helper functions and short circuit to avoid performance regression

Signed-off-by: Steve Coffman <[email protected]>

Co-authored-by: Steve Coffman <[email protected]>
  • Loading branch information
pjmd89 and StevenACoffman authored Apr 10, 2022
1 parent db38a01 commit 815d602
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validator
import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/vektah/gqlparser/v2/ast"
Expand Down Expand Up @@ -132,11 +133,11 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
kind := val.Type().Kind()
switch typ.NamedType {
case "Int":
if kind == reflect.String || kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 {
if kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 || kind == reflect.Float32 || kind == reflect.Float64 || IsValidIntString(val, kind) {
return val, nil
}
case "Float":
if kind == reflect.String || kind == reflect.Float32 || kind == reflect.Float64 || kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 {
if kind == reflect.Float32 || kind == reflect.Float64 || kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 || IsValidFloatString(val, kind) {
return val, nil
}
case "String":
Expand Down Expand Up @@ -218,3 +219,20 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
}
return val, nil
}

func IsValidIntString(val reflect.Value, kind reflect.Kind) bool {
if kind != reflect.String {
return false
}
_, e := strconv.ParseInt(fmt.Sprintf("%v", val.Interface()), 10, 64)

return e == nil
}

func IsValidFloatString(val reflect.Value, kind reflect.Kind) bool {
if kind != reflect.String {
return false
}
_, e := strconv.ParseFloat(fmt.Sprintf("%v", val.Interface()), 64)
return e == nil
}

0 comments on commit 815d602

Please sign in to comment.