Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: numbers are not decoded from gql to value
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Dec 6, 2021
1 parent 73143b7 commit 2ddbc8b
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 14 deletions.
44 changes: 44 additions & 0 deletions internal/adapter/gql/gqlmodel/convert_property_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package gqlmodel

import (
"encoding/json"
"testing"

"github.com/reearth/reearth-backend/pkg/property"
"github.com/stretchr/testify/assert"
)

func TestFromPropertyValueAndType(t *testing.T) {
type args struct {
v interface{}
t ValueType
}
tests := []struct {
name string
args args
want *property.Value
}{
{
name: "number",
args: args{
v: 1.1,
t: ValueTypeNumber,
},
want: property.ValueTypeNumber.ValueFrom(1.1),
},
{
name: "json number",
args: args{
v: json.Number("1.1"),
t: ValueTypeNumber,
},
want: property.ValueTypeNumber.ValueFrom(1.1),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, FromPropertyValueAndType(tt.args.v, tt.args.t))
})
}
}
63 changes: 49 additions & 14 deletions internal/adapter/gql/gqlmodel/convert_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,29 @@ func gqlValueToValueInterface(v interface{}) interface{} {
return nil
}
switch v2 := v.(type) {
case bool:
return v2
case float64:
return v2
case string:
return v2
case *url.URL:
return v2
case LatLng:
return value.LatLng{
Lat: v2.Lat,
Lng: v2.Lng,
}
case *LatLng:
if v2 == nil {
return nil
}
return value.LatLng{
Lat: v2.Lat,
Lng: v2.Lng,
}
case LatLngHeight:
return value.LatLngHeight{
Lat: v2.Lat,
Lng: v2.Lng,
Height: v2.Height,
}
case *LatLng:
return value.LatLng{
Lat: v2.Lat,
Lng: v2.Lng,
}
case *LatLngHeight:
if v2 == nil {
return nil
}
return value.LatLngHeight{
Lat: v2.Lat,
Lng: v2.Lng,
Expand All @@ -122,6 +120,19 @@ func gqlValueToValueInterface(v interface{}) interface{} {
})
}
return value.Coordinates(res)
case []*LatLngHeight:
res := make([]value.LatLngHeight, 0, len(v2))
for _, c := range v2 {
if c == nil {
continue
}
res = append(res, value.LatLngHeight{
Lat: c.Lat,
Lng: c.Lng,
Height: c.Height,
})
}
return value.Coordinates(res)
case [][]LatLngHeight:
res := make([]value.Coordinates, 0, len(v2))
for _, d := range v2 {
Expand All @@ -136,6 +147,30 @@ func gqlValueToValueInterface(v interface{}) interface{} {
res = append(res, coord)
}
return value.Polygon(res)
case [][]*LatLngHeight:
res := make([]value.Coordinates, 0, len(v2))
for _, d := range v2 {
coord := make([]value.LatLngHeight, 0, len(d))
for _, c := range d {
if c == nil {
continue
}
coord = append(coord, value.LatLngHeight{
Lat: c.Lat,
Lng: c.Lng,
Height: c.Height,
})
}
res = append(res, coord)
}
return value.Polygon(res)
case Rect:
return value.Rect{
West: v2.West,
East: v2.East,
North: v2.North,
South: v2.South,
}
case *Rect:
return value.Rect{
West: v2.West,
Expand All @@ -144,7 +179,7 @@ func gqlValueToValueInterface(v interface{}) interface{} {
South: v2.South,
}
}
return nil
return v
}

func ToValueType(t value.Type) ValueType {
Expand Down
13 changes: 13 additions & 0 deletions internal/adapter/gql/gqlmodel/convert_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gqlmodel

import (
"testing"

"github.com/reearth/reearth-backend/pkg/value"
"github.com/stretchr/testify/assert"
)

func Test_FromValueType(t *testing.T) {
assert.Equal(t, value.TypeString, FromValueType(ValueTypeString))
assert.Equal(t, value.TypeNumber, FromValueType(ValueTypeNumber))
}

0 comments on commit 2ddbc8b

Please sign in to comment.