forked from TrueCloudLab/certificates
Move Duration to a new file and move tests to provisioner package.
This commit is contained in:
parent
c24d868d9d
commit
a8d03c39bb
3 changed files with 0 additions and 131 deletions
|
@ -1,7 +1,6 @@
|
||||||
package provisioner
|
package provisioner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -90,40 +89,3 @@ func (pc *Claims) Validate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duration is a wrapper around Time.Duration to aid with marshal/unmarshal.
|
|
||||||
type Duration struct {
|
|
||||||
time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON parses a duration string and sets it to the duration.
|
|
||||||
//
|
|
||||||
// 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".
|
|
||||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
||||||
func (d *Duration) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(d.Duration.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON parses a duration string and sets it to the duration.
|
|
||||||
//
|
|
||||||
// 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".
|
|
||||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
||||||
func (d *Duration) UnmarshalJSON(data []byte) (err error) {
|
|
||||||
var (
|
|
||||||
s string
|
|
||||||
_d time.Duration
|
|
||||||
)
|
|
||||||
if d == nil {
|
|
||||||
return errors.New("duration cannot be nil")
|
|
||||||
}
|
|
||||||
if err = json.Unmarshal(data, &s); err != nil {
|
|
||||||
return errors.Wrapf(err, "error unmarshaling %s", data)
|
|
||||||
}
|
|
||||||
if _d, err = time.ParseDuration(s); err != nil {
|
|
||||||
return errors.Wrapf(err, "error parsing %s as duration", s)
|
|
||||||
}
|
|
||||||
d.Duration = _d
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,48 +2,10 @@ package authority
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Duration is a wrapper around Time.Duration to aid with marshal/unmarshal.
|
|
||||||
type Duration struct {
|
|
||||||
time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON parses a duration string and sets it to the duration.
|
|
||||||
//
|
|
||||||
// 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".
|
|
||||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
||||||
func (d *Duration) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(d.Duration.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON parses a duration string and sets it to the duration.
|
|
||||||
//
|
|
||||||
// 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".
|
|
||||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
||||||
func (d *Duration) UnmarshalJSON(data []byte) (err error) {
|
|
||||||
var (
|
|
||||||
s string
|
|
||||||
_d time.Duration
|
|
||||||
)
|
|
||||||
if d == nil {
|
|
||||||
return errors.New("duration cannot be nil")
|
|
||||||
}
|
|
||||||
if err = json.Unmarshal(data, &s); err != nil {
|
|
||||||
return errors.Wrapf(err, "error unmarshalling %s", data)
|
|
||||||
}
|
|
||||||
if _d, err = time.ParseDuration(s); err != nil {
|
|
||||||
return errors.Wrapf(err, "error parsing %s as duration", s)
|
|
||||||
}
|
|
||||||
d.Duration = _d
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// multiString represents a type that can be encoded/decoded in JSON as a single
|
// multiString represents a type that can be encoded/decoded in JSON as a single
|
||||||
// string or an array of strings.
|
// string or an array of strings.
|
||||||
type multiString []string
|
type multiString []string
|
||||||
|
|
|
@ -3,7 +3,6 @@ package authority
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_multiString_First(t *testing.T) {
|
func Test_multiString_First(t *testing.T) {
|
||||||
|
@ -101,57 +100,3 @@ 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