Skip to content

Commit

Permalink
Merge pull request #54 from kumparan/feature/pointer-string
Browse files Browse the repository at this point in the history
feature: add TrimSpacePointerString, ValueOfPointer
  • Loading branch information
atjhoendz authored Jul 20, 2023
2 parents 1d0557d + a3d6f56 commit 6c88541
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
27 changes: 15 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# go-utils

<a name="v1.33.0"></a>
## [v1.33.0] - 2023-07-20
### New Features
- add TrimSpacePointerString, ValueOfPointer


<a name="v1.32.1"></a>
## [v1.32.1] - 2023-07-06
### Other Improvements
Expand Down Expand Up @@ -102,9 +108,6 @@

<a name="v1.20.0"></a>
## [v1.20.0] - 2022-03-11

<a name="v.1.20.0"></a>
## [v.1.20.0] - 2022-03-11
### New Features
- add constraint size gql directive ([#30](https://github.com/kumparan/kumnats/issues/30))

Expand Down Expand Up @@ -202,11 +205,11 @@
- add money formatter for multiple currencies ([#13](https://github.com/kumparan/kumnats/issues/13))


<a name="v1.7.1"></a>
## [v1.7.1] - 2020-12-10

<a name="v1.8.0"></a>
## [v1.8.0] - 2020-12-10

<a name="v1.7.1"></a>
## [v1.7.1] - 2020-12-10
### New Features
- add formatter for indonesian money and date

Expand Down Expand Up @@ -271,7 +274,8 @@
- init go-utils


[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.32.1...HEAD
[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.33.0...HEAD
[v1.33.0]: https://github.com/kumparan/kumnats/compare/v1.32.1...v1.33.0
[v1.32.1]: https://github.com/kumparan/kumnats/compare/v1.32.0...v1.32.1
[v1.32.0]: https://github.com/kumparan/kumnats/compare/v1.31.0...v1.32.0
[v1.31.0]: https://github.com/kumparan/kumnats/compare/v1.30.0...v1.31.0
Expand All @@ -288,8 +292,7 @@
[v1.22.0]: https://github.com/kumparan/kumnats/compare/v1.21.0...v1.22.0
[v1.21.0]: https://github.com/kumparan/kumnats/compare/v1.20.1...v1.21.0
[v1.20.1]: https://github.com/kumparan/kumnats/compare/v1.20.0...v1.20.1
[v1.20.0]: https://github.com/kumparan/kumnats/compare/v.1.20.0...v1.20.0
[v.1.20.0]: https://github.com/kumparan/kumnats/compare/v1.19.3...v.1.20.0
[v1.20.0]: https://github.com/kumparan/kumnats/compare/v1.19.3...v1.20.0
[v1.19.3]: https://github.com/kumparan/kumnats/compare/v1.19.2...v1.19.3
[v1.19.2]: https://github.com/kumparan/kumnats/compare/v1.19.1...v1.19.2
[v1.19.1]: https://github.com/kumparan/kumnats/compare/v1.19.0...v1.19.1
Expand All @@ -305,9 +308,9 @@
[v1.12.0]: https://github.com/kumparan/kumnats/compare/v1.11.0...v1.12.0
[v1.11.0]: https://github.com/kumparan/kumnats/compare/v1.10.0...v1.11.0
[v1.10.0]: https://github.com/kumparan/kumnats/compare/v1.9.0...v1.10.0
[v1.9.0]: https://github.com/kumparan/kumnats/compare/v1.7.1...v1.9.0
[v1.7.1]: https://github.com/kumparan/kumnats/compare/v1.8.0...v1.7.1
[v1.8.0]: https://github.com/kumparan/kumnats/compare/v1.7.0...v1.8.0
[v1.9.0]: https://github.com/kumparan/kumnats/compare/v1.8.0...v1.9.0
[v1.8.0]: https://github.com/kumparan/kumnats/compare/v1.7.1...v1.8.0
[v1.7.1]: https://github.com/kumparan/kumnats/compare/v1.7.0...v1.7.1
[v1.7.0]: https://github.com/kumparan/kumnats/compare/v1.6.0...v1.7.0
[v1.6.0]: https://github.com/kumparan/kumnats/compare/v1.5.0...v1.6.0
[v1.5.0]: https://github.com/kumparan/kumnats/compare/v1.4.0...v1.5.0
Expand Down
10 changes: 10 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ func DeleteByValue[T comparable](a []T, x T) []T {

return newValue
}

// ValueOfPointer return value of pointer T
func ValueOfPointer[T comparable](i *T) T {
var emptyValue T
if i == nil {
return emptyValue
}

return *i
}
18 changes: 18 additions & 0 deletions generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,21 @@ func Test_DeleteByValue(t *testing.T) {
assert.EqualValues(t, float32s, DeleteByValue[float32](float32s, 5.7))

}

func Test_ValueOfPointer(t *testing.T) {
var i *int
assert.Equal(t, 0, ValueOfPointer(i))
ii := 12345678901
i = &ii
assert.Equal(t, ii, ValueOfPointer(i))
*i = 0
assert.Equal(t, 0, ValueOfPointer(i))

var j *string
assert.Equal(t, "", ValueOfPointer(j))
jj := "jjjjj"
j = &jj
assert.Equal(t, jj, ValueOfPointer(j))
*j = ""
assert.Equal(t, "", ValueOfPointer(j))
}
8 changes: 8 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ func EscapeQuote(in string) string {
}
return string(res)
}

// TrimSpacePointerString :nodoc:
func TrimSpacePointerString(s *string) {
if s == nil {
return
}
*s = strings.TrimSpace(*s)
}
15 changes: 15 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,18 @@ func Test_EscapeQuote(t *testing.T) {
assert.Equal(t, out, EscapeQuote(in))
}
}

func Test_TrimSpacePointerString(t *testing.T) {
str := "this is america"
strWithSpace := " this is america "

testCases := map[*string]*string{
&str: &str,
&strWithSpace: &str,
}

for in, out := range testCases {
TrimSpacePointerString(in)
assert.Equal(t, out, in)
}
}

0 comments on commit 6c88541

Please sign in to comment.