Merge pull request #25 from smallstep/Duration
make Duration wrapper publicly accessible
This commit is contained in:
commit
88a3c4cf83
4 changed files with 75 additions and 13 deletions
|
@ -26,9 +26,9 @@ var (
|
||||||
}
|
}
|
||||||
defaultDisableRenewal = false
|
defaultDisableRenewal = false
|
||||||
globalProvisionerClaims = ProvisionerClaims{
|
globalProvisionerClaims = ProvisionerClaims{
|
||||||
MinTLSDur: &duration{5 * time.Minute},
|
MinTLSDur: &Duration{5 * time.Minute},
|
||||||
MaxTLSDur: &duration{24 * time.Hour},
|
MaxTLSDur: &Duration{24 * time.Hour},
|
||||||
DefaultTLSDur: &duration{24 * time.Hour},
|
DefaultTLSDur: &Duration{24 * time.Hour},
|
||||||
DisableRenewal: &defaultDisableRenewal,
|
DisableRenewal: &defaultDisableRenewal,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,9 +12,9 @@ import (
|
||||||
// ProvisionerClaims so that individual provisioners can override global claims.
|
// ProvisionerClaims so that individual provisioners can override global claims.
|
||||||
type ProvisionerClaims struct {
|
type ProvisionerClaims struct {
|
||||||
globalClaims *ProvisionerClaims
|
globalClaims *ProvisionerClaims
|
||||||
MinTLSDur *duration `json:"minTLSCertDuration,omitempty"`
|
MinTLSDur *Duration `json:"minTLSCertDuration,omitempty"`
|
||||||
MaxTLSDur *duration `json:"maxTLSCertDuration,omitempty"`
|
MaxTLSDur *Duration `json:"maxTLSCertDuration,omitempty"`
|
||||||
DefaultTLSDur *duration `json:"defaultTLSCertDuration,omitempty"`
|
DefaultTLSDur *Duration `json:"defaultTLSCertDuration,omitempty"`
|
||||||
DisableRenewal *bool `json:"disableRenewal,omitempty"`
|
DisableRenewal *bool `json:"disableRenewal,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,8 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type duration struct {
|
// Duration is a wrapper around Time.Duration to aid with marshal/unmarshal.
|
||||||
|
type Duration struct {
|
||||||
time.Duration
|
time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,8 +17,8 @@ type duration struct {
|
||||||
// A duration string is a possibly signed sequence of decimal numbers, each with
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
||||||
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
||||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||||
func (d *duration) MarshalJSON() ([]byte, error) {
|
func (d *Duration) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(d.String())
|
return json.Marshal(d.Duration.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON parses a duration string and sets it to the duration.
|
// UnmarshalJSON parses a duration string and sets it to the duration.
|
||||||
|
@ -25,14 +26,21 @@ func (d *duration) MarshalJSON() ([]byte, error) {
|
||||||
// A duration string is a possibly signed sequence of decimal numbers, each with
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
||||||
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
||||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||||
func (d *duration) UnmarshalJSON(data []byte) (err error) {
|
func (d *Duration) UnmarshalJSON(data []byte) (err error) {
|
||||||
var s string
|
var (
|
||||||
|
s string
|
||||||
|
_d time.Duration
|
||||||
|
)
|
||||||
|
if d == nil {
|
||||||
|
return errors.New("duration cannot be nil")
|
||||||
|
}
|
||||||
if err = json.Unmarshal(data, &s); err != nil {
|
if err = json.Unmarshal(data, &s); err != nil {
|
||||||
return errors.Wrapf(err, "error unmarshalling %s", data)
|
return errors.Wrapf(err, "error unmarshalling %s", data)
|
||||||
}
|
}
|
||||||
if d.Duration, err = time.ParseDuration(s); err != nil {
|
if _d, err = time.ParseDuration(s); err != nil {
|
||||||
return errors.Wrapf(err, "error parsing %s as duration", s)
|
return errors.Wrapf(err, "error parsing %s as duration", s)
|
||||||
}
|
}
|
||||||
|
d.Duration = _d
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package authority
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_multiString_First(t *testing.T) {
|
func Test_multiString_First(t *testing.T) {
|
||||||
|
@ -71,7 +72,6 @@ func Test_multiString_MarshalJSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_multiString_UnmarshalJSON(t *testing.T) {
|
func Test_multiString_UnmarshalJSON(t *testing.T) {
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
data []byte
|
data []byte
|
||||||
}
|
}
|
||||||
|
@ -101,3 +101,57 @@ func Test_multiString_UnmarshalJSON(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDuration_UnmarshalJSON(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
data []byte
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
d *Duration
|
||||||
|
args args
|
||||||
|
want *Duration
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"empty", new(Duration), args{[]byte{}}, new(Duration), true},
|
||||||
|
{"bad type", new(Duration), args{[]byte(`15`)}, new(Duration), true},
|
||||||
|
{"empty string", new(Duration), args{[]byte(`""`)}, new(Duration), true},
|
||||||
|
{"non duration", new(Duration), args{[]byte(`"15"`)}, new(Duration), true},
|
||||||
|
{"duration", new(Duration), args{[]byte(`"15m30s"`)}, &Duration{15*time.Minute + 30*time.Second}, false},
|
||||||
|
{"nil", nil, args{nil}, nil, true},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if err := tt.d.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Duration.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(tt.d, tt.want) {
|
||||||
|
t.Errorf("Duration.UnmarshalJSON() = %v, want %v", tt.d, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_duration_MarshalJSON(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
d *Duration
|
||||||
|
want []byte
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"string", &Duration{15*time.Minute + 30*time.Second}, []byte(`"15m30s"`), false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := tt.d.MarshalJSON()
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Duration.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("Duration.MarshalJSON() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue