Skip to content

Commit

Permalink
Time formats bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnoldas Grigutis committed Mar 8, 2023
1 parent e6895a4 commit 76c624e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
21 changes: 15 additions & 6 deletions time_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ type OpenSearchDateType interface {
GetOpenSearchFieldType() string
}

func (t *TimeBasicDateTime) MarshalText() ([]byte, error) {
return []byte(time.Time(*t).Format(FormatTimeBasicDateTime)), nil
//goland:noinspection GoMixedReceiverTypes
func (t TimeBasicDateTime) MarshalText() ([]byte, error) {
return []byte(time.Time(t).Format(FormatTimeBasicDateTime)), nil
}

//goland:noinspection GoMixedReceiverTypes
func (t *TimeBasicDateTime) UnmarshalText(text []byte) error {
parsedTime, err := time.Parse(FormatTimeBasicDateTime, string(text))
if err != nil {
Expand All @@ -40,16 +42,19 @@ func (t *TimeBasicDateTime) UnmarshalText(text []byte) error {
return nil
}

//goland:noinspection GoMixedReceiverTypes
func (t TimeBasicDateTime) GetOpenSearchFieldType() string {
return "basic_date_time"
}

// TimeBasicDateTimeNoMillis

func (t *TimeBasicDateTimeNoMillis) MarshalText() ([]byte, error) {
return []byte(time.Time(*t).Format(FormatTimeBasicDateTimeNoMillis)), nil
//goland:noinspection GoMixedReceiverTypes
func (t TimeBasicDateTimeNoMillis) MarshalText() ([]byte, error) {
return []byte(time.Time(t).Format(FormatTimeBasicDateTimeNoMillis)), nil
}

//goland:noinspection GoMixedReceiverTypes
func (t *TimeBasicDateTimeNoMillis) UnmarshalText(text []byte) error {
parsedTime, err := time.Parse(FormatTimeBasicDateTimeNoMillis, string(text))
if err != nil {
Expand All @@ -59,16 +64,19 @@ func (t *TimeBasicDateTimeNoMillis) UnmarshalText(text []byte) error {
return nil
}

//goland:noinspection GoMixedReceiverTypes
func (t TimeBasicDateTimeNoMillis) GetOpenSearchFieldType() string {
return "basic_date_time_no_millis"
}

// TimeBasicDate

func (t *TimeBasicDate) MarshalText() ([]byte, error) {
return []byte(time.Time(*t).Format(FormatTimeBasicDate)), nil
//goland:noinspection GoMixedReceiverTypes
func (t TimeBasicDate) MarshalText() ([]byte, error) {
return []byte(time.Time(t).Format(FormatTimeBasicDate)), nil
}

//goland:noinspection GoMixedReceiverTypes
func (t *TimeBasicDate) UnmarshalText(text []byte) error {
parsedTime, err := time.Parse(FormatTimeBasicDate, string(text))
if err != nil {
Expand All @@ -78,6 +86,7 @@ func (t *TimeBasicDate) UnmarshalText(text []byte) error {
return nil
}

//goland:noinspection GoMixedReceiverTypes
func (t TimeBasicDate) GetOpenSearchFieldType() string {
return "basic_date"
}
25 changes: 25 additions & 0 deletions time_formats_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package opensearchutil

import (
"encoding/json"
. "github.com/onsi/gomega"
"testing"
"time"
Expand Down Expand Up @@ -62,3 +63,27 @@ func TestTimeBasicDate_UnmarshalText(t *testing.T) {
g.Expect(obj.UnmarshalText([]byte("20190323"))).To(Succeed())
g.Expect(time.Time(obj).Equal(time.Date(2019, 3, 23, 0, 0, 0, 0, time.UTC))).To(BeTrue())
}

func TestTimeFormat_marshallingIntoJson(t *testing.T) {
g := NewGomegaWithT(t)

type foo struct {
A TimeBasicDateTime
B TimeBasicDateTimeNoMillis
C TimeBasicDate
}

someTime := time.Date(2019, 3, 23, 21, 34, 46, 567000000, time.UTC)
jsonBytes, err := json.Marshal(foo{
A: TimeBasicDateTime(someTime),
B: TimeBasicDateTimeNoMillis(someTime),
C: TimeBasicDate(someTime),
})

var fooUnmarshalled foo
err = json.Unmarshal(jsonBytes, &fooUnmarshalled)
g.Expect(err).To(BeNil())
g.Expect(time.Time(fooUnmarshalled.A).Equal(time.Date(2019, 3, 23, 21, 34, 46, 567000000, time.UTC))).To(BeTrue())
g.Expect(time.Time(fooUnmarshalled.B).Equal(time.Date(2019, 3, 23, 21, 34, 46, 0, time.UTC))).To(BeTrue())
g.Expect(time.Time(fooUnmarshalled.C).Equal(time.Date(2019, 3, 23, 0, 0, 0, 0, time.UTC))).To(BeTrue())
}

0 comments on commit 76c624e

Please sign in to comment.