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

feat(datastore): support civil package types save #3202

Merged
merged 8 commits into from Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions datastore/save.go
Expand Up @@ -21,6 +21,7 @@ import (
"time"
"unicode/utf8"

"cloud.google.com/go/civil"
timepb "github.com/golang/protobuf/ptypes/timestamp"
pb "google.golang.org/genproto/googleapis/datastore/v1"
llpb "google.golang.org/genproto/googleapis/type/latlng"
Expand Down Expand Up @@ -53,6 +54,32 @@ func reflectFieldSave(props *[]Property, p Property, name string, opts saveOpts,
switch x := v.Interface().(type) {
case *Key, time.Time, GeoPoint:
p.Value = x
case civil.Date, civil.DateTime, civil.Time:
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
if dateVal, ok := v.Interface().(civil.Date); ok {
p.Value = dateVal.In(time.Now().Location())
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
*props = append(*props, p)
return nil
}
if timeVal, ok := v.Interface().(civil.Time); ok {
var format string
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
if timeVal.Nanosecond == 0 {
format = "15:04:05"
} else {
format = "15:04:05.000000000"
}
val, err := time.Parse(format, timeVal.String())
if err != nil {
return fmt.Errorf("datastore: error while parsing time: %v", err)
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
}
p.Value = val
*props = append(*props, p)
return nil
}
if dateTimeVal, ok := v.Interface().(civil.DateTime); ok {
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
p.Value = dateTimeVal.In(time.Now().Location())
*props = append(*props, p)
return nil
}
default:
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
Expand Down
83 changes: 83 additions & 0 deletions datastore/save_test.go
Expand Up @@ -15,9 +15,11 @@
package datastore

import (
"fmt"
"testing"
"time"

"cloud.google.com/go/civil"
"cloud.google.com/go/internal/testutil"
pb "google.golang.org/genproto/googleapis/datastore/v1"
)
Expand Down Expand Up @@ -321,6 +323,28 @@ func TestSaveFieldsWithInterface(t *testing.T) {
N2 interface{}
}

civDateVal := civil.Date{
Year: 2020,
Month: 11,
Day: 10,
}
civTimeValNano := civil.Time{
Hour: 1,
Minute: 1,
Second: 1,
Nanosecond: 1,
}
civTimeVal := civil.Time{
Hour: 1,
Minute: 1,
Second: 1,
}
timeValNano, _ := time.Parse("15:04:05.000000000", civTimeValNano.String())
timeVal, _ := time.Parse("15:04:05", civTimeVal.String())
loc := time.Now().Location()
dateTimeStr := fmt.Sprintf("%v %v", civDateVal.String(), civTimeVal.String())
dateTimeVal, _ := time.ParseInLocation("2006-01-02 15:04:05", dateTimeStr, loc)

cases := []struct {
name string
in interface{}
Expand Down Expand Up @@ -371,6 +395,65 @@ func TestSaveFieldsWithInterface(t *testing.T) {
{Name: "Map", Value: []Property{}},
},
},
{
name: "civil.Date",
in: &struct {
CivDate civil.Date
}{
CivDate: civDateVal,
},
want: []Property{
{
Name: "CivDate",
Value: civDateVal.In(time.Now().Location()),
},
},
},
{
name: "civil.Time-nano",
in: &struct {
CivDate civil.Time
}{
CivDate: civTimeValNano,
},
want: []Property{
{
Name: "CivDate",
Value: timeValNano,
},
},
},
{
name: "civil.Time",
in: &struct {
CivDate civil.Time
}{
CivDate: civTimeVal,
},
want: []Property{
{
Name: "CivDate",
Value: timeVal,
},
},
},
{
name: "civil.DateTime",
in: &struct {
CivDateTime civil.DateTime
}{
CivDateTime: civil.DateTime{
Date: civDateVal,
Time: civTimeVal,
},
},
want: []Property{
{
Name: "CivDateTime",
Value: dateTimeVal,
},
},
},
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
{
name: "Nested",
in: &n3{
Expand Down