Skip to content

Commit

Permalink
Merge pull request #15 from strvcom/chore/SGO-100-duration-type
Browse files Browse the repository at this point in the history
chore: change Duration inner type
  • Loading branch information
TomasKocman committed Jan 9, 2023
2 parents 8b3198b + d8e725a commit 501c8ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ How to release a new version:

## [Unreleased]

## [0.2.0] - 2023-01-06
### Changed
- Type `Duration` is derived directly from the `time.Duration`.

## [0.1.0] - 2022-08-01
### Added
- Added Changelog.

[Unreleased]: https://github.com/strvcom/strv-backend-go-time/compare/v0.1.0...HEAD
[Unreleased]: https://github.com/strvcom/strv-backend-go-time/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/strvcom/strv-backend-go-time/releases/tag/v0.2.0
[0.1.0]: https://github.com/strvcom/strv-backend-go-time/releases/tag/v0.1.0

20 changes: 11 additions & 9 deletions duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,26 @@ var (
daysRegex = regexp.MustCompile(`^(\d+)d\w*$`)
)

type Duration struct {
time.Duration
type Duration time.Duration

func (d Duration) Duration() time.Duration {
return time.Duration(d)
}

func (d Duration) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
return []byte(time.Duration(d).String()), nil
}

func (d *Duration) UnmarshalText(b []byte) error {
return d.unmarshalText(string(b))
}

func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
return json.Marshal(time.Duration(d).String())
}

func (d *Duration) UnmarshalJSON(b []byte) error {
var unmarshalledJSON interface{}
var unmarshalledJSON any
decoder := json.NewDecoder(bytes.NewReader(b))
decoder.UseNumber()
if err := decoder.Decode(&unmarshalledJSON); err != nil {
Expand All @@ -68,7 +70,7 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}
d.Duration = time.Duration(dur)
*d = Duration(dur)
case string:
return d.unmarshalText(t)
default:
Expand All @@ -85,7 +87,7 @@ func (d *Duration) unmarshalText(text string) error {
if err != nil {
return err
}
d.Duration = v
*d = Duration(v)
return nil
}

Expand All @@ -94,7 +96,7 @@ func (d *Duration) unmarshalText(text string) error {
if err != nil {
return fmt.Errorf("invalid duration: %w", err)
}
d.Duration = Day * time.Duration(i)
*d = Duration(Day * time.Duration(i))

// Remove days from text and continue the typical way.
rest := strings.TrimPrefix(text, days[0][1]+"d")
Expand All @@ -106,7 +108,7 @@ func (d *Duration) unmarshalText(text string) error {
if err != nil {
return err
}
d.Duration += v
*d += Duration(v)

return nil
}
12 changes: 6 additions & 6 deletions duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestDuration_MarshalText(t *testing.T) {
d := Duration{Duration: time.Hour * 3}
d := Duration(time.Hour * 3)
expected := []byte("3h0m0s")

data, err := d.MarshalText()
Expand Down Expand Up @@ -57,18 +57,18 @@ func TestDuration_UnmarshalText(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := Duration{}
d := Duration(0)
err := d.UnmarshalText([]byte(tt.text))
if tt.expectedErr != "" {
assert.Equal(t, tt.expectedErr, err.Error())
}
assert.Equal(t, tt.expected, d.Duration)
assert.Equal(t, tt.expected, d.Duration())
})
}
}

func TestDuration_MarshalJSON(t *testing.T) {
d := Duration{Duration: time.Hour * 3}
d := Duration(time.Hour * 3)
expected := []byte(`"3h0m0s"`)

data, err := d.MarshalJSON()
Expand Down Expand Up @@ -141,12 +141,12 @@ func TestDuration_UnmarshalJSON(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := Duration{}
d := Duration(0)
err := d.UnmarshalJSON([]byte(tt.text))
if tt.expectedErr != "" {
assert.Equal(t, tt.expectedErr, err.Error())
}
assert.Equal(t, tt.expected, d.Duration)
assert.Equal(t, tt.expected, d.Duration())
})
}
}

0 comments on commit 501c8ae

Please sign in to comment.