Skip to content

Commit

Permalink
Added helper to improve readability of pointer literals (#535)
Browse files Browse the repository at this point in the history
* Added helper to improve readability of pointer literals

* Added comments an examples of how to use pointer
  • Loading branch information
ezilber-akamai authored Jul 5, 2024
1 parent bd61260 commit 1328865
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
25 changes: 25 additions & 0 deletions pointer_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package linodego

/*
Pointer takes a value of any type T and returns a pointer to that value.
Go does not allow directly creating pointers to literals, so Pointer enables
abstraction away the pointer logic.
Example:
booted := true
createOpts := linodego.InstanceCreateOptions{
Booted: &booted,
}
can be replaced with
createOpts := linodego.InstanceCreateOptions{
Booted: linodego.Pointer(true),
}
*/

func Pointer[T any](value T) *T {
return &value
}
47 changes: 47 additions & 0 deletions pointer_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package linodego

import (
"testing"
)

// TestPointer tests the Pointer helper function with various types
func TestPointer(t *testing.T) {
// Test with an integer
intValue := 11
intPtr := Pointer(intValue)
if *intPtr != intValue {
t.Errorf("Expected %d, got %d", intValue, *intPtr)
}

// Test with a float
floatValue := 1.23
floatPtr := Pointer(floatValue)
if *floatPtr != floatValue {
t.Errorf("Expected %f, got %f", floatValue, *floatPtr)
}

// Test with a string
stringValue := "hello world"
stringPtr := Pointer(stringValue)
if *stringPtr != stringValue {
t.Errorf("Expected %s, got %s", stringValue, *stringPtr)
}

// Test with a boolean
boolValue := true
boolPtr := Pointer(boolValue)
if *boolPtr != boolValue {
t.Errorf("Expected %t, got %t", boolValue, *boolPtr)
}

// Test with a struct
type myStruct struct {
Field1 int
Field2 string
}
structValue := myStruct{Field1: 1, Field2: "test"}
structPtr := Pointer(structValue)
if structPtr.Field1 != structValue.Field1 || structPtr.Field2 != structValue.Field2 {
t.Errorf("Expected %+v, got %+v", structValue, *structPtr)
}
}
3 changes: 1 addition & 2 deletions test/integration/example_nodebalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,13 @@ func ExampleClient_CreateNodeBalancerNode() {
log.Fatal(err)
}

booted := false
instanceOpts := linodego.InstanceCreateOptions{
Label: "nodebalancer-example-instance",
RootPass: randPassword(),
Region: "us-southeast",
Type: "g6-nanode-1",
Image: "linode/debian9",
Booted: &booted,
Booted: linodego.Pointer(false),
FirewallID: GetFirewallID(),
}
instance, err := linodeClient.CreateInstance(context.Background(), instanceOpts)
Expand Down
7 changes: 3 additions & 4 deletions test/integration/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,14 @@ func createInstance(t *testing.T, client *linodego.Client, enableCloudFirewall b
if t != nil {
t.Helper()
}
booted := false

createOpts := linodego.InstanceCreateOptions{
Label: "go-test-ins-" + randLabel(),
RootPass: randPassword(),
Region: getRegionsWithCaps(t, client, []string{"linodes"})[0],
Type: "g6-nanode-1",
Image: "linode/debian9",
Booted: &booted,
Booted: linodego.Pointer(false),
}

if enableCloudFirewall {
Expand Down Expand Up @@ -565,12 +565,11 @@ func createInstanceWithoutDisks(
) (*linodego.Instance, *linodego.InstanceConfig, func(), error) {
t.Helper()

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

if enableCloudFirewall {
Expand Down
8 changes: 2 additions & 6 deletions test/integration/waitfor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ func TestEventPoller_InstancePower(t *testing.T) {
t.Fatalf("failed to initialize event poller: %s", err)
}

booted := false

instance, err := client.CreateInstance(context.Background(), linodego.InstanceCreateOptions{
Region: getRegionsWithCaps(t, client, []string{"Linodes"})[0],
Type: "g6-nanode-1",
Image: "linode/ubuntu22.04",
RootPass: randPassword(),
Label: "go-ins-poll-test",
Booted: &booted,
Booted: linodego.Pointer(false),
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -146,13 +144,11 @@ func TestEventPoller_Secondary(t *testing.T) {
t.Fatalf("failed to initialize event poller: %s", err)
}

booted := false

instance, err := client.CreateInstance(context.Background(), linodego.InstanceCreateOptions{
Region: getRegionsWithCaps(t, client, []string{"Linodes"})[0],
Type: "g6-nanode-1",
Label: "go-ins-poll-test",
Booted: &booted,
Booted: linodego.Pointer(false),
})
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 1328865

Please sign in to comment.