Skip to content

Commit

Permalink
implement
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Jan 19, 2023
1 parent a0aaa2b commit c36a366
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 15 deletions.
17 changes: 2 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
.idea/
vendor/
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# ptr
Simple pointer helpers for Go

[![Go Reference](https://pkg.go.dev/badge/github.com/candiduslynx/ptr.svg)](https://pkg.go.dev/github.com/candiduslynx/ptr)

## Obtaining pointer to the value

Simply pass the value to the `To` function, the type will be inferred by the Go compiler.

```go
p := ptr.To(float32(0.5)) // p is *float32 pointing to the value 0.5
```

## Retrieving value designated by pointer

To retrieve the value designated by pointer simply pass the pointer to `From` function.
If the pointer might be `nil` you can supply optional default value as well.
if the pointer is nil and default value isn't provided, `From` will panic.

```go
// panics
_ = ptr.From((*float32)(nil))

var vv = floaat32(0.5)
value = ptr.From(&vv) // value = float32(0.5)
value = ptr.From((*float32)(nil), 0.7) // value = float32(0.7)
```
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/candiduslynx/ptr

go 1.18

require github.com/stretchr/testify v1.8.1

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 15 additions & 0 deletions ptr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Package ptr implements a simple pointer instrumentation.
// As it is based on Go generics, the minimal Go version is 1.18.
package ptr

// From will extract a value designated by pointer provider.
// It will panic if the pointer provided is nil and the default isn't supplied.
func From[T any](pointer *T, dflt ...T) T {
if pointer == (*T)(nil) && len(dflt) > 0 {
return dflt[0]
}
return *pointer
}

// To will return a link to the value.
func To[T any](value T) (pointer *T) { return &value }
39 changes: 39 additions & 0 deletions ptr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ptr_test

import (
"testing"
"time"

"github.com/candiduslynx/ptr"
"github.com/stretchr/testify/require"
)

func TestFrom(t *testing.T) {
require.IsType(t, 0, ptr.From(new(int)))
require.IsType(t, int32(0), ptr.From(new(int32)))
require.IsType(t, int64(0), ptr.From(new(int64)))
require.IsType(t, struct{}{}, ptr.From(new(struct{})))
require.IsType(t, time.Time{}, ptr.From(new(time.Time)))
require.IsType(t, new(time.Time), ptr.From(new(*time.Time)))
}

func TestFrom_Panics(t *testing.T) {
require.Panics(t, func() {
ptr.From((*float32)(nil))
})
}

func TestFrom_NotPanics(t *testing.T) {
require.NotPanics(t, func() {
ptr.From((*float32)(nil), 0)
})
}

func TestTo(t *testing.T) {
require.IsType(t, new(int), ptr.To(0))
require.IsType(t, new(int32), ptr.To(int32(0)))
require.IsType(t, new(int64), ptr.To(int64(0)))
require.IsType(t, new(struct{}), ptr.To(struct{}{}))
require.IsType(t, new(time.Time), ptr.To(time.Time{}))
require.IsType(t, new(*time.Time), ptr.To(new(time.Time)))
}

0 comments on commit c36a366

Please sign in to comment.