Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug and delete String of Int and Float #205

Merged
merged 11 commits into from
Apr 10, 2022
6 changes: 4 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,12 @@ 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 {
_,e := strconv.ParseInt(fmt.Sprintf("%v", val.Interface()), 10, 64);
if e == nil && (kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 || kind == reflect.Float32 || kind == reflect.Float64){
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why you removed the kind == reflect.String condition as "45.230343" strings are often how they are serialized.

Copy link
Contributor Author

@pjmd89 pjmd89 Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tests return an error because you are giving a string to an Int.

I removed the string from the int and float because if you set a variable as int or float you should get an int or a float, not a string, in fact, I got errors for that too. I hope it is useful for something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still, adding the string without removing the validation on the Int, there is no problem. the bad thing is that it can return a string in an int or float.

return val, nil
}
case "String":
Expand Down