generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added custom duration for webhook registration unmarshaling (#103)
* added custom duration for webhook registration unmarshaling * added tests, fixed error * updated changelog, prep for release
- Loading branch information
1 parent
d3db9b4
commit e4d9474
Showing
6 changed files
with
331 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright 2022 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package ancla | ||
|
||
import ( | ||
"bytes" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type InvalidDurationError struct { | ||
Value string | ||
} | ||
|
||
func (ide *InvalidDurationError) Error() string { | ||
var o strings.Builder | ||
o.WriteString("duration must be of type int or string (ex:'5m'); Invalid value: ") | ||
o.WriteString(ide.Value) | ||
return o.String() | ||
} | ||
|
||
type CustomDuration time.Duration | ||
|
||
func (cd CustomDuration) String() string { | ||
return time.Duration(cd).String() | ||
} | ||
|
||
func (cd CustomDuration) MarshalJSON() ([]byte, error) { | ||
d := bytes.NewBuffer(nil) | ||
d.WriteByte('"') | ||
d.WriteString(cd.String()) | ||
d.WriteByte('"') | ||
return d.Bytes(), nil | ||
} | ||
|
||
func (cd *CustomDuration) UnmarshalJSON(b []byte) (err error) { | ||
if b[0] == '"' { | ||
var d time.Duration | ||
d, err = time.ParseDuration(string(b[1 : len(b)-1])) | ||
if err == nil { | ||
*cd = CustomDuration(d) | ||
return | ||
} | ||
} | ||
|
||
var d int64 | ||
d, err = strconv.ParseInt(string(b), 10, 64) | ||
if err == nil { | ||
*cd = CustomDuration(time.Duration(d) * time.Second) | ||
return | ||
} | ||
|
||
err = &InvalidDurationError{ | ||
Value: string(b), | ||
} | ||
|
||
return | ||
} |
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,108 @@ | ||
/** | ||
* Copyright 2022 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package ancla | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnmarshalJSON(t *testing.T) { | ||
type test struct { | ||
Duration CustomDuration | ||
} | ||
tests := []struct { | ||
description string | ||
input []byte | ||
expectedDuration CustomDuration | ||
errExpected bool | ||
}{ | ||
{ | ||
description: "Int success", | ||
input: []byte(`{"duration":50}`), | ||
expectedDuration: CustomDuration(50 * time.Second), | ||
}, | ||
{ | ||
description: "String success", | ||
input: []byte(`{"duration":"5m"}`), | ||
expectedDuration: CustomDuration(5 * time.Minute), | ||
}, | ||
{ | ||
description: "String failure", | ||
input: []byte(`{"duration":"2r"}`), | ||
errExpected: true, | ||
}, | ||
{ | ||
description: "Object failure", | ||
input: []byte(`{"duration":{"key":"val"}}`), | ||
errExpected: true, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.description, func(t *testing.T) { | ||
assert := assert.New(t) | ||
cd := test{} | ||
err := json.Unmarshal(tc.input, &cd) | ||
assert.Equal(tc.expectedDuration, cd.Duration) | ||
if !tc.errExpected { | ||
assert.NoError(err) | ||
return | ||
} | ||
assert.Error(err) | ||
}) | ||
} | ||
} | ||
|
||
func TestMarshalJSON(t *testing.T) { | ||
type test struct { | ||
Duration CustomDuration | ||
} | ||
tests := []struct { | ||
description string | ||
input test | ||
expectedOutput []byte | ||
errExpected bool | ||
}{ | ||
{ | ||
description: "Int success", | ||
input: test{Duration: CustomDuration(50 * time.Second)}, | ||
expectedOutput: []byte(`{"Duration":"50s"}`), | ||
}, | ||
{ | ||
description: "String success", | ||
input: test{Duration: CustomDuration(5 * time.Minute)}, | ||
expectedOutput: []byte(`{"Duration":"5m0s"}`), | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.description, func(t *testing.T) { | ||
assert := assert.New(t) | ||
output, err := json.Marshal(tc.input) | ||
assert.Equal(tc.expectedOutput, output) | ||
if !tc.errExpected { | ||
assert.NoError(err) | ||
return | ||
} | ||
assert.Error(err) | ||
}) | ||
} | ||
|
||
} |
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
Oops, something went wrong.