Skip to content

Commit 03b3667

Browse files
committed
chore: Add JSON unmarshalling for duration in helpers package
1 parent 5b86c0d commit 03b3667

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

helpers/helpers.go

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package helpers
33

44
import (
5+
"encoding/json"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -33,3 +34,25 @@ func SafeOpenFile(filePath string) (*os.File, error) {
3334
// Open the file if the path is deemed safe
3435
return os.Open(absPath)
3536
}
37+
38+
// UnmarshalJSON parses the duration from JSON string.
39+
func (d *JSONDuration) UnmarshalJSON(b []byte) error {
40+
var s string
41+
if err := json.Unmarshal(b, &s); err != nil {
42+
return err
43+
}
44+
duration, err := time.ParseDuration(s)
45+
if err != nil {
46+
return err
47+
}
48+
*d = JSONDuration(duration)
49+
return nil
50+
}
51+
52+
// Duration returns the time.Duration value.
53+
func (d JSONDuration) Duration() time.Duration {
54+
return time.Duration(d)
55+
}
56+
57+
// JSONDuration wraps time.Duration for custom JSON unmarshalling.
58+
type JSONDuration time.Duration

httpclient/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ type ConcurrencyConfig struct {
9595

9696
// TimeoutConfig holds custom timeout settings.
9797
type TimeoutConfig struct {
98-
CustomTimeout time.Duration // Custom timeout for the HTTP client
99-
TokenRefreshBufferPeriod time.Duration // Buffer period before token expiry to attempt token refresh
100-
TotalRetryDuration time.Duration // Total duration to attempt retries
98+
CustomTimeout time.Duration `json:"CustomTimeout,omitempty"` // Custom timeout for the HTTP client
99+
TokenRefreshBufferPeriod time.Duration `json:"TokenRefreshBufferPeriod,omitempty"` // Buffer period before token expiry to attempt token refresh
100+
TotalRetryDuration time.Duration `json:"TotalRetryDuration,omitempty"` // Total duration to attempt retries
101101
}
102102

103103
// RedirectConfig holds configuration related to redirect handling.

0 commit comments

Comments
 (0)