Skip to content

Commit 104cf06

Browse files
Refactor pointer utilities with generic implementations (#95)
* Refactor pointer utilities with generics Replace type-specific pointer helper functions with generic Pointer[T] and Dereference[T] implementations that work with any type. Changes: - Add generic Pointer[T any](val T) *T function to create pointers - Add generic Dereference[T any](ptr *T) T function to safely dereference - Deprecate type-specific functions: BoolP, PBool, StringP, PString, IntP, PInt, Int32P, PInt32, Int64P, PInt64 - Add comprehensive unit tests with 100% coverage for generic functions - Test coverage includes primitives, structs, nil handling, and zero values Signed-off-by: AdityaHarindar <aditya.harindar@gmail.com> * add test for structs with nested references Signed-off-by: AdityaHarindar <aditya.harindar@gmail.com> * simplify variable names Signed-off-by: AdityaHarindar <aditya.harindar@gmail.com> * replace deprecated BoolP() with Pointer() Signed-off-by: AdityaHarindar <aditya.harindar@gmail.com> --------- Signed-off-by: AdityaHarindar <aditya.harindar@gmail.com>
1 parent 05e3844 commit 104cf06

3 files changed

Lines changed: 328 additions & 1 deletion

File tree

db/mongo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func NewMongoClient(conf *MongoConfig) (StoreClient, error) {
472472
// majority, so we need to carefully evaluate right configuration
473473
// for the same
474474
wc := writeconcern.Majority()
475-
wc.Journal = utils.BoolP(true)
475+
wc.Journal = utils.Pointer(true)
476476
clientOptions.SetWriteConcern(wc)
477477

478478
client, err := mongo.Connect(clientOptions)

utils/pointer.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33

44
package utils
55

6+
// Pointer returns a pointer to the given value.
7+
// Usage:
8+
//
9+
// ptr := utils.Pointer(42) // *int pointing to 42
10+
// ptr := utils.Pointer("hello") // *string pointing to "hello"
11+
func Pointer[T any](v T) *T {
12+
return &v
13+
}
14+
15+
// Dereference returns the value of a pointer, or the zero value if the pointer is nil.
16+
// Usage:
17+
//
18+
// val := utils.Dereference(ptr) // returns value pointed by ptr, or zero value if ptr is nil
19+
func Dereference[T any](p *T) T {
20+
var val T
21+
if p != nil {
22+
val = *p
23+
}
24+
return val
25+
}
26+
27+
// Deprecated: Use Pointer instead.
628
// BoolP returns a pointer to the given bool value.
729
// Usage:
830
//
@@ -11,6 +33,7 @@ func BoolP(val bool) *bool {
1133
return &val
1234
}
1335

36+
// Deprecated: Use Dereference instead.
1437
// PBool returns the value of a *bool pointer, or false if the pointer is nil.
1538
// Usage:
1639
//
@@ -23,6 +46,7 @@ func PBool(ptr *bool) bool {
2346
return val
2447
}
2548

49+
// Deprecated: Use Pointer instead.
2650
// StringP returns a pointer to the given string value.
2751
// Usage:
2852
//
@@ -31,6 +55,7 @@ func StringP(val string) *string {
3155
return &val
3256
}
3357

58+
// Deprecated: Use Dereference instead.
3459
// PString returns the value of a *string pointer, or "" if the pointer is nil.
3560
// Usage:
3661
//
@@ -43,6 +68,7 @@ func PString(ptr *string) string {
4368
return val
4469
}
4570

71+
// Deprecated: Use Pointer instead.
4672
// IntP returns a pointer to the given int value.
4773
// Usage:
4874
//
@@ -51,6 +77,7 @@ func IntP(val int) *int {
5177
return &val
5278
}
5379

80+
// Deprecated: Use Dereference instead.
5481
// PInt returns the value of a *int pointer, or 0 if the pointer is nil.
5582
// Usage:
5683
//
@@ -63,6 +90,7 @@ func PInt(ptr *int) int {
6390
return val
6491
}
6592

93+
// Deprecated: Use Pointer instead.
6694
// Int32P returns a pointer to the given int32 value.
6795
// Usage:
6896
//
@@ -71,6 +99,7 @@ func Int32P(val int32) *int32 {
7199
return &val
72100
}
73101

102+
// Deprecated: Use Dereference instead.
74103
// PInt32 returns the value of a *int32 pointer, or 0 if the pointer is nil.
75104
// Usage:
76105
//
@@ -83,6 +112,7 @@ func PInt32(ptr *int32) int32 {
83112
return val
84113
}
85114

115+
// Deprecated: Use Pointer instead.
86116
// Int64P returns a pointer to the given int64 value.
87117
// Usage:
88118
//
@@ -91,6 +121,7 @@ func Int64P(val int64) *int64 {
91121
return &val
92122
}
93123

124+
// Deprecated: Use Dereference instead.
94125
// PInt64 returns the value of a *int64 pointer, or 0 if the pointer is nil.
95126
// Usage:
96127
//

0 commit comments

Comments
 (0)