Skip to content

Commit

Permalink
feat: uint support
Browse files Browse the repository at this point in the history
Adds in support for uint

Signed-off-by: Jason Field <jason@avon-lea.co.uk>
  • Loading branch information
xorima committed Jun 10, 2024
1 parent 560bbd6 commit 776ec88
Show file tree
Hide file tree
Showing 12 changed files with 551 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: all build test clean

all: test build

build:
@echo "Building the project..."
go build github.com/xorima/pointerhelpers

test:
@echo "Running tests..."
go test ./... --race

clean:
@echo "Cleaning up..."
go clean
248 changes: 246 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,247 @@
# pointer helpers
# pointerhelpers

Go repo to hold all the helpers for pointers to save rewriting the same code every time
`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.

## Installation

To install the `pointerhelpers` package, use the following command:

```bash
go get github.com/xorima/pointerhelpers
```

## Usage

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:

### Boolean

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

func main() {
b := true
bPtr := pointerhelpers.Bool(b)
fmt.Println(*bPtr) // Output: true

nilBoolPtr := (*bool)(nil)
fmt.Println(pointerhelpers.BoolValue(nilBoolPtr)) // Output: false
}
```

### Integers

#### int

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

func main() {
i := 42
iPtr := pointerhelpers.Int(i)
fmt.Println(*iPtr) // Output: 42

nilIntPtr := (*int)(nil)
fmt.Println(pointerhelpers.IntValue(nilIntPtr)) // Output: 0
}
```

#### Other integer types

The package supports other integer types similarly: `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`.

### Floating Point Numbers

#### float32

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

func main() {
f := float32(3.14)
fPtr := pointerhelpers.Float32(f)
fmt.Println(*fPtr) // Output: 3.14

nilFloat32Ptr := (*float32)(nil)
fmt.Println(pointerhelpers.Float32Value(nilFloat32Ptr)) // Output: 0
}
```

#### float64

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

func main() {
f := 3.14
fPtr := pointerhelpers.Float64(f)
fmt.Println(*fPtr) // Output: 3.14

nilFloat64Ptr := (*float64)(nil)
fmt.Println(pointerhelpers.Float64Value(nilFloat64Ptr)) // Output: 0
}
```

### Complex Numbers

#### complex64

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

func main() {
c := complex64(1 + 2i)
cPtr := pointerhelpers.Complex64(c)
fmt.Println(*cPtr) // Output: (1+2i)

nilComplex64Ptr := (*complex64)(nil)
fmt.Println(pointerhelpers.Complex64Value(nilComplex64Ptr)) // Output: (0+0i)
}
```

#### complex128

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

func main() {
c := complex(1 + 2i)
cPtr := pointerhelpers.Complex128(c)
fmt.Println(*cPtr) // Output: (1+2i)

nilComplex128Ptr := (*complex128)(nil)
fmt.Println(pointerhelpers.Complex128Value(nilComplex128Ptr)) // Output: (0+0i)
}
```

#### Embedding Complex64Helper

For users who want to embed `Complex64Helper` into their custom types to gain the pointer helper functionalities directly, the package provides the `Complex64Helper` struct.

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

type MyStruct struct {
pointerhelpers.Complex64Helper
}

func main() {
s := MyStruct{}
c := complex64(1 + 2i)
cPtr := s.Complex64(c)
fmt.Println(*cPtr) // Output: (1+2i)

nilComplex64Ptr := (*complex64)(nil)
fmt.Println(s.Complex64Value(nilComplex64Ptr)) // Output: (0+0i)
}
```

### String

```go
package main

import (
"fmt"
"github.com/xorima/pointerhelpers"
)

func main() {
s := "hello"
sPtr := pointerhelpers.String(s)
fmt.Println(*sPtr) // Output: hello

nilStringPtr := (*string)(nil)
fmt.Println(pointerhelpers.StringValue(nilStringPtr)) // Output: ""
}
```

## Full API Reference

### Functions

- `Bool(v bool) *bool`
- `BoolValue(v *bool) bool`
- `Int(v int) *int`
- `IntValue(v *int) int`
- `Int8(v int8) *int8`
- `Int8Value(v *int8) int8`
- `Int16(v int16) *int16`
- `Int16Value(v *int16) int16`
- `Int32(v int32) *int32`
- `Int32Value(v *int32) int32`
- `Int64(v int64) *int64`
- `Int64Value(v *int64) int64`
- `Uint(v uint) *uint`
- `UintValue(v *uint) uint`
- `Uint8(v uint8) *uint8`
- `Uint8Value(v *uint8) uint8`
- `Uint16(v uint16) *uint16`
- `Uint16Value(v *uint16) uint16`
- `Uint32(v uint32) *uint32`
- `Uint32Value(v *uint32) uint32`
- `Uint64(v uint64) *uint64`
- `Uint64Value(v *uint64) uint64`
- `Float32(v float32) *float32`
- `Float32Value(v *float32) float32`
- `Float64(v float64) *float64`
- `Float64Value(v *float64) float64`
- `Complex64(v complex64) *complex64`
- `Complex64Value(v *complex64) complex64`
- `Complex128(v complex128) *complex128`
- `Complex128Value(v *complex128) complex128`
- `String(v string) *string`
- `StringValue(v *string) string`

### Structs

- `Complex64Helper`

The `Complex64Helper` struct can be embedded into your custom types to easily incorporate complex64 pointer helper methods.

## Contributing

Contributions are welcome! Please open an issue or submit a pull request with any improvements or bug fixes.

## License

This project is licensed under the MIT License - see the LICENSE file for details.

---

This README now includes information about the `Complex64Helper` struct and its usage. Feel free to customize it further based on your specific requirements.
31 changes: 31 additions & 0 deletions uint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pointerhelpers

// UintHelper contains all Uint related
// pointer helpers
type UintHelper struct {
}

// Uint returns a pointer to the uint value passed in.
func Uint(v uint) *uint {
return &v
}

// Uint returns a pointer to the uint value passed in.
func (i *UintHelper) Uint(v uint) *uint {
return Uint(v)
}

// UintValue returns the value of the uint pointer passed in or
// 0 if the pointer is nil.
func UintValue(v *uint) uint {
if v != nil {
return *v
}
return 0
}

// UintValue returns the value of the uint pointer passed in or
// 0 if the pointer is nil.
func (i *UintHelper) UintValue(v *uint) uint {
return UintValue(v)
}
31 changes: 31 additions & 0 deletions uint16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pointerhelpers

// Uint16Helper contains all Uint16 related
// pointer helpers
type Uint16Helper struct {
}

// Uint16 returns a pointer to the uint16 value passed in.
func Uint16(v uint16) *uint16 {
return &v
}

// Uint16 returns a pointer to the uint16 value passed in.
func (i *Uint16Helper) Uint16(v uint16) *uint16 {
return Uint16(v)
}

// Uint16Value returns the value of the uint16 pointer passed in or
// 0 if the pointer is nil.
func Uint16Value(v *uint16) uint16 {
if v != nil {
return *v
}
return 0
}

// Uint16Value returns the value of the uint16 pointer passed in or
// 0 if the pointer is nil.
func (i *Uint16Helper) Uint16Value(v *uint16) uint16 {
return Uint16Value(v)
}
27 changes: 27 additions & 0 deletions uint16_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pointerhelpers

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestUint16(t *testing.T) {
h := Uint16Helper{}
want := uint16(42)
got := h.Uint16(want)
assert.EqualValues(t, want, *got)
}

func TestUint16Value(t *testing.T) {
t.Run("when nil returns 0", func(t *testing.T) {
h := Uint16Helper{}
got := h.Uint16Value(nil)
assert.Equal(t, uint16(0), got)
})
t.Run("when set returns the given uint16", func(t *testing.T) {
h := Uint16Helper{}
want := h.Uint16(uint16(42))
got := h.Uint16Value(want)
assert.Equal(t, *want, got)
})
}
31 changes: 31 additions & 0 deletions uint32.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pointerhelpers

// Uint32Helper contains all Uint32 related
// pointer helpers
type Uint32Helper struct {
}

// Uint32 returns a pointer to the uint32 value passed in.
func Uint32(v uint32) *uint32 {
return &v
}

// Uint32 returns a pointer to the uint32 value passed in.
func (i *Uint32Helper) Uint32(v uint32) *uint32 {
return Uint32(v)
}

// Uint32Value returns the value of the uint32 pointer passed in or
// 0 if the pointer is nil.
func Uint32Value(v *uint32) uint32 {
if v != nil {
return *v
}
return 0
}

// Uint32Value returns the value of the uint32 pointer passed in or
// 0 if the pointer is nil.
func (i *Uint32Helper) Uint32Value(v *uint32) uint32 {
return Uint32Value(v)
}
Loading

0 comments on commit 776ec88

Please sign in to comment.