File: pointer_helpers.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (51 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
}

// DoublePointer creates a double pointer to a value of type T.
//
// This is useful for APIs that distinguish between null and omitted fields.
//
// Example:
//
//	// For a field that should be non-null value in the API payload:
//	value := linodego.DoublePointer(42) // Returns **int pointing a *int pointer pointing to 42
//
//	// For a field that should be null in the API payload, use `DoublePointerNull` function instead:
//	nullValue := linodego.DoublePointerNull[int]() // Returns **int that is nil
//
//	// For a field that should not be included in the API payload, simply not include it in the struct.
func DoublePointer[T any](value T) **T {
	valuePtr := &value
	return &valuePtr
}

// DoublePointerNull creates a double pointer pointing to a nil pointer of type T,
// indicating that the field should be null in the API payload.
//
// This is useful for APIs that distinguish between null and omitted fields.
func DoublePointerNull[T any]() **T {
	return Pointer[*T](nil)
}