Skip to content

Commit 9e66efe

Browse files
authored
Add yaml Marshal for xtime.Duration (#136)
1 parent e86abe3 commit 9e66efe

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

ldap/validation_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ func TestConfigValidator(t *testing.T) {
267267
expectedGroups := set.CreateStringSet(
268268
"cn=projecta,ou=groups,ou=swengg,dc=min,dc=io",
269269
"cn=projectb,ou=groups,ou=swengg,dc=min,dc=io",
270+
"cn=project/d,ou=groups,ou=swengg,dc=min,dc=io",
270271
)
271272

272273
for i, test := range testCases {

xtime/time.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,8 @@ func (d *Duration) DecodeMsg(reader *msgp.Reader) error {
122122
func (d Duration) Msgsize() int {
123123
return msgp.Int64Size
124124
}
125+
126+
// MarshalYAML implements yaml.Marshaler - Converts duration to human-readable format (e.g., "2h", "30m")
127+
func (d Duration) MarshalYAML() (interface{}, error) {
128+
return time.Duration(d).String(), nil
129+
}

xtime/time_unmarshal_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,34 @@ func TestEncodeDecodeDuration(t *testing.T) {
111111
t.Error(err)
112112
}
113113
}
114+
115+
func TestDuration_Marshal(t *testing.T) {
116+
type testDuration struct {
117+
A Duration `json:"a" yaml:"a"`
118+
Dur Duration `json:"dur" yaml:"dur"`
119+
DurationPointer *Duration `json:"durationPointer,omitempty" yaml:"durationPointer,omitempty"`
120+
}
121+
122+
d1 := Duration(time.Second)
123+
d2 := Duration(0)
124+
d3 := Duration(time.Hour*24*7 + time.Second)
125+
126+
testData := testDuration{
127+
A: d1,
128+
Dur: d2,
129+
DurationPointer: &d3,
130+
}
131+
132+
yamlData, err := yaml.Marshal(&testData)
133+
if err != nil {
134+
t.Fatalf("Failed to marshal YAML: %v", err)
135+
}
136+
137+
expected := `a: 1s
138+
dur: 0s
139+
durationPointer: 168h0m1s
140+
`
141+
if string(yamlData) != expected {
142+
t.Errorf("Expected:\n%s\nGot:\n%s", expected, string(yamlData))
143+
}
144+
}

0 commit comments

Comments
 (0)