Skip to content

Commit

Permalink
fixed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshil Goel committed Mar 19, 2024
1 parent 3ab3531 commit b5b8387
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
3 changes: 1 addition & 2 deletions dql/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,7 @@ func (t *MathTree) subs(vmap varMap) error {
t.Var = ""
}
for _, i := range t.Child {
err := i.subs(vmap)
if err != nil {
if err := i.subs(vmap); err != nil {
return err
}
}
Expand Down
70 changes: 35 additions & 35 deletions dql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,64 +317,64 @@ type Request struct {
}

func parseValue(v varInfo) (types.Val, error) {
var err error
typ := v.Type
if v.Value != "" {
switch typ {
case "int":
{
var i int64
if i, err = strconv.ParseInt(v.Value, 0, 64); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected an int but got %v", v.Value)
}
if v.Value == "" {
return types.Val{}, errors.Errorf("No value found")
}

switch typ {
case "int":
{
if i, err := strconv.ParseInt(v.Value, 0, 64); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected an int but got %v", v.Value)
} else {
return types.Val{
Tid: types.IntID,
Value: i,
}, nil
}
case "float":
{
var i float64
if i, err = strconv.ParseFloat(v.Value, 64); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected a float but got %v", v.Value)
}
}
case "float":
{
if i, err := strconv.ParseFloat(v.Value, 64); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected a float but got %v", v.Value)
} else {
return types.Val{
Tid: types.FloatID,
Value: i,
}, nil
}
case "bool":
{
var i bool
if i, err = strconv.ParseBool(v.Value); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected a bool but got %v", v.Value)
}
}
case "bool":
{
if i, err := strconv.ParseBool(v.Value); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected a bool but got %v", v.Value)
} else {
return types.Val{
Tid: types.BoolID,
Value: i,
}, nil
}
case "vfloat":
{
var i []float32
if i, err = types.ParseVFloat(v.Value); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected a vfloat but got %v", v.Value)
}
}
case "vfloat":
{
if i, err := types.ParseVFloat(v.Value); err != nil {
return types.Val{}, errors.Wrapf(err, "Expected a vfloat but got %v", v.Value)
} else {
return types.Val{
Tid: types.VFloatID,
Value: i,
}, nil
}
case "string": // Value is a valid string. No checks required.
return types.Val{
Tid: types.StringID,
Value: v.Value,
}, nil
default:
return types.Val{}, errors.Errorf("Type %q not supported", typ)
}
case "string": // Value is a valid string. No checks required.
return types.Val{
Tid: types.StringID,
Value: v.Value,
}, nil
default:
return types.Val{}, errors.Errorf("Type %q not supported", typ)
}
return types.Val{}, errors.Errorf("No value found")
}

func checkValueType(vm varMap) error {
Expand Down
10 changes: 9 additions & 1 deletion dql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestParseMathSubs(t *testing.T) {
q := `query test($a: int) {
q(func: uid(0x1)) {
x as count(uid)
math(x + $a)
p : math(x + $a)
}
}`

Expand All @@ -57,6 +57,14 @@ func TestParseMathSubs(t *testing.T) {
require.NotNil(t, val)
require.Equal(t, val.Tid, types.IntID)
require.Equal(t, val.Value, int64(3))

r = Request{
Str: q,
Variables: map[string]string{"$a": "3.3"},
}
_, err = Parse(r)
require.Error(t, err)
require.Contains(t, err.Error(), "Expected an int but got 3.3")
}

func TestParseCountValError(t *testing.T) {
Expand Down

0 comments on commit b5b8387

Please sign in to comment.