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

Extract core/store/models #296

Merged
merged 16 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
75 changes: 57 additions & 18 deletions pkg/config/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,51 @@ func MustNewDuration(d time.Duration) *Duration {
return &rv
}

func MakeDuration(d time.Duration) (Duration, error) {
if d < time.Duration(0) {
return Duration{}, fmt.Errorf("cannot make negative time duration: %s", d)
}
return Duration{d: d}, nil
}
DylanTinianov marked this conversation as resolved.
Show resolved Hide resolved

func ParseDuration(s string) (Duration, error) {
d, err := time.ParseDuration(s)
if err != nil {
return Duration{}, err
}

return MakeDuration(d)
}

func MustMakeDuration(d time.Duration) Duration {
rv, err := MakeDuration(d)
if err != nil {
panic(err)
}
return rv
}
DylanTinianov marked this conversation as resolved.
Show resolved Hide resolved

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

// Before returns the time d units before time t
func (d Duration) Before(t time.Time) time.Time {
return t.Add(-d.Duration())
}

// Shorter returns true if and only if d is shorter than od.
func (d Duration) Shorter(od Duration) bool { return d.d < od.d }

// IsInstant is true if and only if d is of duration 0
func (d Duration) IsInstant() bool { return d.d == 0 }

// String returns a string representing the duration in the form "72h3m0.5s".
// Leading zero units are omitted. As a special case, durations less than one
// second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
// that the leading digit is non-zero. The zero duration formats as 0s.
func (d Duration) String() string {
return d.d.String()
return d.Duration().String()
}

// MarshalJSON implements the json.Marshaler interface.
Expand All @@ -51,13 +90,28 @@ func (d *Duration) UnmarshalJSON(input []byte) error {
if err != nil {
return err
}
*d, err = NewDuration(v)
*d, err = MakeDuration(v)
if err != nil {
return err
}
return nil
}

func (d *Duration) Scan(v interface{}) (err error) {
switch tv := v.(type) {
case int64:
*d, err = MakeDuration(time.Duration(tv))
return err
default:
return errors.Errorf(`don't know how to parse "%s" of type %T as a `+
`models.Duration`, tv, tv)
}
}

func (d Duration) Value() (driver.Value, error) {
return int64(d.d), nil
}

// MarshalText implements the text.Marshaler interface.
func (d Duration) MarshalText() ([]byte, error) {
return []byte(d.d.String()), nil
Expand All @@ -69,25 +123,10 @@ func (d *Duration) UnmarshalText(input []byte) error {
if err != nil {
return err
}
pd, err := NewDuration(v)
pd, err := MakeDuration(v)
if err != nil {
return err
}
*d = pd
return nil
}

func (d *Duration) Scan(v interface{}) (err error) {
switch tv := v.(type) {
case int64:
*d, err = NewDuration(time.Duration(tv))
return err
default:
return errors.Errorf(`don't know how to parse "%s" of type %T as a `+
`models.Duration`, tv, tv)
}
}

func (d Duration) Value() (driver.Value, error) {
return int64(d.d), nil
}
76 changes: 76 additions & 0 deletions pkg/config/duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package config

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDuration_MarshalJSON(t *testing.T) {
tests := []struct {
name string
input Duration
want string
}{
{"zero", MustMakeDuration(0), `"0s"`},
{"one second", MustMakeDuration(time.Second), `"1s"`},
{"one minute", MustMakeDuration(time.Minute), `"1m0s"`},
{"one hour", MustMakeDuration(time.Hour), `"1h0m0s"`},
{"one hour thirty minutes", MustMakeDuration(time.Hour + 30*time.Minute), `"1h30m0s"`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b, err := json.Marshal(test.input)
assert.NoError(t, err)
assert.Equal(t, test.want, string(b))
})
}
}

func TestDuration_Scan_Value(t *testing.T) {
t.Parallel()

d := MustMakeDuration(100)
require.NotNil(t, d)

d.Duration()
DylanTinianov marked this conversation as resolved.
Show resolved Hide resolved

val, err := d.Value()
require.NoError(t, err)

dNew := MustMakeDuration(0)
err = dNew.Scan(val)
require.NoError(t, err)

require.Equal(t, d, dNew)
}

func TestDuration_MarshalJSON_UnmarshalJSON(t *testing.T) {
t.Parallel()

d := MustMakeDuration(100)
require.NotNil(t, d)

json, err := d.MarshalJSON()
require.NoError(t, err)

dNew := MustMakeDuration(0)
err = dNew.UnmarshalJSON(json)
require.NoError(t, err)

require.Equal(t, d, dNew)
}

func TestDuration_MakeDurationFromString(t *testing.T) {
t.Parallel()

d, err := ParseDuration("1s")
require.NoError(t, err)
require.Equal(t, 1*time.Second, d.Duration())

_, err = ParseDuration("xyz")
require.Error(t, err)
}
24 changes: 23 additions & 1 deletion pkg/config/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,30 @@ func MustParseURL(s string) *URL {
return u
}

func (u *URL) String() string {
return (*url.URL)(u).String()
}

// URL returns a copy of u as a *url.URL
func (u *URL) URL() *url.URL {
if u == nil {
return nil
}
// defensive copy
r := url.URL(*u)
if u.User != nil {
r.User = new(url.Userinfo)
*r.User = *u.User
}
return &r
}

func (u *URL) IsZero() bool {
return (url.URL)(*u) == url.URL{}
}

func (u *URL) MarshalText() ([]byte, error) {
return []byte((*url.URL)(u).String()), nil
return []byte(u.String()), nil
}

func (u *URL) UnmarshalText(input []byte) error {
Expand Down
Loading