Skip to content

Commit 776ec88

Browse files
committed
feat: uint support
Adds in support for uint Signed-off-by: Jason Field <jason@avon-lea.co.uk>
1 parent 560bbd6 commit 776ec88

File tree

12 files changed

+551
-2
lines changed

12 files changed

+551
-2
lines changed

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.PHONY: all build test clean
2+
3+
all: test build
4+
5+
build:
6+
@echo "Building the project..."
7+
go build github.com/xorima/pointerhelpers
8+
9+
test:
10+
@echo "Running tests..."
11+
go test ./... --race
12+
13+
clean:
14+
@echo "Cleaning up..."
15+
go clean

README.md

Lines changed: 246 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,247 @@
1-
# pointer helpers
1+
# pointerhelpers
22

3-
Go repo to hold all the helpers for pointers to save rewriting the same code every time
3+
`pointerhelpers` is a Go package designed to reduce boilerplate code when working with pointers. This package provides helper functions for various data types, including `bool`, `int` (and its variations), `float` (and its variations), `complex` (and its variations), and `string`. By using this package, you can streamline your code and enhance readability.
4+
5+
## Installation
6+
7+
To install the `pointerhelpers` package, use the following command:
8+
9+
```bash
10+
go get github.com/xorima/pointerhelpers
11+
```
12+
13+
## Usage
14+
15+
The `pointerhelpers` package provides functions to obtain pointers to values and to safely retrieve values from pointers. Below are the examples for each supported type:
16+
17+
### Boolean
18+
19+
```go
20+
package main
21+
22+
import (
23+
"fmt"
24+
"github.com/xorima/pointerhelpers"
25+
)
26+
27+
func main() {
28+
b := true
29+
bPtr := pointerhelpers.Bool(b)
30+
fmt.Println(*bPtr) // Output: true
31+
32+
nilBoolPtr := (*bool)(nil)
33+
fmt.Println(pointerhelpers.BoolValue(nilBoolPtr)) // Output: false
34+
}
35+
```
36+
37+
### Integers
38+
39+
#### int
40+
41+
```go
42+
package main
43+
44+
import (
45+
"fmt"
46+
"github.com/xorima/pointerhelpers"
47+
)
48+
49+
func main() {
50+
i := 42
51+
iPtr := pointerhelpers.Int(i)
52+
fmt.Println(*iPtr) // Output: 42
53+
54+
nilIntPtr := (*int)(nil)
55+
fmt.Println(pointerhelpers.IntValue(nilIntPtr)) // Output: 0
56+
}
57+
```
58+
59+
#### Other integer types
60+
61+
The package supports other integer types similarly: `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`.
62+
63+
### Floating Point Numbers
64+
65+
#### float32
66+
67+
```go
68+
package main
69+
70+
import (
71+
"fmt"
72+
"github.com/xorima/pointerhelpers"
73+
)
74+
75+
func main() {
76+
f := float32(3.14)
77+
fPtr := pointerhelpers.Float32(f)
78+
fmt.Println(*fPtr) // Output: 3.14
79+
80+
nilFloat32Ptr := (*float32)(nil)
81+
fmt.Println(pointerhelpers.Float32Value(nilFloat32Ptr)) // Output: 0
82+
}
83+
```
84+
85+
#### float64
86+
87+
```go
88+
package main
89+
90+
import (
91+
"fmt"
92+
"github.com/xorima/pointerhelpers"
93+
)
94+
95+
func main() {
96+
f := 3.14
97+
fPtr := pointerhelpers.Float64(f)
98+
fmt.Println(*fPtr) // Output: 3.14
99+
100+
nilFloat64Ptr := (*float64)(nil)
101+
fmt.Println(pointerhelpers.Float64Value(nilFloat64Ptr)) // Output: 0
102+
}
103+
```
104+
105+
### Complex Numbers
106+
107+
#### complex64
108+
109+
```go
110+
package main
111+
112+
import (
113+
"fmt"
114+
"github.com/xorima/pointerhelpers"
115+
)
116+
117+
func main() {
118+
c := complex64(1 + 2i)
119+
cPtr := pointerhelpers.Complex64(c)
120+
fmt.Println(*cPtr) // Output: (1+2i)
121+
122+
nilComplex64Ptr := (*complex64)(nil)
123+
fmt.Println(pointerhelpers.Complex64Value(nilComplex64Ptr)) // Output: (0+0i)
124+
}
125+
```
126+
127+
#### complex128
128+
129+
```go
130+
package main
131+
132+
import (
133+
"fmt"
134+
"github.com/xorima/pointerhelpers"
135+
)
136+
137+
func main() {
138+
c := complex(1 + 2i)
139+
cPtr := pointerhelpers.Complex128(c)
140+
fmt.Println(*cPtr) // Output: (1+2i)
141+
142+
nilComplex128Ptr := (*complex128)(nil)
143+
fmt.Println(pointerhelpers.Complex128Value(nilComplex128Ptr)) // Output: (0+0i)
144+
}
145+
```
146+
147+
#### Embedding Complex64Helper
148+
149+
For users who want to embed `Complex64Helper` into their custom types to gain the pointer helper functionalities directly, the package provides the `Complex64Helper` struct.
150+
151+
```go
152+
package main
153+
154+
import (
155+
"fmt"
156+
"github.com/xorima/pointerhelpers"
157+
)
158+
159+
type MyStruct struct {
160+
pointerhelpers.Complex64Helper
161+
}
162+
163+
func main() {
164+
s := MyStruct{}
165+
c := complex64(1 + 2i)
166+
cPtr := s.Complex64(c)
167+
fmt.Println(*cPtr) // Output: (1+2i)
168+
169+
nilComplex64Ptr := (*complex64)(nil)
170+
fmt.Println(s.Complex64Value(nilComplex64Ptr)) // Output: (0+0i)
171+
}
172+
```
173+
174+
### String
175+
176+
```go
177+
package main
178+
179+
import (
180+
"fmt"
181+
"github.com/xorima/pointerhelpers"
182+
)
183+
184+
func main() {
185+
s := "hello"
186+
sPtr := pointerhelpers.String(s)
187+
fmt.Println(*sPtr) // Output: hello
188+
189+
nilStringPtr := (*string)(nil)
190+
fmt.Println(pointerhelpers.StringValue(nilStringPtr)) // Output: ""
191+
}
192+
```
193+
194+
## Full API Reference
195+
196+
### Functions
197+
198+
- `Bool(v bool) *bool`
199+
- `BoolValue(v *bool) bool`
200+
- `Int(v int) *int`
201+
- `IntValue(v *int) int`
202+
- `Int8(v int8) *int8`
203+
- `Int8Value(v *int8) int8`
204+
- `Int16(v int16) *int16`
205+
- `Int16Value(v *int16) int16`
206+
- `Int32(v int32) *int32`
207+
- `Int32Value(v *int32) int32`
208+
- `Int64(v int64) *int64`
209+
- `Int64Value(v *int64) int64`
210+
- `Uint(v uint) *uint`
211+
- `UintValue(v *uint) uint`
212+
- `Uint8(v uint8) *uint8`
213+
- `Uint8Value(v *uint8) uint8`
214+
- `Uint16(v uint16) *uint16`
215+
- `Uint16Value(v *uint16) uint16`
216+
- `Uint32(v uint32) *uint32`
217+
- `Uint32Value(v *uint32) uint32`
218+
- `Uint64(v uint64) *uint64`
219+
- `Uint64Value(v *uint64) uint64`
220+
- `Float32(v float32) *float32`
221+
- `Float32Value(v *float32) float32`
222+
- `Float64(v float64) *float64`
223+
- `Float64Value(v *float64) float64`
224+
- `Complex64(v complex64) *complex64`
225+
- `Complex64Value(v *complex64) complex64`
226+
- `Complex128(v complex128) *complex128`
227+
- `Complex128Value(v *complex128) complex128`
228+
- `String(v string) *string`
229+
- `StringValue(v *string) string`
230+
231+
### Structs
232+
233+
- `Complex64Helper`
234+
235+
The `Complex64Helper` struct can be embedded into your custom types to easily incorporate complex64 pointer helper methods.
236+
237+
## Contributing
238+
239+
Contributions are welcome! Please open an issue or submit a pull request with any improvements or bug fixes.
240+
241+
## License
242+
243+
This project is licensed under the MIT License - see the LICENSE file for details.
244+
245+
---
246+
247+
This README now includes information about the `Complex64Helper` struct and its usage. Feel free to customize it further based on your specific requirements.

uint.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pointerhelpers
2+
3+
// UintHelper contains all Uint related
4+
// pointer helpers
5+
type UintHelper struct {
6+
}
7+
8+
// Uint returns a pointer to the uint value passed in.
9+
func Uint(v uint) *uint {
10+
return &v
11+
}
12+
13+
// Uint returns a pointer to the uint value passed in.
14+
func (i *UintHelper) Uint(v uint) *uint {
15+
return Uint(v)
16+
}
17+
18+
// UintValue returns the value of the uint pointer passed in or
19+
// 0 if the pointer is nil.
20+
func UintValue(v *uint) uint {
21+
if v != nil {
22+
return *v
23+
}
24+
return 0
25+
}
26+
27+
// UintValue returns the value of the uint pointer passed in or
28+
// 0 if the pointer is nil.
29+
func (i *UintHelper) UintValue(v *uint) uint {
30+
return UintValue(v)
31+
}

uint16.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pointerhelpers
2+
3+
// Uint16Helper contains all Uint16 related
4+
// pointer helpers
5+
type Uint16Helper struct {
6+
}
7+
8+
// Uint16 returns a pointer to the uint16 value passed in.
9+
func Uint16(v uint16) *uint16 {
10+
return &v
11+
}
12+
13+
// Uint16 returns a pointer to the uint16 value passed in.
14+
func (i *Uint16Helper) Uint16(v uint16) *uint16 {
15+
return Uint16(v)
16+
}
17+
18+
// Uint16Value returns the value of the uint16 pointer passed in or
19+
// 0 if the pointer is nil.
20+
func Uint16Value(v *uint16) uint16 {
21+
if v != nil {
22+
return *v
23+
}
24+
return 0
25+
}
26+
27+
// Uint16Value returns the value of the uint16 pointer passed in or
28+
// 0 if the pointer is nil.
29+
func (i *Uint16Helper) Uint16Value(v *uint16) uint16 {
30+
return Uint16Value(v)
31+
}

uint16_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pointerhelpers
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestUint16(t *testing.T) {
9+
h := Uint16Helper{}
10+
want := uint16(42)
11+
got := h.Uint16(want)
12+
assert.EqualValues(t, want, *got)
13+
}
14+
15+
func TestUint16Value(t *testing.T) {
16+
t.Run("when nil returns 0", func(t *testing.T) {
17+
h := Uint16Helper{}
18+
got := h.Uint16Value(nil)
19+
assert.Equal(t, uint16(0), got)
20+
})
21+
t.Run("when set returns the given uint16", func(t *testing.T) {
22+
h := Uint16Helper{}
23+
want := h.Uint16(uint16(42))
24+
got := h.Uint16Value(want)
25+
assert.Equal(t, *want, got)
26+
})
27+
}

uint32.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pointerhelpers
2+
3+
// Uint32Helper contains all Uint32 related
4+
// pointer helpers
5+
type Uint32Helper struct {
6+
}
7+
8+
// Uint32 returns a pointer to the uint32 value passed in.
9+
func Uint32(v uint32) *uint32 {
10+
return &v
11+
}
12+
13+
// Uint32 returns a pointer to the uint32 value passed in.
14+
func (i *Uint32Helper) Uint32(v uint32) *uint32 {
15+
return Uint32(v)
16+
}
17+
18+
// Uint32Value returns the value of the uint32 pointer passed in or
19+
// 0 if the pointer is nil.
20+
func Uint32Value(v *uint32) uint32 {
21+
if v != nil {
22+
return *v
23+
}
24+
return 0
25+
}
26+
27+
// Uint32Value returns the value of the uint32 pointer passed in or
28+
// 0 if the pointer is nil.
29+
func (i *Uint32Helper) Uint32Value(v *uint32) uint32 {
30+
return Uint32Value(v)
31+
}

0 commit comments

Comments
 (0)