-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add config structs json/yaml conversion support
- Loading branch information
Showing
5 changed files
with
182 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// Duration is a replacement of time.Duration that supports JSON | ||
type Duration time.Duration | ||
|
||
// MarshalJSON ... | ||
func (d *Duration) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(time.Duration(*d).String()) | ||
} | ||
|
||
// UnmarshalJSON ... | ||
func (d *Duration) UnmarshalJSON(b []byte) error { | ||
var v interface{} | ||
if err := json.Unmarshal(b, &v); err != nil { | ||
return xerrors.Errorf("failed to parse '%s' to time.Duration: %w", string(b), err) | ||
} | ||
switch value := v.(type) { | ||
case float64: | ||
*d = Duration(time.Duration(value)) | ||
return nil | ||
case string: | ||
tmp, err := time.ParseDuration(value) | ||
if err != nil { | ||
return xerrors.Errorf("failed to parse '%s' to time.Duration: %w", string(b), err) | ||
} | ||
*d = Duration(tmp) | ||
return nil | ||
default: | ||
return xerrors.Errorf("failed to parse '%s' to time.Duration", string(b)) | ||
} | ||
} | ||
|
||
// bug in YAMLv2, fixed in YAMLv3 | ||
// // MarshalYAML ... | ||
// func (d *Duration) MarshalYAML() (interface{}, error) { | ||
// return time.Duration(*d).String(), nil | ||
// } | ||
|
||
// UnmarshalYAML ... | ||
func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var tm string | ||
if err := unmarshal(&tm); err != nil { | ||
return xerrors.Errorf("failed to parse '%s' to time.Duration: %w", tm, err) | ||
} | ||
|
||
lastChar := byte(tm[len(tm)-1]) | ||
if lastChar >= '0' && lastChar <= '9' { | ||
// ends with number, no units | ||
tm = tm + "ns" | ||
} | ||
|
||
td, err := time.ParseDuration(tm) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse '%s' to time.Duration: %w", tm, err) | ||
} | ||
|
||
*d = Duration(td) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package testcases | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cyverse/go-irodsclient/irods/types" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestDuration(t *testing.T) { | ||
t.Run("test YAMLMarshal", testYAMLMarshal) | ||
t.Run("test YAMLUnmarshalFromStringWithoutUnits", testYAMLUnmarshalFromStringWithoutUnits) | ||
t.Run("test YAMLUnmarshalFromString", testYAMLUnmarshalFromString) | ||
} | ||
|
||
func testYAMLMarshal(t *testing.T) { | ||
d1 := types.Duration(5 * time.Minute) | ||
|
||
yamlBytes, err := yaml.Marshal(d1) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, string(yamlBytes)) | ||
|
||
var d2 types.Duration | ||
err = yaml.Unmarshal(yamlBytes, &d2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, d1, d2) | ||
} | ||
|
||
func testYAMLUnmarshalFromStringWithoutUnits(t *testing.T) { | ||
v1 := []byte("60000000000") | ||
|
||
var d1 types.Duration | ||
err := yaml.Unmarshal(v1, &d1) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1*time.Minute, time.Duration(d1)) | ||
|
||
v2 := []byte("6h60000000000") | ||
|
||
var d2 types.Duration | ||
err = yaml.Unmarshal(v2, &d2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 6*time.Hour+1*time.Minute, time.Duration(d2)) | ||
} | ||
|
||
func testYAMLUnmarshalFromString(t *testing.T) { | ||
v1 := []byte("6m") | ||
|
||
var d1 types.Duration | ||
err := yaml.Unmarshal(v1, &d1) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 6*time.Minute, time.Duration(d1)) | ||
|
||
v2 := []byte("6h6m") | ||
|
||
var d2 types.Duration | ||
err = yaml.Unmarshal(v2, &d2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 6*time.Hour+6*time.Minute, time.Duration(d2)) | ||
} |