Skip to content

Commit 302d211

Browse files
wtrockis-urbaniak
andauthored
admin/utils.go: add helper methods for parsing and pointer handling (#269)
Co-authored-by: Sergiusz Urbaniak <[email protected]>
1 parent 053f029 commit 302d211

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

admin/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ func IsNil(i interface{}) bool {
4949
return false
5050
}
5151

52+
// SetOrNil returns the address of the given value or nil if it equals defaultValue
53+
func SetOrNil[T comparable](val T, defaultValue T) *T {
54+
if val == defaultValue {
55+
return nil
56+
}
57+
return &val
58+
}
59+
5260
// GetOrDefault returns the value of a pointer or a default value
5361
func GetOrDefault[T any](ptr *T, defaultValue T) T {
5462
if ptr != nil {
@@ -74,6 +82,13 @@ func TimeToString(t time.Time) string {
7482
return t.UTC().Format(time.RFC3339Nano)
7583
}
7684

85+
// StringToTime parses the given string and returns the resulting time.
86+
// The expected format is identical to the format returned by Atlas API, documented as ISO 8601 timestamp format in UTC.
87+
// Example formats: "2023-07-18T16:12:23Z", "2023-07-18T16:12:23.456Z"
88+
func StringToTime(val string) (time.Time, error) {
89+
return time.Parse(time.RFC3339Nano, val)
90+
}
91+
7792
// Int64PtrToIntPtr convert int64 to int pointer.
7893
func Int64PtrToIntPtr(i64 *int64) *int {
7994
if i64 == nil {

internal/core/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package core
55
// For more information please see: https://github.com/mongodb/atlas-sdk-go/blob/main/docs/doc_1_concepts.md
66
const (
77
// SDK release tag version.
8-
Version = "v20231115005.0.0"
8+
Version = "v20231115005.1.0"
99
// Resource Version.
1010
Resource = "20231115"
1111
)

test/utils_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"go.mongodb.org/atlas-sdk/v20231115005/admin"
8+
)
9+
10+
type testCase[T comparable] struct {
11+
name string
12+
val T
13+
defaultValue T
14+
wantNil bool
15+
}
16+
17+
//nolint:thelper // used to minimize duplication in test
18+
func assertSetOrNil[T comparable](t *testing.T, tc testCase[T]) {
19+
t.Run(tc.name, func(t *testing.T) {
20+
ptr := admin.SetOrNil(tc.val, tc.defaultValue)
21+
if gotNil := ptr == nil; gotNil != tc.wantNil {
22+
t.Errorf("got nil %t, want %t", gotNil, tc.wantNil)
23+
}
24+
})
25+
}
26+
27+
func TestSetOrNil(t *testing.T) {
28+
assertSetOrNil(t, testCase[int]{
29+
name: "non default int",
30+
val: 1,
31+
defaultValue: 0,
32+
wantNil: false,
33+
})
34+
35+
assertSetOrNil(t, testCase[int]{
36+
name: "default int",
37+
val: 0,
38+
defaultValue: 0,
39+
wantNil: true,
40+
})
41+
42+
assertSetOrNil(t, testCase[string]{
43+
name: "non default string",
44+
val: "hello",
45+
defaultValue: "",
46+
wantNil: false,
47+
})
48+
49+
assertSetOrNil(t, testCase[string]{
50+
name: "default string",
51+
val: "",
52+
defaultValue: "",
53+
wantNil: true,
54+
})
55+
}
56+
57+
func TestStringToTime(t *testing.T) {
58+
for _, tc := range []struct {
59+
name string
60+
input string
61+
62+
want time.Time
63+
wantErr string
64+
}{
65+
{
66+
name: "valid time",
67+
input: "2023-07-18T16:12:23Z",
68+
want: time.Date(
69+
2023, 7, 18,
70+
16, 12, 23, 0,
71+
time.UTC,
72+
),
73+
},
74+
{
75+
name: "valid time with millis",
76+
input: "2023-07-18T16:12:23.456Z",
77+
want: time.Date(
78+
2023, 7, 18,
79+
16, 12, 23, 456_000_000,
80+
time.UTC,
81+
),
82+
},
83+
{
84+
name: "invalid time",
85+
input: "invalid",
86+
wantErr: `parsing time "invalid" as "2006-01-02T15:04:05.999999999Z07:00": cannot parse "invalid" as "2006"`,
87+
},
88+
} {
89+
t.Run(tc.name, func(t *testing.T) {
90+
gotErr := ""
91+
got, err := admin.StringToTime(tc.input)
92+
if err != nil {
93+
gotErr = err.Error()
94+
}
95+
96+
if gotErr != tc.wantErr {
97+
t.Errorf("want error %q, got %q", tc.wantErr, gotErr)
98+
}
99+
if got != tc.want {
100+
t.Errorf("want %v, got %v", tc.want, got)
101+
}
102+
})
103+
}
104+
}

tools/config/go-templates/utils.mustache

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func PtrString(v string) *string { return &v }
3030
// PtrTime is helper routine that returns a pointer to given Time value.
3131
func PtrTime(v time.Time) *time.Time { return &v }
3232

33-
3433
type modelWithMap interface {
3534
ToMap() (map[string]interface{}, error)
3635
}
@@ -49,6 +48,14 @@ func IsNil(i interface{}) bool {
4948
return false
5049
}
5150

51+
// SetOrNil returns the address of the given value or nil if it equals defaultValue
52+
func SetOrNil[T comparable](val T, defaultValue T) *T {
53+
if val == defaultValue {
54+
return nil
55+
}
56+
return &val
57+
}
58+
5259
// GetOrDefault returns the value of a pointer or a default value
5360
func GetOrDefault[T any](ptr *T, defaultValue T) T {
5461
if ptr != nil {
@@ -74,6 +81,13 @@ func TimeToString(t time.Time) string {
7481
return t.UTC().Format(time.RFC3339Nano)
7582
}
7683

84+
// StringToTime parses the given string and returns the resulting time.
85+
// The expected format is identical to the format returned by Atlas API, documented as ISO 8601 timestamp format in UTC.
86+
// Example formats: "2023-07-18T16:12:23Z", "2023-07-18T16:12:23.456Z"
87+
func StringToTime(val string) (time.Time, error) {
88+
return time.Parse(time.RFC3339Nano, val)
89+
}
90+
7791
// Int64PtrToIntPtr convert int64 to int pointer.
7892
func Int64PtrToIntPtr(i64 *int64) *int {
7993
if i64 == nil {

0 commit comments

Comments
 (0)