Skip to content

Commit

Permalink
fix: Handle proper type conversion on sort nodes. (sourcenetwork#228)
Browse files Browse the repository at this point in the history
* Handled CBOR encoding edge case

* Fixed tests which manually casted results to uint64 due to previous encoding inconsistency
  • Loading branch information
jsimnz authored Mar 2, 2022
1 parent ffb9480 commit 882f00d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
7 changes: 6 additions & 1 deletion db/fetcher/versioned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@ func compareVersionedDocs(t *testing.T, doc, expected map[string]interface{}) {
// make sure our floats are converted
if f, ok := expected[k].(float64); ok {
if f == float64(int64(f)) {
expected[k] = int64(f)
expected[k] = float64(f)
}

if u, ok := v.(uint64); ok {
v = float64(u)
}
}

Expand All @@ -356,6 +360,7 @@ func compareVersionedDocs(t *testing.T, doc, expected map[string]interface{}) {
v = int64(i)
}
}

assert.Equal(t, expected[k], v)
}
}
Expand Down
2 changes: 1 addition & 1 deletion db/tests/query/one_to_many/with_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestQueryOneToManyWithParentJoinGroupNumber(t *testing.T) {
"published": []map[string]interface{}{
{
"name": "Histoiare des Celtes et particulierement des Gaulois et des Germains depuis les temps fabuleux jusqua la prise de Roze par les Gaulois",
"rating": uint64(2),
"rating": float64(2),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions db/tests/query/one_to_two_many/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TestQueryOneToTwoManyWithNamedAndUnnamedRelationships(t *testing.T) {
},
"price": map[string]interface{}{
"currency": "SEK",
"value": uint64(129),
"value": float64(129),
},
},
{
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestQueryOneToTwoManyWithNamedAndUnnamedRelationships(t *testing.T) {
{
"name": "A Time for Mercy",
"price": map[string]interface{}{
"value": uint64(129),
"value": float64(129),
},
},
{
Expand Down
14 changes: 14 additions & 0 deletions document/encoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ func (e EncProperty) Decode() (core.CType, interface{}, error) {
}
val = stringArray
}
} else { // CBOR often encodes values typed as floats as ints
switch e.Desc.Kind {
case base.FieldKind_FLOAT:
switch v := val.(type) {
case int64:
return ctype, float64(v), nil
case int:
return ctype, float64(v), nil
case uint64:
return ctype, float64(v), nil
case uint:
return ctype, float64(v), nil
}
}
}

return ctype, val, nil
Expand Down

0 comments on commit 882f00d

Please sign in to comment.