Skip to content

Commit a5cd797

Browse files
authored
feat: Use generics to get pointers to types (#219)
Since the minimum required Go version is 1.18 according to go.mod we can use generics to have a single function that can return a pointer to any type. This avoids the need to add more $Type helpers in the future. The new Ptr function is introduced and exsiting functions are updated to call through Ptr instead. Any exported functions are now marked as deprecated but not removed in order to avoid breaking backwards compatibility. All non-exported functions are removed. All call sites are updated to use Ptr directly.
1 parent 69b0a11 commit a5cd797

16 files changed

+240
-231
lines changed

hcloud/certificate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func TestCertificateClientUpdate(t *testing.T) {
405405
t.Fatal(err)
406406
}
407407
expectedReqBody := schema.CertificateUpdateRequest{
408-
Name: String("test"),
408+
Name: Ptr("test"),
409409
Labels: func() *map[string]string {
410410
labels := map[string]string{"key": "value"}
411411
return &labels

hcloud/firewall_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func TestFirewallClientUpdate(t *testing.T) {
281281
t.Fatal(err)
282282
}
283283
expectedReqBody := schema.FirewallUpdateRequest{
284-
Name: String("test"),
284+
Name: Ptr("test"),
285285
Labels: func() *map[string]string {
286286
labels := map[string]string{"key": "value"}
287287
return &labels

hcloud/floating_ip.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ func (c *FloatingIPClient) Create(ctx context.Context, opts FloatingIPCreateOpts
241241
Name: opts.Name,
242242
}
243243
if opts.HomeLocation != nil {
244-
reqBody.HomeLocation = String(opts.HomeLocation.Name)
244+
reqBody.HomeLocation = Ptr(opts.HomeLocation.Name)
245245
}
246246
if opts.Server != nil {
247-
reqBody.Server = Int(opts.Server.ID)
247+
reqBody.Server = Ptr(opts.Server.ID)
248248
}
249249
if opts.Labels != nil {
250250
reqBody.Labels = &opts.Labels

hcloud/floating_ip_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func TestFloatingIPClientCreate(t *testing.T) {
247247

248248
opts := FloatingIPCreateOpts{
249249
Type: FloatingIPTypeIPv4,
250-
Description: String("test"),
250+
Description: Ptr("test"),
251251
HomeLocation: &Location{Name: "test"},
252252
Server: &Server{ID: 1},
253253
Labels: map[string]string{"key": "value"},
@@ -292,10 +292,10 @@ func TestFloatingIPClientCreateWithName(t *testing.T) {
292292

293293
opts := FloatingIPCreateOpts{
294294
Type: FloatingIPTypeIPv4,
295-
Description: String("test"),
295+
Description: Ptr("test"),
296296
HomeLocation: &Location{Name: "test"},
297297
Server: &Server{ID: 1},
298-
Name: String("MyFloatingIP"),
298+
Name: Ptr("MyFloatingIP"),
299299
Labels: map[string]string{"key": "value"},
300300
}
301301

@@ -564,7 +564,7 @@ func TestFloatingIPClientChangeProtection(t *testing.T) {
564564
})
565565

566566
opts := FloatingIPChangeProtectionOpts{
567-
Delete: Bool(true),
567+
Delete: Ptr(true),
568568
}
569569
action, _, err := env.Client.FloatingIP.ChangeProtection(ctx, floatingIP, opts)
570570
if err != nil {

hcloud/helper.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@ package hcloud
22

33
import "time"
44

5+
// Ptr returns a pointer to p.
6+
func Ptr[T any](p T) *T {
7+
return &p
8+
}
9+
510
// String returns a pointer to the passed string s.
6-
func String(s string) *string { return &s }
11+
//
12+
// Deprecated: Use [Ptr] instead.
13+
func String(s string) *string { return Ptr(s) }
714

815
// Int returns a pointer to the passed integer i.
9-
func Int(i int) *int { return &i }
16+
//
17+
// Deprecated: Use [Ptr] instead.
18+
func Int(i int) *int { return Ptr(i) }
1019

1120
// Bool returns a pointer to the passed bool b.
12-
func Bool(b bool) *bool { return &b }
21+
//
22+
// Deprecated: Use [Ptr] instead.
23+
func Bool(b bool) *bool { return Ptr(b) }
1324

1425
// Duration returns a pointer to the passed time.Duration d.
15-
func Duration(d time.Duration) *time.Duration { return &d }
16-
17-
func intSlice(is []int) *[]int { return &is }
18-
func stringSlice(ss []string) *[]string { return &ss }
26+
//
27+
// Deprecated: Use [Ptr] instead.
28+
func Duration(d time.Duration) *time.Duration { return Ptr(d) }

hcloud/image.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (c *ImageClient) Update(ctx context.Context, image *Image, opts ImageUpdate
222222
Description: opts.Description,
223223
}
224224
if opts.Type != "" {
225-
reqBody.Type = String(string(opts.Type))
225+
reqBody.Type = Ptr(string(opts.Type))
226226
}
227227
if opts.Labels != nil {
228228
reqBody.Labels = &opts.Labels

hcloud/image_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func TestImageClientUpdate(t *testing.T) {
338338
})
339339

340340
opts := ImageUpdateOpts{
341-
Description: String("test"),
341+
Description: Ptr("test"),
342342
Type: ImageTypeSnapshot,
343343
}
344344
updatedImage, _, err := env.Client.Image.Update(ctx, image, opts)
@@ -417,7 +417,7 @@ func TestImageClientChangeProtection(t *testing.T) {
417417
})
418418

419419
opts := ImageChangeProtectionOpts{
420-
Delete: Bool(true),
420+
Delete: Ptr(true),
421421
}
422422
action, _, err := env.Client.Image.ChangeProtection(ctx, image, opts)
423423
if err != nil {

hcloud/load_balancer.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ type LoadBalancerCreateResult struct {
491491
func (c *LoadBalancerClient) Create(ctx context.Context, opts LoadBalancerCreateOpts) (LoadBalancerCreateResult, *Response, error) {
492492
reqBody := loadBalancerCreateOptsToSchema(opts)
493493
reqBodyData, err := json.Marshal(reqBody)
494-
495494
if err != nil {
496495
return LoadBalancerCreateResult{}, nil, err
497496
}
@@ -865,7 +864,7 @@ func (c *LoadBalancerClient) AttachToNetwork(ctx context.Context, loadBalancer *
865864
Network: opts.Network.ID,
866865
}
867866
if opts.IP != nil {
868-
reqBody.IP = String(opts.IP.String())
867+
reqBody.IP = Ptr(opts.IP.String())
869868
}
870869
reqBodyData, err := json.Marshal(reqBody)
871870
if err != nil {

hcloud/load_balancer_test.go

+56-56
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func TestLoadBalancerCreate(t *testing.T) {
179179
Algorithm: &schema.LoadBalancerCreateRequestAlgorithm{
180180
Type: "round_robin",
181181
},
182-
Location: String("fsn1"),
182+
Location: Ptr("fsn1"),
183183
}
184184
if !cmp.Equal(expectedReqBody, reqBody) {
185185
t.Log(cmp.Diff(expectedReqBody, reqBody))
@@ -246,7 +246,7 @@ func TestLoadBalancerClientUpdate(t *testing.T) {
246246
t.Fatal(err)
247247
}
248248
expectedReqBody := schema.LoadBalancerUpdateRequest{
249-
Name: String("test"),
249+
Name: Ptr("test"),
250250
}
251251
if !cmp.Equal(expectedReqBody, reqBody) {
252252
t.Log(cmp.Diff(expectedReqBody, reqBody))
@@ -289,7 +289,7 @@ func TestLoadBalancerClientChangeProtection(t *testing.T) {
289289
t.Fatal(err)
290290
}
291291
expectedReqBody := schema.LoadBalancerActionChangeProtectionRequest{
292-
Delete: Bool(true),
292+
Delete: Ptr(true),
293293
}
294294
if !cmp.Equal(expectedReqBody, reqBody) {
295295
t.Log(cmp.Diff(expectedReqBody, reqBody))
@@ -308,7 +308,7 @@ func TestLoadBalancerClientChangeProtection(t *testing.T) {
308308
)
309309

310310
opts := LoadBalancerChangeProtectionOpts{
311-
Delete: Bool(true),
311+
Delete: Ptr(true),
312312
}
313313
action, _, err := env.Client.LoadBalancer.ChangeProtection(ctx, loadBalancer, opts)
314314
if err != nil {
@@ -336,7 +336,7 @@ func TestLoadBalancerClientAddServerTarget(t *testing.T) {
336336
Server: &schema.LoadBalancerActionAddTargetRequestServer{
337337
ID: 1,
338338
},
339-
UsePrivateIP: Bool(true),
339+
UsePrivateIP: Ptr(true),
340340
}
341341
if !cmp.Equal(expectedReqBody, reqBody) {
342342
t.Log(cmp.Diff(expectedReqBody, reqBody))
@@ -357,7 +357,7 @@ func TestLoadBalancerClientAddServerTarget(t *testing.T) {
357357

358358
opts := LoadBalancerAddServerTargetOpts{
359359
Server: server,
360-
UsePrivateIP: Bool(true),
360+
UsePrivateIP: Ptr(true),
361361
}
362362
action, _, err := env.Client.LoadBalancer.AddServerTarget(ctx, loadBalancer, opts)
363363
if err != nil {
@@ -426,23 +426,23 @@ func TestLoadBalancerAddService(t *testing.T) {
426426
}
427427
expectedReqBody := schema.LoadBalancerActionAddServiceRequest{
428428
Protocol: string(LoadBalancerServiceProtocolHTTP),
429-
ListenPort: Int(4711),
430-
DestinationPort: Int(80),
429+
ListenPort: Ptr(4711),
430+
DestinationPort: Ptr(80),
431431
HTTP: &schema.LoadBalancerActionAddServiceRequestHTTP{
432-
CookieName: String("HCLBSTICKY"),
433-
CookieLifetime: Int(5 * 60),
434-
RedirectHTTP: Bool(false),
435-
StickySessions: Bool(true),
432+
CookieName: Ptr("HCLBSTICKY"),
433+
CookieLifetime: Ptr(5 * 60),
434+
RedirectHTTP: Ptr(false),
435+
StickySessions: Ptr(true),
436436
},
437437
HealthCheck: &schema.LoadBalancerActionAddServiceRequestHealthCheck{
438438
Protocol: "http",
439-
Port: Int(4711),
440-
Interval: Int(15),
441-
Timeout: Int(10),
442-
Retries: Int(3),
439+
Port: Ptr(4711),
440+
Interval: Ptr(15),
441+
Timeout: Ptr(10),
442+
Retries: Ptr(3),
443443
HTTP: &schema.LoadBalancerActionAddServiceRequestHealthCheckHTTP{
444-
Domain: String("example.com"),
445-
Path: String("/"),
444+
Domain: Ptr("example.com"),
445+
Path: Ptr("/"),
446446
},
447447
},
448448
}
@@ -464,23 +464,23 @@ func TestLoadBalancerAddService(t *testing.T) {
464464

465465
opts := LoadBalancerAddServiceOpts{
466466
Protocol: LoadBalancerServiceProtocolHTTP,
467-
ListenPort: Int(4711),
468-
DestinationPort: Int(80),
467+
ListenPort: Ptr(4711),
468+
DestinationPort: Ptr(80),
469469
HTTP: &LoadBalancerAddServiceOptsHTTP{
470-
CookieName: String("HCLBSTICKY"),
471-
CookieLifetime: Duration(5 * time.Minute),
472-
RedirectHTTP: Bool(false),
473-
StickySessions: Bool(true),
470+
CookieName: Ptr("HCLBSTICKY"),
471+
CookieLifetime: Ptr(5 * time.Minute),
472+
RedirectHTTP: Ptr(false),
473+
StickySessions: Ptr(true),
474474
},
475475
HealthCheck: &LoadBalancerAddServiceOptsHealthCheck{
476476
Protocol: "http",
477-
Port: Int(4711),
478-
Interval: Duration(15 * time.Second),
479-
Timeout: Duration(10 * time.Second),
480-
Retries: Int(3),
477+
Port: Ptr(4711),
478+
Interval: Ptr(15 * time.Second),
479+
Timeout: Ptr(10 * time.Second),
480+
Retries: Ptr(3),
481481
HTTP: &LoadBalancerAddServiceOptsHealthCheckHTTP{
482-
Domain: String("example.com"),
483-
Path: String("/"),
482+
Domain: Ptr("example.com"),
483+
Path: Ptr("/"),
484484
},
485485
},
486486
}
@@ -506,24 +506,24 @@ func TestLoadBalancerUpdateService(t *testing.T) {
506506
t.Fatal(err)
507507
}
508508
expectedReqBody := schema.LoadBalancerActionUpdateServiceRequest{
509-
Protocol: String(string(LoadBalancerServiceProtocolHTTP)),
509+
Protocol: Ptr(string(LoadBalancerServiceProtocolHTTP)),
510510
ListenPort: 4711,
511-
DestinationPort: Int(80),
511+
DestinationPort: Ptr(80),
512512
HTTP: &schema.LoadBalancerActionUpdateServiceRequestHTTP{
513-
CookieName: String("HCLBSTICKY"),
514-
CookieLifetime: Int(5 * 60),
515-
RedirectHTTP: Bool(false),
516-
StickySessions: Bool(true),
513+
CookieName: Ptr("HCLBSTICKY"),
514+
CookieLifetime: Ptr(5 * 60),
515+
RedirectHTTP: Ptr(false),
516+
StickySessions: Ptr(true),
517517
},
518518
HealthCheck: &schema.LoadBalancerActionUpdateServiceRequestHealthCheck{
519-
Protocol: String(string(LoadBalancerServiceProtocolHTTP)),
520-
Port: Int(4711),
521-
Interval: Int(15),
522-
Timeout: Int(10),
523-
Retries: Int(3),
519+
Protocol: Ptr(string(LoadBalancerServiceProtocolHTTP)),
520+
Port: Ptr(4711),
521+
Interval: Ptr(15),
522+
Timeout: Ptr(10),
523+
Retries: Ptr(3),
524524
HTTP: &schema.LoadBalancerActionUpdateServiceRequestHealthCheckHTTP{
525-
Domain: String("example.com"),
526-
Path: String("/"),
525+
Domain: Ptr("example.com"),
526+
Path: Ptr("/"),
527527
},
528528
},
529529
}
@@ -545,22 +545,22 @@ func TestLoadBalancerUpdateService(t *testing.T) {
545545

546546
opts := LoadBalancerUpdateServiceOpts{
547547
Protocol: LoadBalancerServiceProtocolHTTP,
548-
DestinationPort: Int(80),
548+
DestinationPort: Ptr(80),
549549
HTTP: &LoadBalancerUpdateServiceOptsHTTP{
550-
CookieName: String("HCLBSTICKY"),
551-
CookieLifetime: Duration(5 * time.Minute),
552-
RedirectHTTP: Bool(false),
553-
StickySessions: Bool(true),
550+
CookieName: Ptr("HCLBSTICKY"),
551+
CookieLifetime: Ptr(5 * time.Minute),
552+
RedirectHTTP: Ptr(false),
553+
StickySessions: Ptr(true),
554554
},
555555
HealthCheck: &LoadBalancerUpdateServiceOptsHealthCheck{
556556
Protocol: LoadBalancerServiceProtocolHTTP,
557-
Port: Int(4711),
558-
Interval: Duration(15 * time.Second),
559-
Timeout: Duration(10 * time.Second),
560-
Retries: Int(3),
557+
Port: Ptr(4711),
558+
Interval: Ptr(15 * time.Second),
559+
Timeout: Ptr(10 * time.Second),
560+
Retries: Ptr(3),
561561
HTTP: &LoadBalancerUpdateServiceOptsHealthCheckHTTP{
562-
Domain: String("example.com"),
563-
Path: String("/"),
562+
Domain: Ptr("example.com"),
563+
Path: Ptr("/"),
564564
},
565565
},
566566
}
@@ -670,7 +670,7 @@ func TestLoadBalancerClientAttachToNetwork(t *testing.T) {
670670
}
671671
expectedReqBody := schema.LoadBalancerActionAttachToNetworkRequest{
672672
Network: 1,
673-
IP: String("10.0.1.1"),
673+
IP: Ptr("10.0.1.1"),
674674
}
675675
if !cmp.Equal(expectedReqBody, reqBody) {
676676
t.Log(cmp.Diff(expectedReqBody, reqBody))
@@ -898,7 +898,7 @@ func TestLoadBalancerClientAddLabelSelectorTarget(t *testing.T) {
898898
ctx := context.Background()
899899
action, _, err := env.Client.LoadBalancer.AddLabelSelectorTarget(ctx, &LoadBalancer{ID: 1}, LoadBalancerAddLabelSelectorTargetOpts{
900900
Selector: "key=value",
901-
UsePrivateIP: Bool(false),
901+
UsePrivateIP: Ptr(false),
902902
})
903903
if err != nil {
904904
t.Fatal(err)

hcloud/network_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ func TestNetworkClientChangeProtection(t *testing.T) {
622622
})
623623

624624
opts := NetworkChangeProtectionOpts{
625-
Delete: Bool(true),
625+
Delete: Ptr(true),
626626
}
627627
action, _, err := env.Client.Network.ChangeProtection(ctx, network, opts)
628628
if err != nil {

0 commit comments

Comments
 (0)