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

Firestore/encoding text #2481

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions firestore/from_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package firestore

import (
"encoding"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -101,6 +102,18 @@ func setReflectFromProtoValue(v reflect.Value, vproto *pb.Value, c *Client) erro
return nil
}

// Check if type implement encoding
if v.CanAddr() {
switch e := v.Addr().Interface().(type) {
case encoding.TextUnmarshaler:
x, ok := val.(*pb.Value_StringValue)
if !ok {
return typeErr()
}
return e.UnmarshalText([]byte(x.StringValue))
}
}

switch v.Kind() {
case reflect.Bool:
x, ok := val.(*pb.Value_BooleanValue)
Expand Down
18 changes: 13 additions & 5 deletions firestore/to_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package firestore

import (
"encoding"
"errors"
"fmt"
"reflect"
Expand All @@ -30,11 +31,12 @@ import (
var nullValue = &pb.Value{ValueType: &pb.Value_NullValue{}}

var (
typeOfByteSlice = reflect.TypeOf([]byte{})
typeOfGoTime = reflect.TypeOf(time.Time{})
typeOfLatLng = reflect.TypeOf((*latlng.LatLng)(nil))
typeOfDocumentRef = reflect.TypeOf((*DocumentRef)(nil))
typeOfProtoTimestamp = reflect.TypeOf((*ts.Timestamp)(nil))
typeOfByteSlice = reflect.TypeOf([]byte{})
typeOfGoTime = reflect.TypeOf(time.Time{})
typeOfLatLng = reflect.TypeOf((*latlng.LatLng)(nil))
typeOfDocumentRef = reflect.TypeOf((*DocumentRef)(nil))
typeOfProtoTimestamp = reflect.TypeOf((*ts.Timestamp)(nil))
typeOfTextUnmarshaler = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
)

// toProtoValue converts a Go value to a Firestore Value protobuf.
Expand Down Expand Up @@ -91,6 +93,12 @@ func toProtoValue(v reflect.Value) (pbv *pb.Value, sawTransform bool, err error)
// types whose underlying types are those primitives.
// E.g. Given "type mybool bool", an ordinary type switch on bool will
// not catch a mybool, but the reflect.Kind of a mybool is reflect.Bool.
case encoding.TextMarshaler:
text, err := x.MarshalText()
if err != nil {
return nil, false, err
}
return &pb.Value{ValueType: &pb.Value_StringValue{string(text)}}, false, nil
}
switch v.Kind() {
case reflect.Bool:
Expand Down
13 changes: 13 additions & 0 deletions firestore/to_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func TestToProtoValue_Conversions(t *testing.T) {
in: [1]int{7},
want: arrayval(intval(7)),
},
{
desc: "encodingText",
in: encodingText{},
want: strval("encodingText"),
},
{
desc: "pointer to docref",
in: &DocumentRef{
Expand Down Expand Up @@ -312,6 +317,14 @@ type stringy struct{}

func (stringy) String() string { return "stringy" }

type encodingText struct {
text string
}

func (_ encodingText) MarshalText() ([]byte, error) {
return []byte(`encodingText`), nil
}

func TestToProtoValue_Errors(t *testing.T) {
for _, in := range []interface{}{
uint64(0), // a bad fit for int64
Expand Down