Skip to content

Commit

Permalink
🐛 Fixing a bug. about pointer package function (#230)
Browse files Browse the repository at this point in the history
update:
1. UnwarpOr
2. UnwarpOrDefault
3. ExtractPointer

add:
1. UnwrapOr
2. IsNil
  • Loading branch information
aide-cloud authored Jul 18, 2024
1 parent 2097277 commit dac706d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
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
}

0 comments on commit dac706d

Please sign in to comment.