pointy

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 0 Imported by: 13

README

pointy

Simple helper functions to provide a shorthand to get a pointer to a variable holding a constant...because it's annoying when you have to do it hundreds of times in unit tests:


val := 42
pointerToVal := &val
// vs.
pointerToVal := pointy.Int(42) // if using Go 1.17 or earlier w/o generics
pointerToVal := pointy.Pointer(42) // if using Go 1.18+ w/ generics
New in release 2.0.0

🚨 Breaking change

Package has changed to go.openly.dev. Please use

import "go.openly.dev/pointy"
New in release 1.2.0

Generic implementation of the pointer-to-value and value-to-pointer functions. Requires Go 1.18+. The type-specific functions are still available for backwards-compatibility.

pointerToInt := pointy.Pointer(42) 
pointerToString := pointy.Pointer("foo") 
// then later in your code..
intValue := pointy.PointerValue(pointerToInt, 99) 
stringValue := pointy.PointerValue(pointerToString, "bar") 

Convenience functions to safely compare pointers by their dereferenced values:

// when both values are pointers
a := pointy.Int(1)
b := pointy.Int(1)
if pointy.PointersValueEqual(a, b) {
	fmt.Println("a and b contain equal dereferenced values")
}

// or if just one is a pointer
a := pointy.Int(1)
b := 1
if pointy.PointerValueEqual(a, b) {
	fmt.Println("a and b contain equal dereferenced values")
}
New in release 1.1.0

Additional helper functions have been added to safely dereference pointers or return a fallback value:

val := 42
pointerToVal := &val
// then later in your code..
myVal := pointy.IntValue(pointerToVal, 99) // returns 42 (or 99 if pointerToVal was nil)

GoDoc

https://godoc.org/github.com/openly-engineering/pointy

Installation

go get go.openly.dev/pointy

Example

package main

import (
	"fmt"

	"go.openly.dev/pointy"
)

func main() {
	foo := pointy.Pointer(2018)
	fmt.Println("foo is a pointer to:", *foo)

	bar := pointy.Pointer("point to me")
	fmt.Println("bar is a pointer to:", *bar)

	// get the value back out (new in v1.1.0)
	barVal := pointy.PointerValue(bar, "empty!")
	fmt.Println("bar's value is:", barVal)
}

Available Functions

Pointer[T any](x T) *T
PointerValue[T any](p *T, fallback T) T
Bool(x bool) *bool
BoolValue(p *bool, fallback bool) bool
Byte(x byte) *byte
ByteValue(p *byte, fallback byte) byte
Complex128(x complex128) *complex128
Complex128Value(p *complex128, fallback complex128) complex128
Complex64(x complex64) *complex64
Complex64Value(p *complex64, fallback complex64) complex64
Float32(x float32) *float32
Float32Value(p *float32, fallback float32) float32
Float64(x float64) *float64
Float64Value(p *float64, fallback float64) float64
Int(x int) *int
IntValue(p *int, fallback int) int
Int8(x int8) *int8
Int8Value(p *int8, fallback int8) int8
Int16(x int16) *int16
Int16Value(p *int16, fallback int16) int16
Int32(x int32) *int32
Int32Value(p *int32, fallback int32) int32
Int64(x int64) *int64
Int64Value(p *int64, fallback int64) int64
Uint(x uint) *uint
UintValue(p *uint, fallback uint) uint
Uint8(x uint8) *uint8
Uint8Value(p *uint8, fallback uint8) uint8
Uint16(x uint16) *uint16
Uint16Value(p *uint16, fallback uint16) uint16
Uint32(x uint32) *uint32
Uint32Value(p *uint32, fallback uint32) uint32
Uint64(x uint64) *uint64
Uint64Value(p *uint64, fallback uint64) uint64
String(x string) *string
StringValue(p *string, fallback string) string
Rune(x rune) *rune
RuneValue(p *rune, fallback rune) rune
PointersValueEqual[T comparable](a *T, b *T) bool
PointerValueEqual[T comparable](a *T, b T) bool

Motivation

Creating pointers to literal constant values is useful, especially in unit tests. Go doesn't support simply using the address operator (&) to reference the location of e.g. value := &int64(42) so we're forced to create little workarounds. A common solution is to create a helper function:

func createInt64Pointer(x int64) *int64 {
    return &x
}
// now you can create a pointer to 42 inline
value := createInt64Pointer(42)

This package provides a library of these simple little helper functions for every native Go primitive.

Made @ Openly. Join us and use Go to build cool stuff.

Documentation

Overview

Package pointy is a set of simple helper functions to provide a shorthand to get a pointer to a variable holding a constant.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(x bool) *bool

Bool returns a pointer to a variable holding the supplied bool constant

func BoolValue

func BoolValue(p *bool, fallback bool) bool

BoolValue returns the bool value pointed to by p or fallback if p is nil

func Byte

func Byte(x byte) *byte

Byte returns a pointer to a variable holding the supplied byte constant

func ByteValue

func ByteValue(p *byte, fallback byte) byte

ByteValue returns the byte value pointed to by p or fallback if p is nil

func Complex128

func Complex128(x complex128) *complex128

Complex128 returns a pointer to a variable holding the supplied complex128 constant

func Complex128Value

func Complex128Value(p *complex128, fallback complex128) complex128

Complex128Value returns the complex128 value pointed to by p or fallback if p is nil

func Complex64

func Complex64(x complex64) *complex64

Complex64 returns a pointer to a variable holding the supplied complex64 constant

func Complex64Value

func Complex64Value(p *complex64, fallback complex64) complex64

Complex64Value returns the complex64 value pointed to by p or fallback if p is nil

func Float32

func Float32(x float32) *float32

Float32 returns a pointer to a variable holding the supplied float32 constant

func Float32Value

func Float32Value(p *float32, fallback float32) float32

Float32Value returns the float32 value pointed to by p or fallback if p is nil

func Float64

func Float64(x float64) *float64

Float64 returns a pointer to a variable holding the supplied float64 constant

func Float64Value

func Float64Value(p *float64, fallback float64) float64

Float64Value returns the float64 value pointed to by p or fallback if p is nil

func Int

func Int(x int) *int

Int returns a pointer to a variable holding the supplied int constant

func Int16

func Int16(x int16) *int16

Int16 returns a pointer to a variable holding the supplied int16 constant

func Int16Value

func Int16Value(p *int16, fallback int16) int16

Int16Value returns the int16 value pointed to by p or fallback if p is nil

func Int32

func Int32(x int32) *int32

Int32 returns a pointer to a variable holding the supplied int32 constant

func Int32Value

func Int32Value(p *int32, fallback int32) int32

Int32Value returns the int32 value pointed to by p or fallback if p is nil

func Int64

func Int64(x int64) *int64

Int64 returns a pointer to a variable holding the supplied int64 constant

Example

This example returns a pointer to a variable holding the `int64` constant `2018`.

package main

import (
	"fmt"

	"go.openly.dev/pointy"
)

func main() {
	foo := pointy.Int64(2018)
	fmt.Println("foo contains value:", *foo)
}
Output:

func Int64Value

func Int64Value(p *int64, fallback int64) int64

Int64Value returns the int64 value pointed to by p or fallback if p is nil

func Int8

func Int8(x int8) *int8

Int8 returns a pointer to a variable holding the supplied int8 constant

func Int8Value

func Int8Value(p *int8, fallback int8) int8

Int8Value returns the int8 value pointed to by p or fallback if p is nil

func IntValue

func IntValue(p *int, fallback int) int

IntValue returns the int value pointed to by p or fallback if p is nil

func Pointer

func Pointer[T any](x T) *T

Pointer returns a pointer to a variable holding the supplied T constant

Example

This example uses the new generic construct to returns a pointer to a variable holding the `string` constant "point to me".

package main

import (
	"fmt"

	"go.openly.dev/pointy"
)

func main() {
	foo := pointy.Pointer("point to me")
	fmt.Println("foo contains value:", *foo)
}
Output:

func PointerValue

func PointerValue[T any](p *T, fallback T) T

PointerValue returns the T value pointed to by p or fallback if p is nil

func PointerValueEqual

func PointerValueEqual[T comparable](a *T, b T) bool

PointerValueEqual returns true if the pointer parameter is not nil and contains the same dereferenced value as the value parameter.

Example

This example demonstrates comparing a pointer to a value using dereferenced value equality.

package main

import (
	"fmt"

	"go.openly.dev/pointy"
)

func main() {
	a := pointy.Int(1)
	b := 1
	if pointy.PointerValueEqual(a, b) {
		fmt.Println("a and b contain equal dereferenced values")
	}
}
Output:

func PointersValueEqual

func PointersValueEqual[T comparable](a *T, b *T) bool

PointersValueEqual returns true if both pointer parameters are nil or contain the same dereferenced value.

Example

This example demonstrates comparing two pointers using dereferenced value equality.

package main

import (
	"fmt"

	"go.openly.dev/pointy"
)

func main() {
	a := pointy.Int(1)
	b := pointy.Int(1)
	if pointy.PointersValueEqual(a, b) {
		fmt.Println("a and b contain equal dereferenced values")
	}
}
Output:

func Rune

func Rune(x rune) *rune

Rune returns a pointer to a variable holding the supplied rune constant

func RuneValue

func RuneValue(p *rune, fallback rune) rune

RuneValue returns the rune value pointed to by p or fallback if p is nil

func String

func String(x string) *string

String returns a pointer to a variable holding the supplied string constant

Example

This example returns a pointer to a variable holding the `string` constant "point to me".

package main

import (
	"fmt"

	"go.openly.dev/pointy"
)

func main() {
	bar := pointy.String("point to me")
	fmt.Println("bar contains value:", *bar)
}
Output:

func StringValue

func StringValue(p *string, fallback string) string

StringValue returns the string value pointed to by p or fallback if p is nil

func Uint

func Uint(x uint) *uint

Uint returns a pointer to a variable holding the supplied uint constant

func Uint16

func Uint16(x uint16) *uint16

Uint16 returns a pointer to a variable holding the supplied uint16 constant

func Uint16Value

func Uint16Value(p *uint16, fallback uint16) uint16

Uint16Value returns the uint16 value pointed to by p or fallback if p is nil

func Uint32

func Uint32(x uint32) *uint32

Uint32 returns a pointer to a variable holding the supplied uint32 constant

func Uint32Value

func Uint32Value(p *uint32, fallback uint32) uint32

Uint32Value returns the uint32 value pointed to by p or fallback if p is nil

func Uint64

func Uint64(x uint64) *uint64

Uint64 returns a pointer to a variable holding the supplied uint64 constant

func Uint64Value

func Uint64Value(p *uint64, fallback uint64) uint64

Uint64Value returns the uint64 value pointed to by p or fallback if p is nil

func Uint8

func Uint8(x uint8) *uint8

Uint8 returns a pointer to a variable holding the supplied uint8 constant

func Uint8Value

func Uint8Value(p *uint8, fallback uint8) uint8

Uint8Value returns the uint8 value pointed to by p or fallback if p is nil

func UintValue

func UintValue(p *uint, fallback uint) uint

UintValue returns the uint value pointed to by p or fallback if p is nil

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL