Skip to content

Commit

Permalink
Merge pull request #176 from mailgun/cclark/clock
Browse files Browse the repository at this point in the history
add support for marshal/unmarshal gopkg.in/yaml marshaler
  • Loading branch information
Takumi2008 authored Jul 18, 2023
2 parents fa93174 + 79e6aad commit 1230bd9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
13 changes: 13 additions & 0 deletions clock/rfc822.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ func (t RFC822Time) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(t.Format(RFC1123))), nil
}

func (t RFC822Time) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}

func (t *RFC822Time) UnmarshalText(s []byte) error {
parsed, err := ParseRFC822Time(string(s))
if err != nil {
return err
}
t.Time = parsed
return nil
}

func (t *RFC822Time) UnmarshalJSON(s []byte) error {
q, err := strconv.Unquote(string(s))
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion clock/rfc822_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"time"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

type testStruct struct {
Time RFC822Time `json:"ts"`
Time RFC822Time `json:"ts" yaml:"ts"`
}

func TestRFC822New(t *testing.T) {
Expand Down Expand Up @@ -42,6 +43,19 @@ func TestRFC822SecondPrecision(t *testing.T) {
"want=%s, got=%s", rfc822Time1.Time, rfc822Time2.Time)
}

func TestRFC822YAMLMarshaler(t *testing.T) {
rfcTime := NewRFC822Time(Date(1955, November, 12, 6, 38, 0, 0, UTC))
ts := testStruct{Time: rfcTime}
encoded, err := yaml.Marshal(ts)
assert.NoError(t, err)
assert.Equal(t, "ts: Sat, 12 Nov 1955 06:38:00 UTC\n", string(encoded))

var decoded testStruct
err = yaml.Unmarshal(encoded, &decoded)
assert.NoError(t, err)
assert.Equal(t, rfcTime, decoded.Time)
}

// Marshaled representation is truncated down to second precision.
func TestRFC822Marshaling(t *testing.T) {
stdTime, err := Parse(RFC3339Nano, "2019-08-29T11:20:07.123456789+03:30")
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ services:
- 22379:22379

consul-agent:
image: consul:latest
image: hashicorp/consul:latest
command: "agent -retry-join consul-server-bootstrap -client 0.0.0.0"
volumes:
- ${PWD}/consul/config:/consul/config

consul-server-bootstrap:
image: consul:latest
image: hashicorp/consul:latest
ports:
- "8400:8400"
- "8500:8500"
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
go.opentelemetry.io/otel/trace v1.16.0
golang.org/x/net v0.8.0
google.golang.org/grpc v1.55.0
sigs.k8s.io/yaml v1.3.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down Expand Up @@ -83,6 +83,4 @@ require (
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -780,5 +780,3 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
2 changes: 1 addition & 1 deletion mongoutil/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/mailgun/holster/v4/mongoutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
"gopkg.in/yaml.v3"
)

func TestMongoConfig_URIWithOptions(t *testing.T) {
Expand Down

0 comments on commit 1230bd9

Please sign in to comment.