mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
🐛 Fixing a bug. about pointer package function (#230)
update: 1. UnwarpOr 2. UnwarpOrDefault 3. ExtractPointer add: 1. UnwrapOr 2. IsNil
This commit is contained in:
@@ -11,17 +11,24 @@ import (
|
|||||||
// Of returns a pointer to the value `v`.
|
// Of returns a pointer to the value `v`.
|
||||||
// Play: https://go.dev/play/p/HFd70x4DrMj
|
// Play: https://go.dev/play/p/HFd70x4DrMj
|
||||||
func Of[T any](v T) *T {
|
func Of[T any](v T) *T {
|
||||||
|
if IsNil(v) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unwrap returns the value from the pointer.
|
// 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 {
|
func Unwrap[T any](p *T) T {
|
||||||
return *p
|
return *p
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnwarpOr returns the value from the pointer or fallback if the pointer is nil.
|
// 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 {
|
func UnwarpOr[T any](p *T, fallback T) T {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return fallback
|
return fallback
|
||||||
@@ -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.
|
// 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 {
|
func UnwarpOrDefault[T any](p *T) T {
|
||||||
var v T
|
var v T
|
||||||
|
|
||||||
@@ -40,9 +49,24 @@ func UnwarpOrDefault[T any](p *T) T {
|
|||||||
return *p
|
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
|
// ExtractPointer returns the underlying value by the given interface type
|
||||||
// Play: https://go.dev/play/p/D7HFjeWU2ZP
|
// Play: https://go.dev/play/p/D7HFjeWU2ZP
|
||||||
func ExtractPointer(value any) any {
|
func ExtractPointer(value any) any {
|
||||||
|
if IsNil(value) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
t := reflect.TypeOf(value)
|
t := reflect.TypeOf(value)
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
|
|
||||||
@@ -56,3 +80,8 @@ func ExtractPointer(value any) any {
|
|||||||
|
|
||||||
return nil
|
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())
|
||||||
|
}
|
||||||
|
|||||||
@@ -90,3 +90,61 @@ func ExampleExtractPointer() {
|
|||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user