Skip to content

Commit d41b727

Browse files
committed
feat(string): Add in basic string helpers
Signed-off-by: Jason Field <jason@avon-lea.co.uk>
1 parent 8e03fc1 commit d41b727

File tree

7 files changed

+291
-0
lines changed

7 files changed

+291
-0
lines changed

.gitignore

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/go,goland+all,macos,linux,windows,visualstudiocode
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=go,goland+all,macos,linux,windows,visualstudiocode
3+
4+
### Go ###
5+
# If you prefer the allow list template instead of the deny list, see community template:
6+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
7+
#
18
# Binaries for programs and plugins
29
*.exe
310
*.exe~
@@ -13,3 +20,189 @@
1320

1421
# Dependency directories (remove the comment below to include it)
1522
# vendor/
23+
24+
# Go workspace file
25+
go.work
26+
27+
### GoLand+all ###
28+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
29+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
30+
31+
# User-specific stuff
32+
.idea/**/workspace.xml
33+
.idea/**/tasks.xml
34+
.idea/**/usage.statistics.xml
35+
.idea/**/dictionaries
36+
.idea/**/shelf
37+
38+
# AWS User-specific
39+
.idea/**/aws.xml
40+
41+
# Generated files
42+
.idea/**/contentModel.xml
43+
44+
# Sensitive or high-churn files
45+
.idea/**/dataSources/
46+
.idea/**/dataSources.ids
47+
.idea/**/dataSources.local.xml
48+
.idea/**/sqlDataSources.xml
49+
.idea/**/dynamic.xml
50+
.idea/**/uiDesigner.xml
51+
.idea/**/dbnavigator.xml
52+
53+
# Gradle
54+
.idea/**/gradle.xml
55+
.idea/**/libraries
56+
57+
# Gradle and Maven with auto-import
58+
# When using Gradle or Maven with auto-import, you should exclude module files,
59+
# since they will be recreated, and may cause churn. Uncomment if using
60+
# auto-import.
61+
# .idea/artifacts
62+
# .idea/compiler.xml
63+
# .idea/jarRepositories.xml
64+
# .idea/modules.xml
65+
# .idea/*.iml
66+
# .idea/modules
67+
# *.iml
68+
# *.ipr
69+
70+
# CMake
71+
cmake-build-*/
72+
73+
# Mongo Explorer plugin
74+
.idea/**/mongoSettings.xml
75+
76+
# File-based project format
77+
*.iws
78+
79+
# IntelliJ
80+
out/
81+
82+
# mpeltonen/sbt-idea plugin
83+
.idea_modules/
84+
85+
# JIRA plugin
86+
atlassian-ide-plugin.xml
87+
88+
# Cursive Clojure plugin
89+
.idea/replstate.xml
90+
91+
# SonarLint plugin
92+
.idea/sonarlint/
93+
94+
# Crashlytics plugin (for Android Studio and IntelliJ)
95+
com_crashlytics_export_strings.xml
96+
crashlytics.properties
97+
crashlytics-build.properties
98+
fabric.properties
99+
100+
# Editor-based Rest Client
101+
.idea/httpRequests
102+
103+
# Android studio 3.1+ serialized cache file
104+
.idea/caches/build_file_checksums.ser
105+
106+
### GoLand+all Patch ###
107+
# Ignore everything but code style settings and run configurations
108+
# that are supposed to be shared within teams.
109+
110+
.idea/*
111+
112+
!.idea/codeStyles
113+
!.idea/runConfigurations
114+
115+
### Linux ###
116+
*~
117+
118+
# temporary files which can be created if a process still has a handle open of a deleted file
119+
.fuse_hidden*
120+
121+
# KDE directory preferences
122+
.directory
123+
124+
# Linux trash folder which might appear on any partition or disk
125+
.Trash-*
126+
127+
# .nfs files are created when an open file is removed but is still being accessed
128+
.nfs*
129+
130+
### macOS ###
131+
# General
132+
.DS_Store
133+
.AppleDouble
134+
.LSOverride
135+
136+
# Icon must end with two \r
137+
Icon
138+
139+
140+
# Thumbnails
141+
._*
142+
143+
# Files that might appear in the root of a volume
144+
.DocumentRevisions-V100
145+
.fseventsd
146+
.Spotlight-V100
147+
.TemporaryItems
148+
.Trashes
149+
.VolumeIcon.icns
150+
.com.apple.timemachine.donotpresent
151+
152+
# Directories potentially created on remote AFP share
153+
.AppleDB
154+
.AppleDesktop
155+
Network Trash Folder
156+
Temporary Items
157+
.apdisk
158+
159+
### macOS Patch ###
160+
# iCloud generated files
161+
*.icloud
162+
163+
### VisualStudioCode ###
164+
.vscode/*
165+
!.vscode/settings.json
166+
!.vscode/tasks.json
167+
!.vscode/launch.json
168+
!.vscode/extensions.json
169+
!.vscode/*.code-snippets
170+
171+
# Local History for Visual Studio Code
172+
.history/
173+
174+
# Built Visual Studio Code Extensions
175+
*.vsix
176+
177+
### VisualStudioCode Patch ###
178+
# Ignore all local history of files
179+
.history
180+
.ionide
181+
182+
### Windows ###
183+
# Windows thumbnail cache files
184+
Thumbs.db
185+
Thumbs.db:encryptable
186+
ehthumbs.db
187+
ehthumbs_vista.db
188+
189+
# Dump file
190+
*.stackdump
191+
192+
# Folder config file
193+
[Dd]esktop.ini
194+
195+
# Recycle Bin used on file shares
196+
$RECYCLE.BIN/
197+
198+
# Windows Installer files
199+
*.cab
200+
*.msi
201+
*.msix
202+
*.msm
203+
*.msp
204+
205+
# Windows shortcuts
206+
*.lnk
207+
208+
# End of https://www.toptal.com/developers/gitignore/api/go,goland+all,macos,linux,windows,visualstudiocode

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# pointerhelpers
2+
23
Go repo to hold all the helpers for pointers to save rewriting the same code every time
4+

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/xorima/pointerhelpers
2+
3+
go 1.19
4+
5+
require github.com/stretchr/testify v1.8.1
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

string.go

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

string_test.go

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

types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pointerhelpers
2+
3+
// PointerHelper is a struct to include all the various helpers
4+
// in this module to enable easier composition outside of this package
5+
type PointerHelper struct {
6+
StringHelper
7+
}

0 commit comments

Comments
 (0)