From 7941af9464074978ce3bb022be38ca463896aca5 Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Mon, 1 Jun 2020 16:09:01 -0600 Subject: [PATCH] Fix loading of numbers. (#3) The javascript firestore sdks can convert automatically between integer and floating types. This updates the loading code to convert between ints and floats. Based on https://github.com/googleapis/google-cloud-go/blob/27b5416a80e6a65ee9eebda55f06def51b9b7b88/firestore/from_value.go#L119 --- load.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/load.go b/load.go index 6a13eca..48d1a27 100644 --- a/load.go +++ b/load.go @@ -151,8 +151,18 @@ func (l *propertyLoader) load(codec *structCodec, structValue reflect.Value, p P func setVal(v reflect.Value, pValue interface{}) string { switch v.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - x, ok := pValue.(int64) - if !ok && pValue != nil { + var x int64 + switch pv := pValue.(type) { + case int64: + x = pv + case float64: + x = int64(pv) + if float64(x) != pv { + return fmt.Sprintf("float %f does not fit into %s", pv, v.Type()) + } + case nil: + x = 0 + default: return typeMismatchReason(pValue, v) } if v.OverflowInt(x) { @@ -177,14 +187,24 @@ func setVal(v reflect.Value, pValue interface{}) string { } } case reflect.Float32, reflect.Float64: - x, ok := pValue.(float64) - if !ok && pValue != nil { + var f float64 + switch x := pValue.(type) { + case float64: + f = x + case int64: + f = float64(x) + if int64(f) != x { + return fmt.Sprintf("value %v overflows struct field of type %v", x, v.Type()) + } + case nil: + f = 0 + default: return typeMismatchReason(pValue, v) } - if v.OverflowFloat(x) { - return fmt.Sprintf("value %v overflows struct field of type %v", x, v.Type()) + if v.OverflowFloat(f) { + return fmt.Sprintf("value %v overflows struct field of type %v", f, v.Type()) } - v.SetFloat(x) + v.SetFloat(f) case reflect.Ptr: x, ok := pValue.(*Key) if !ok && pValue != nil {