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

add-marshal-unmarshal-text-param-for-common-date #164

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"context"

"github.com/DataDog/datadog-go/statsd"
)

Expand Down Expand Up @@ -33,7 +34,7 @@ func GetString(ctx context.Context, key ContextKey) (value string) {
func GetBasic(ctx context.Context, key string) (value interface{}) {
if ctx != nil {
if basics, ok := ctx.Value(ctxBasics).(Basics); ok {
value, _ = basics[key]
value = basics[key]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

}
}
return
Expand Down Expand Up @@ -90,7 +91,7 @@ func SetExtras(parent context.Context, extras Extras) context.Context {
func GetExtra(ctx context.Context, key string) (value interface{}) {
if ctx != nil {
if extras, ok := ctx.Value(ctxExtras).(Extras); ok {
value, _ = extras[key]
value = extras[key]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

}
}
return
Expand Down
23 changes: 22 additions & 1 deletion common/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Date time.Time

const layout = "2006-01-02"

// UnmarshalJSON Parses the json string in the Date
// UnmarshalJSON Parses the json string to the Date
func (d *Date) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), `"`)
nt, err := time.Parse(layout, s)
Expand All @@ -24,6 +24,27 @@ func (d Date) MarshalJSON() ([]byte, error) {
return []byte(d.quote()), nil
}

// UnmarshalText Parses the text string to the Date
func (d *Date) UnmarshalText(text []byte) (err error) {
s := strings.Trim(string(text), `"`)
parsedTime, err := time.Parse(layout, s)
*d = Date(parsedTime)
return
}

// MarshalText marshall Date into Text
func (d Date) MarshalText() ([]byte, error) {
return []byte(d.quote()), nil
}
Comment on lines +28 to +38
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if these are useful in the future as it is not used by gin
gin-gonic/gin#3933


// UnmarshalParam Parses the form's value to the Date
func (d *Date) UnmarshalParam(param string) (err error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to implement their BindUnmarshaler interface
gin-gonic/gin#3933

s := strings.Trim(param, `"`)
parsedTime, err := time.Parse(layout, s)
*d = Date(parsedTime)
return
}

// String returns the time in the custom format
func (d Date) String() string {
t := time.Time(d)
Expand Down
229 changes: 229 additions & 0 deletions common/date_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package common_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/wego/pkg/common"
)

func TestDate_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input []byte
expected common.Date
hasError bool
}{
{
name: "Invalid date format",
input: []byte(`"2020-30-02"`),
expected: common.Date{},
hasError: true,
},
{
name: "Empty date",
input: []byte(`""`),
expected: common.Date{},
hasError: true,
},
{
name: "Normal date without quotes",
input: []byte(`1999-12-31`),
expected: common.Date(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC)),
hasError: false,
},
{
name: "Normal date with quotes",
input: []byte(`"1999-12-31"`),
expected: common.Date(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC)),
hasError: false,
},
}

assert := assert.New(t)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var result common.Date
err := result.UnmarshalJSON(tc.input)

if tc.hasError {
assert.Error(err)
} else {
assert.NoError(err)
}
assert.Equal(tc.expected, result)
})
}
}

func TestDate_MarshallJson(t *testing.T) {
assert := assert.New(t)

date := common.Date(time.Date(1999, 12, 31, 11, 11, 11, 0, time.Local))
result, err := date.MarshalJSON()
assert.NoError(err)
assert.Equal([]byte(`"1999-12-31"`), result)
}

func TestDate_UnmarshalParam(t *testing.T) {
tests := []struct {
name string
input string
expected common.Date
hasError bool
}{
{
name: "Invalid date format",
input: `"2020-30-02"`,
expected: common.Date{},
hasError: true,
},
{
name: "Empty date",
input: `""`,
expected: common.Date{},
hasError: true,
},
{
name: "Normal date without quotes",
input: `1999-12-31`,
expected: common.Date(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC)),
hasError: false,
},
{
name: "Normal date with quotes",
input: `"1999-12-31"`,
expected: common.Date(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC)),
hasError: false,
},
}

assert := assert.New(t)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var result common.Date
err := result.UnmarshalParam(tc.input)

if tc.hasError {
assert.Error(err)
} else {
assert.NoError(err)
}
assert.Equal(tc.expected, result)
})
}
}

func TestDate_MarshallText(t *testing.T) {
assert := assert.New(t)

date := common.Date(time.Date(1999, 12, 31, 11, 11, 11, 0, time.Local))
result, err := date.MarshalText()
assert.NoError(err)
assert.Equal([]byte(`"1999-12-31"`), result)
}

func TestDate_UnmarshalText(t *testing.T) {
tests := []struct {
name string
input []byte
expected common.Date
hasError bool
}{
{
name: "Invalid date format",
input: []byte(`"2020-30-02"`),
expected: common.Date{},
hasError: true,
},
{
name: "Empty date",
input: []byte(`""`),
expected: common.Date{},
hasError: true,
},
{
name: "Normal date without quotes",
input: []byte(`1999-12-31`),
expected: common.Date(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC)),
hasError: false,
},
{
name: "Normal date with quotes",
input: []byte(`"1999-12-31"`),
expected: common.Date(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC)),
hasError: false,
},
}

assert := assert.New(t)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var result common.Date
err := result.UnmarshalText(tc.input)

if tc.hasError {
assert.Error(err)
} else {
assert.NoError(err)
}
assert.Equal(tc.expected, result)
})
}
}

func TestDate_String(t *testing.T) {
tests := []struct {
name string
date common.Date
expected string
}{
{
name: "single digit day",
date: common.Date(time.Date(2023, 10, 5, 11, 11, 11, 0, time.Local)),
expected: "2023-10-05",
},
{
name: "double digit month and day",
date: common.Date(time.Date(1999, 12, 31, 11, 11, 11, 0, time.Local)),
expected: "1999-12-31",
},
{
name: "single digit month and day",
date: common.Date(time.Date(2000, 1, 1, 11, 11, 11, 0, time.Local)),
expected: "2000-01-01",
},
}

assert := assert.New(t)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.date.String()
assert.Equal(tc.expected, result)
})
}
}

func TestDate_Before(t *testing.T) {
assert := assert.New(t)

now := time.Now()
assert.True(common.Date(now).Before(common.Date(now.Add(time.Hour))))
assert.False(common.Date(now.Add(time.Hour)).Before(common.Date(now)))
}

func TestDate_IsZero(t *testing.T) {
assert := assert.New(t)

assert.True(common.Date{}.IsZero())
assert.False(common.Date(time.Now()).IsZero())
}

func TestDate_Equal(t *testing.T) {
assert := assert.New(t)

now := time.Now()
assert.True(common.Date(now).Equal(now))
assert.False(common.Date(now).Equal(now.Add(time.Hour)))
}
Loading