Skip to content

Commit 1328865

Browse files
Added helper to improve readability of pointer literals (#535)
* Added helper to improve readability of pointer literals * Added comments an examples of how to use pointer
1 parent bd61260 commit 1328865

File tree

5 files changed

+78
-12
lines changed

5 files changed

+78
-12
lines changed

pointer_helpers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package linodego
2+
3+
/*
4+
Pointer takes a value of any type T and returns a pointer to that value.
5+
Go does not allow directly creating pointers to literals, so Pointer enables
6+
abstraction away the pointer logic.
7+
8+
Example:
9+
10+
booted := true
11+
12+
createOpts := linodego.InstanceCreateOptions{
13+
Booted: &booted,
14+
}
15+
16+
can be replaced with
17+
18+
createOpts := linodego.InstanceCreateOptions{
19+
Booted: linodego.Pointer(true),
20+
}
21+
*/
22+
23+
func Pointer[T any](value T) *T {
24+
return &value
25+
}

pointer_helpers_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package linodego
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestPointer tests the Pointer helper function with various types
8+
func TestPointer(t *testing.T) {
9+
// Test with an integer
10+
intValue := 11
11+
intPtr := Pointer(intValue)
12+
if *intPtr != intValue {
13+
t.Errorf("Expected %d, got %d", intValue, *intPtr)
14+
}
15+
16+
// Test with a float
17+
floatValue := 1.23
18+
floatPtr := Pointer(floatValue)
19+
if *floatPtr != floatValue {
20+
t.Errorf("Expected %f, got %f", floatValue, *floatPtr)
21+
}
22+
23+
// Test with a string
24+
stringValue := "hello world"
25+
stringPtr := Pointer(stringValue)
26+
if *stringPtr != stringValue {
27+
t.Errorf("Expected %s, got %s", stringValue, *stringPtr)
28+
}
29+
30+
// Test with a boolean
31+
boolValue := true
32+
boolPtr := Pointer(boolValue)
33+
if *boolPtr != boolValue {
34+
t.Errorf("Expected %t, got %t", boolValue, *boolPtr)
35+
}
36+
37+
// Test with a struct
38+
type myStruct struct {
39+
Field1 int
40+
Field2 string
41+
}
42+
structValue := myStruct{Field1: 1, Field2: "test"}
43+
structPtr := Pointer(structValue)
44+
if structPtr.Field1 != structValue.Field1 || structPtr.Field2 != structValue.Field2 {
45+
t.Errorf("Expected %+v, got %+v", structValue, *structPtr)
46+
}
47+
}

test/integration/example_nodebalancers_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,13 @@ func ExampleClient_CreateNodeBalancerNode() {
153153
log.Fatal(err)
154154
}
155155

156-
booted := false
157156
instanceOpts := linodego.InstanceCreateOptions{
158157
Label: "nodebalancer-example-instance",
159158
RootPass: randPassword(),
160159
Region: "us-southeast",
161160
Type: "g6-nanode-1",
162161
Image: "linode/debian9",
163-
Booted: &booted,
162+
Booted: linodego.Pointer(false),
164163
FirewallID: GetFirewallID(),
165164
}
166165
instance, err := linodeClient.CreateInstance(context.Background(), instanceOpts)

test/integration/instances_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,14 @@ func createInstance(t *testing.T, client *linodego.Client, enableCloudFirewall b
515515
if t != nil {
516516
t.Helper()
517517
}
518-
booted := false
518+
519519
createOpts := linodego.InstanceCreateOptions{
520520
Label: "go-test-ins-" + randLabel(),
521521
RootPass: randPassword(),
522522
Region: getRegionsWithCaps(t, client, []string{"linodes"})[0],
523523
Type: "g6-nanode-1",
524524
Image: "linode/debian9",
525-
Booted: &booted,
525+
Booted: linodego.Pointer(false),
526526
}
527527

528528
if enableCloudFirewall {
@@ -565,12 +565,11 @@ func createInstanceWithoutDisks(
565565
) (*linodego.Instance, *linodego.InstanceConfig, func(), error) {
566566
t.Helper()
567567

568-
falseBool := false
569568
createOpts := linodego.InstanceCreateOptions{
570569
Label: "go-test-ins-wo-disk-" + randLabel(),
571570
Region: getRegionsWithCaps(t, client, []string{"linodes"})[0],
572571
Type: "g6-nanode-1",
573-
Booted: &falseBool,
572+
Booted: linodego.Pointer(false),
574573
}
575574

576575
if enableCloudFirewall {

test/integration/waitfor_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ func TestEventPoller_InstancePower(t *testing.T) {
1717
t.Fatalf("failed to initialize event poller: %s", err)
1818
}
1919

20-
booted := false
21-
2220
instance, err := client.CreateInstance(context.Background(), linodego.InstanceCreateOptions{
2321
Region: getRegionsWithCaps(t, client, []string{"Linodes"})[0],
2422
Type: "g6-nanode-1",
2523
Image: "linode/ubuntu22.04",
2624
RootPass: randPassword(),
2725
Label: "go-ins-poll-test",
28-
Booted: &booted,
26+
Booted: linodego.Pointer(false),
2927
})
3028
if err != nil {
3129
t.Fatal(err)
@@ -146,13 +144,11 @@ func TestEventPoller_Secondary(t *testing.T) {
146144
t.Fatalf("failed to initialize event poller: %s", err)
147145
}
148146

149-
booted := false
150-
151147
instance, err := client.CreateInstance(context.Background(), linodego.InstanceCreateOptions{
152148
Region: getRegionsWithCaps(t, client, []string{"Linodes"})[0],
153149
Type: "g6-nanode-1",
154150
Label: "go-ins-poll-test",
155-
Booted: &booted,
151+
Booted: linodego.Pointer(false),
156152
})
157153
if err != nil {
158154
t.Fatal(err)

0 commit comments

Comments
 (0)