Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixing bug. about pointer package function, #227 #228 #230

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions pointer/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ import (
// Of returns a pointer to the value `v`.
// Play: https://go.dev/play/p/HFd70x4DrMj
func Of[T any](v T) *T {
if IsNil(v) {
return nil
}
return &v
}

// Unwrap returns the value from the pointer.
// Play: https://go.dev/play/p/cgeu3g7cjWb
//
// Play: https://go.dev/play/p/cgeu3g7cjWb
// Deprecated: Please use UnwrapOr
func Unwrap[T any](p *T) T {
return *p
}

// UnwarpOr returns the value from the pointer or fallback if the pointer is nil.
// Play: https://go.dev/play/p/mmNaLC38W8C
//
// Play: https://go.dev/play/p/mmNaLC38W8C
// Deprecated: Please use UnwrapOr
func UnwarpOr[T any](p *T, fallback T) T {
if p == nil {
return fallback
Expand All @@ -30,7 +37,9 @@ func UnwarpOr[T any](p *T, fallback T) T {
}

// UnwarpOrDefault returns the value from the pointer or the default value if the pointer is nil.
// Play: https://go.dev/play/p/ZnGIHf8_o4E
//
// Play: https://go.dev/play/p/ZnGIHf8_o4E
// Deprecated: Please use UnwrapOr
func UnwarpOrDefault[T any](p *T) T {
var v T

Expand All @@ -40,9 +49,24 @@ func UnwarpOrDefault[T any](p *T) T {
return *p
}

// UnwrapOr returns the value from the pointer or fallback if the pointer is nil.
func UnwrapOr[T any](p *T, fallback ...T) T {
if !IsNil(p) {
return *p
}
if len(fallback) > 0 {
return fallback[0]
}
var t T
return t
}

// ExtractPointer returns the underlying value by the given interface type
// Play: https://go.dev/play/p/D7HFjeWU2ZP
func ExtractPointer(value any) any {
if IsNil(value) {
return value
}
t := reflect.TypeOf(value)
v := reflect.ValueOf(value)

Expand All @@ -56,3 +80,8 @@ func ExtractPointer(value any) any {

return nil
}

// IsNil returns true if the given interface is nil or the underlying value is nil.
func IsNil(i interface{}) bool {
return i == nil || (reflect.ValueOf(i).Kind() == reflect.Ptr && reflect.ValueOf(i).IsNil())
}
58 changes: 58 additions & 0 deletions pointer/pointer_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,61 @@ func ExampleExtractPointer() {
// Output:
// 1
}

func ExampleIsNil() {
a := 1
b := &a
c := &b
d := &c
e := &d
var f *int

result1 := IsNil(a)
result2 := IsNil(b)
result3 := IsNil(c)
result4 := IsNil(d)
result5 := IsNil(e)
result6 := IsNil(f)
result7 := IsNil(nil)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
fmt.Println(result7)

// Output:
// false
// false
// false
// false
// false
// true
// true
}

func ExampleUnwrapOr() {
a := 123
b := "abc"

var c *int
var d *string

result1 := UnwrapOr(&a, 456)
result2 := UnwrapOr(&b, "efg")
result3 := UnwrapOr(c, 456)
result4 := UnwrapOr(d, "def")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)

// Output:
// 123
// abc
// 456
// def
}