diff --git a/CHANGELOG.md b/CHANGELOG.md index d4cd12a..0f03957 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # go-utils + +## [v1.33.0] - 2023-07-20 +### New Features +- add TrimSpacePointerString, ValueOfPointer + + ## [v1.32.1] - 2023-07-06 ### Other Improvements @@ -102,9 +108,6 @@ ## [v1.20.0] - 2022-03-11 - - -## [v.1.20.0] - 2022-03-11 ### New Features - add constraint size gql directive ([#30](https://github.com/kumparan/kumnats/issues/30)) @@ -202,11 +205,11 @@ - add money formatter for multiple currencies ([#13](https://github.com/kumparan/kumnats/issues/13)) - -## [v1.7.1] - 2020-12-10 - ## [v1.8.0] - 2020-12-10 + + +## [v1.7.1] - 2020-12-10 ### New Features - add formatter for indonesian money and date @@ -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 @@ -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 @@ -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 diff --git a/generic.go b/generic.go index d4af43a..95de8f6 100644 --- a/generic.go +++ b/generic.go @@ -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 +} diff --git a/generic_test.go b/generic_test.go index 9286015..ff92c69 100644 --- a/generic_test.go +++ b/generic_test.go @@ -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)) +} diff --git a/string.go b/string.go index e3af5e0..fcc75be 100644 --- a/string.go +++ b/string.go @@ -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) +} diff --git a/string_test.go b/string_test.go index 08d3fba..d5fe56b 100644 --- a/string_test.go +++ b/string_test.go @@ -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) + } +}