Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
elee1766 committed Dec 23, 2023
1 parent c91d72a commit 3fec555
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
8 changes: 4 additions & 4 deletions gtrsconvert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ type encodable struct {
Data int
}

func (e *encodable) UnmarshalText(text []byte) error {
func (e *encodable) UnmarshalBinary(text []byte) error {
e.Data = 5
return nil
}

func (e encodable) MarshalText() (text []byte, err error) {
func (e encodable) MarshalBinary() (text []byte, err error) {
return []byte("five"), nil
}

Expand Down Expand Up @@ -120,7 +120,7 @@ func TestUtils_convertMapToStruct_AllTypes(t *testing.T) {
F64 float64
N int
D time.Duration
T time.Time
T gtrsconvert.AsciiTime
M gtrsconvert.Metadata
E encodable
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestUtils_convertMapToStruct_AllTypes(t *testing.T) {
F32: 1.0,
F64: 1.0,
D: 3 * time.Second,
T: time.Date(2023, time.March, 29, 15, 25, 47, 89126000, time.UTC),
T: gtrsconvert.AsciiTime(time.Date(2023, time.March, 29, 15, 25, 47, 89126000, time.UTC)),
M: gtrsconvert.Metadata{"s": "string", "n": float64(1234)},
E: encodable{Data: 5},
}
Expand Down
39 changes: 39 additions & 0 deletions gtrsconvert/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gtrsconvert

import (
"encoding"
"time"
)

type AsciiTime time.Time

var _ encoding.BinaryMarshaler = (AsciiTime)(time.Time{})
var _ encoding.BinaryUnmarshaler = (*AsciiTime)(nil)

func (m *AsciiTime) UnmarshalBinary(text []byte) error {
// create m if nil
if m == nil {
*m = AsciiTime{}
}
// cast the type so it unmarshals correctly
var am *time.Time = (*time.Time)(m)
err := am.UnmarshalText(text)
if err != nil {
return err
}
// assign the value back
*m = (AsciiTime)(*am)
return nil
}

func (m AsciiTime) MarshalBinary() (text []byte, err error) {
// converts type so it marshals correctly
return time.Time(m).MarshalText()
}

func (m *AsciiTime) Time() time.Time {
if m == nil {
return time.Time{}
}
return time.Time(*m)
}
6 changes: 6 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import (
// to JSON can be inserted here.
type Metadata = gtrsconvert.Metadata

// AsciiTime is a type that wraps time.Time, however the (Un)[M/m]arshalbinary functions are overridden to marshal in the same format as Text
type AsciiTime = gtrsconvert.AsciiTime

// ConvertibleTo is implemented by types that can convert themselves to a map.
type ConvertibleTo = gtrsconvert.ConvertibleTo

// ConvertibleFrom is implemented by types that can load themselves from a map.
type ConvertibleFrom = gtrsconvert.ConvertibleFrom

// FieldParseError is returned with a field fails to be parsed
type FieldParseError = gtrsconvert.FieldParseError

// SerializeError is returned with a field fails to be serialized
type SerializeError = gtrsconvert.SerializeError

func copyMap[K comparable, V any](in map[K]V) map[K]V {
Expand Down

0 comments on commit 3fec555

Please sign in to comment.