Skip to content

Commit 38926db

Browse files
authored
add feature parse date time string (#13)
1 parent 6961586 commit 38926db

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

defaults.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func newDefaultFiller() *Filler {
7878
funcs[reflect.Uint64] = funcs[reflect.Uint]
7979

8080
funcs[reflect.String] = func(field *FieldData) {
81-
field.Value.SetString(field.TagValue)
81+
tagValue := parseDateTimeString(field.TagValue)
82+
field.Value.SetString(tagValue)
8283
}
8384

8485
funcs[reflect.Struct] = func(field *FieldData) {
@@ -135,3 +136,40 @@ func newDefaultFiller() *Filler {
135136

136137
return &Filler{FuncByKind: funcs, FuncByType: types, Tag: "default"}
137138
}
139+
140+
func parseDateTimeString(data string) string {
141+
142+
pattern := regexp.MustCompile(`\{\{(\w+\:(?:-|)\d*,(?:-|)\d*,(?:-|)\d*)\}\}`)
143+
matches := pattern.FindAllStringSubmatch(data, -1) // matches is [][]string
144+
for _, match := range matches {
145+
146+
tags := strings.Split(match[1], ":")
147+
if len(tags) == 2 {
148+
149+
valueStrings := strings.Split(tags[1], ",")
150+
if len(valueStrings) == 3 {
151+
var values [3]int
152+
for key, valueString := range valueStrings {
153+
num, _ := strconv.ParseInt(valueString, 10, 64)
154+
values[key] = int(num)
155+
}
156+
157+
switch tags[0] {
158+
159+
case "date":
160+
str := time.Now().AddDate(values[0], values[1], values[2]).Format("2006-01-02")
161+
data = strings.Replace(data, match[0], str, -1)
162+
break
163+
case "time":
164+
str := time.Now().Add((time.Duration(values[0]) * time.Hour) +
165+
(time.Duration(values[1]) * time.Minute) +
166+
(time.Duration(values[2]) * time.Second)).Format("15:04:05")
167+
data = strings.Replace(data, match[0], str, -1)
168+
break
169+
}
170+
}
171+
}
172+
173+
}
174+
return data
175+
}

defaults_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import (
44
"testing"
55
"time"
66

7+
"bou.ke/monkey"
78
. "gopkg.in/check.v1"
89
)
910

1011
// Hook up gocheck into the "go test" runner.
11-
func Test(t *testing.T) { TestingT(t) }
12+
13+
func Test(t *testing.T) {
14+
monkey.Patch(time.Now, func() time.Time {
15+
t, _ := time.Parse("2006-01-02 15:04:05", "2020-06-10 12:00:00")
16+
return t
17+
})
18+
19+
TestingT(t)
20+
}
1221

1322
type DefaultsSuite struct{}
1423

@@ -50,6 +59,8 @@ type ExampleBasic struct {
5059
IntSlice []int `default:"[1,2,3,4]"`
5160
IntSliceSlice [][]int `default:"[[1],[2],[3],[4]]"`
5261
StringSliceSlice [][]string `default:"[[1],[]]"`
62+
63+
DateTime string `default:"{{date:1,-10,0}} {{time:1,-5,10}}"`
5364
}
5465

5566
func (s *DefaultsSuite) TestSetDefaultsBasic(c *C) {
@@ -94,6 +105,7 @@ func (s *DefaultsSuite) assertTypes(c *C, foo *ExampleBasic) {
94105
c.Assert(foo.IntSlice, DeepEquals, []int{1, 2, 3, 4})
95106
c.Assert(foo.IntSliceSlice, DeepEquals, [][]int{[]int{1}, []int{2}, []int{3}, []int{4}})
96107
c.Assert(foo.StringSliceSlice, DeepEquals, [][]string{[]string{"1"}, []string{}})
108+
c.Assert(foo.DateTime, Equals, "2020-08-10 12:55:10")
97109
}
98110

99111
func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/mcuadros/go-defaults
33
go 1.14
44

55
require (
6+
bou.ke/monkey v1.0.2 // indirect
67
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
78
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
2+
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
13
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
24
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
35
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=

0 commit comments

Comments
 (0)