Skip to content

Commit 9f17fc9

Browse files
committed
pointer
1 parent 10fb484 commit 9f17fc9

5 files changed

Lines changed: 331 additions & 0 deletions

File tree

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Pointer
2+
3+
A tool, that helps to create pointers on the fly.
4+
5+
For APIs, tests, etc
6+
7+
Usage:
8+
9+
```go
10+
11+
import "github.com/spirin/pointer"
12+
13+
type Bar{}
14+
15+
func main() {
16+
pObj := pointer.Of(Bar{})
17+
18+
pString := pointer.Of("some string")
19+
20+
pIntA := pointer.Of(1)
21+
pIntB := pointer.Of(2)
22+
23+
if pointer.Equal(pIntA, pIntB){
24+
...
25+
}
26+
27+
if pointer.EqualValue(pIntA, 123){
28+
...
29+
}
30+
31+
fmt.Println(pointer.Value(pIntB, 123))
32+
33+
// types that cannot be autoresolved
34+
35+
pInt64 := pointer.Int64(1)
36+
37+
pFloat16 := pointer.Float16(1)
38+
39+
...
40+
}
41+
```

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/spirin/pointer
2+
3+
go 1.18
4+
5+
require github.com/stretchr/testify v1.8.4
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pointer.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package pointer
2+
3+
// any types
4+
5+
func Of[T any](v T) *T {
6+
return &v
7+
}
8+
9+
func Value[T any](v *T, def T) T {
10+
if v == nil {
11+
return def
12+
}
13+
14+
return *v
15+
}
16+
17+
func Equal[T comparable](a *T, b *T) bool {
18+
if a == nil && b == nil {
19+
return true
20+
}
21+
if a != nil && b != nil {
22+
return *a == *b
23+
}
24+
25+
return false
26+
}
27+
28+
func EqualValue[T comparable](p *T, v T) bool {
29+
if p == nil {
30+
return false
31+
}
32+
33+
return *p == v
34+
}
35+
36+
// non autoresolveable types
37+
38+
func Int8(v int8) *int8 {
39+
return Of(v)
40+
}
41+
42+
func Int16(v int16) *int16 {
43+
return Of(v)
44+
}
45+
46+
func Int32(v int32) *int32 {
47+
return Of(v)
48+
}
49+
50+
func Int64(v int64) *int64 {
51+
return Of(v)
52+
}
53+
54+
func UInt8(v uint8) *uint8 {
55+
return Of(v)
56+
}
57+
58+
func UInt16(v uint16) *uint16 {
59+
return Of(v)
60+
}
61+
62+
func UInt32(v uint32) *uint32 {
63+
return Of(v)
64+
}
65+
66+
func UInt64(v uint64) *uint64 {
67+
return Of(v)
68+
}
69+
70+
func Float32(v float32) *float32 {
71+
return Of(v)
72+
}
73+
74+
func Float64(v float64) *float64 {
75+
return Of(v)
76+
}
77+
78+
func Complex64(v complex64) *complex64 {
79+
return Of(v)
80+
}
81+
82+
func Complex128(v complex128) *complex128 {
83+
return Of(v)
84+
}

pointer_test.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package pointer
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestOf(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
target any
15+
}{
16+
{
17+
name: "string type",
18+
target: "test",
19+
},
20+
{
21+
name: "int type",
22+
target: 2,
23+
},
24+
{
25+
name: "time type",
26+
target: func() time.Time {
27+
date, err := time.Parse(time.RFC3339, "2022-01-01T12:30:00Z")
28+
require.NoError(t, err)
29+
return date
30+
}(),
31+
},
32+
{
33+
name: "uint32 type",
34+
target: uint32(4),
35+
},
36+
}
37+
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
result := Of(tt.target)
41+
require.True(t, reflect.ValueOf(result).Kind() == reflect.Ptr)
42+
})
43+
}
44+
}
45+
46+
func TestValue(t *testing.T) {
47+
var v = 12.5
48+
if got := Value(Of(v), 0); got != v {
49+
t.Errorf("Value() = %v, want %v", got, &v)
50+
}
51+
}
52+
53+
func TestValueWithDefault(t *testing.T) {
54+
var v = 12.5
55+
if got := Value(nil, v); got != v {
56+
t.Errorf("Value() = %v, want %v", got, &v)
57+
}
58+
}
59+
60+
func TestEqual(t *testing.T) {
61+
testCases := []struct {
62+
name string
63+
a *int
64+
b *int
65+
want bool
66+
}{
67+
{name: "a, b nil", want: true},
68+
{name: "a nill, b not", b: Of(1), want: false},
69+
{name: "a not, b nil", a: Of(1), want: false},
70+
{name: "a, b not equal", a: Of(1), b: Of(2), want: false},
71+
{name: "a, b equal", a: Of(1), b: Of(1), want: true},
72+
}
73+
for _, tt := range testCases {
74+
t.Run(tt.name, func(t *testing.T) {
75+
if got := Equal(tt.a, tt.b); got != tt.want {
76+
t.Errorf("Equal() = %v, want %v", got, tt.want)
77+
}
78+
})
79+
}
80+
}
81+
82+
func TestEqualValue(t *testing.T) {
83+
testCases := []struct {
84+
name string
85+
p *int
86+
v int
87+
want bool
88+
}{
89+
{name: "p nil", v: 123, want: false},
90+
{name: "p not nil, not eq", p: Of(999), v: 123, want: false},
91+
{name: "p not nil, eq", p: Of(123), v: 123, want: true},
92+
}
93+
94+
for _, tt := range testCases {
95+
t.Run(tt.name, func(t *testing.T) {
96+
if got := EqualValue(tt.p, tt.v); got != tt.want {
97+
t.Errorf("Equal() = %v, want %v", got, tt.want)
98+
}
99+
})
100+
}
101+
}
102+
103+
func TestInt8(t *testing.T) {
104+
var v = int8(1)
105+
if got := Int8(v); !reflect.DeepEqual(got, &v) {
106+
t.Errorf("Int8() = %v, want %v", got, &v)
107+
}
108+
}
109+
110+
func TestInt16(t *testing.T) {
111+
var v = int16(1)
112+
if got := Int16(v); !reflect.DeepEqual(got, &v) {
113+
t.Errorf("Int16() = %v, want %v", got, &v)
114+
}
115+
}
116+
117+
func TestInt32(t *testing.T) {
118+
var v = int32(1)
119+
if got := Int32(v); !reflect.DeepEqual(got, &v) {
120+
t.Errorf("Int32() = %v, want %v", got, &v)
121+
}
122+
}
123+
124+
func TestInt64(t *testing.T) {
125+
var v = int64(1)
126+
if got := Int64(v); !reflect.DeepEqual(got, &v) {
127+
t.Errorf("Int64() = %v, want %v", got, &v)
128+
}
129+
}
130+
131+
func TestUInt8(t *testing.T) {
132+
var v = uint8(1)
133+
if got := UInt8(v); !reflect.DeepEqual(got, &v) {
134+
t.Errorf("UInt8() = %v, want %v", got, &v)
135+
}
136+
}
137+
138+
func TestUInt16(t *testing.T) {
139+
var v = uint16(1)
140+
if got := UInt16(v); !reflect.DeepEqual(got, &v) {
141+
t.Errorf("UInt16() = %v, want %v", got, &v)
142+
}
143+
}
144+
145+
func TestUInt32(t *testing.T) {
146+
var v = uint32(1)
147+
if got := UInt32(v); !reflect.DeepEqual(got, &v) {
148+
t.Errorf("UInt32() = %v, want %v", got, &v)
149+
}
150+
}
151+
152+
func TestUInt64(t *testing.T) {
153+
var v = uint64(1)
154+
if got := UInt64(v); !reflect.DeepEqual(got, &v) {
155+
t.Errorf("UInt64() = %v, want %v", got, &v)
156+
}
157+
}
158+
159+
func TestFloat32(t *testing.T) {
160+
var v = float32(1)
161+
if got := Float32(v); !reflect.DeepEqual(got, &v) {
162+
t.Errorf("Float32() = %v, want %v", got, &v)
163+
}
164+
}
165+
166+
func TestFloat64(t *testing.T) {
167+
var v = float64(1)
168+
if got := Float64(v); !reflect.DeepEqual(got, &v) {
169+
t.Errorf("Float64() = %v, want %v", got, &v)
170+
}
171+
}
172+
173+
func TestComplex64(t *testing.T) {
174+
var v = complex64(1)
175+
if got := Complex64(v); !reflect.DeepEqual(got, &v) {
176+
t.Errorf("Complex64() = %v, want %v", got, &v)
177+
}
178+
}
179+
180+
func TestComplex128(t *testing.T) {
181+
var v = complex128(1)
182+
if got := Complex128(v); !reflect.DeepEqual(got, &v) {
183+
t.Errorf("Complex128() = %v, want %v", got, &v)
184+
}
185+
}

0 commit comments

Comments
 (0)