diff --git a/pointer_helpers.go b/pointer_helpers.go new file mode 100644 index 000000000..a6045fdf0 --- /dev/null +++ b/pointer_helpers.go @@ -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 +} diff --git a/pointer_helpers_test.go b/pointer_helpers_test.go new file mode 100644 index 000000000..c7ddc6733 --- /dev/null +++ b/pointer_helpers_test.go @@ -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) + } +} diff --git a/test/integration/example_nodebalancers_test.go b/test/integration/example_nodebalancers_test.go index 55aa279d4..03712ee10 100644 --- a/test/integration/example_nodebalancers_test.go +++ b/test/integration/example_nodebalancers_test.go @@ -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) diff --git a/test/integration/instances_test.go b/test/integration/instances_test.go index 5bd41062e..bb88015b5 100644 --- a/test/integration/instances_test.go +++ b/test/integration/instances_test.go @@ -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 { @@ -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 { diff --git a/test/integration/waitfor_test.go b/test/integration/waitfor_test.go index 8775598ea..244b220f6 100644 --- a/test/integration/waitfor_test.go +++ b/test/integration/waitfor_test.go @@ -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) @@ -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)