Skip to content

Commit

Permalink
add *P functions return the pointer of converted value #6
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Sep 3, 2022
1 parent 38bb361 commit dbfa185
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func Bool(v interface{}, def ...bool) bool {
return false
}

// BoolP convert and store in a new bool value, and returns a pointer to it
func BoolP(v interface{}, def ...bool) *bool {
i := Bool(v, def...)
return &i
}

// BoolE convert an interface to a bool type
func BoolE(val interface{}) (bool, error) {
v, rv := indirect(val)
Expand Down
21 changes: 21 additions & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ func TestBool_BaseLine(t *testing.T) {
}
}

func TestBoolP(t *testing.T) {
tests := []struct {
input interface{}
expect bool
}{
{"abc", false},
{123, true},
{0, false},
{false, false},
{"false", false},
{"true", true},
}

for i, tt := range tests {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect)

v := cvt.BoolP(tt.input)
assertEqual(t, tt.expect, *v, "[NonE] "+msg)
}
}

func TestBoolE(t *testing.T) {
tests := []struct {
input interface{}
Expand Down
12 changes: 12 additions & 0 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func Float64(v interface{}, def ...float64) float64 {
return 0
}

// Float64P convert and store in a new float64 value, and returns a pointer to it
func Float64P(v interface{}, def ...float64) *float64 {
i := Float64(v, def...)
return &i
}

// Float64E convert an interface to a float64 type
func Float64E(val interface{}) (float64, error) {
v, rv := indirect(val)
Expand Down Expand Up @@ -72,6 +78,12 @@ func Float32(v interface{}, def ...float32) float32 {
return 0
}

// Float32P convert and store in a new float32 value, and returns a pointer to it
func Float32P(v interface{}, def ...float32) *float32 {
i := Float32(v, def...)
return &i
}

// Float32E convert an interface to a float32 type
func Float32E(val interface{}) (float32, error) {
v, e := Float64E(val)
Expand Down
36 changes: 36 additions & 0 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ func TestFloat64_BaseLine(t *testing.T) {
}
}

func TestFloat64P(t *testing.T) {
tests := []struct {
input interface{}
expect float64
}{
{"123", 123},
{123, 123},
{123.01, 123.01},
}

for i, tt := range tests {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect)

v := cvt.Float64P(tt.input)
assertEqual(t, tt.expect, *v, "[NonE] "+msg)
}
}

func TestFloat32_HasDefault(t *testing.T) {
tests := []struct {
input interface{}
Expand Down Expand Up @@ -197,6 +215,24 @@ func TestFloat32_BaseLine(t *testing.T) {
}
}

func TestFloat32P(t *testing.T) {
tests := []struct {
input interface{}
expect float32
}{
{"123", 123},
{123, 123},
{123.01, 123.01},
}

for i, tt := range tests {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect)

v := cvt.Float32P(tt.input)
assertEqual(t, tt.expect, *v, "[NonE] "+msg)
}
}

func TestFloat64E(t *testing.T) {
tests := []struct {
input interface{}
Expand Down
60 changes: 60 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func Uint64(v interface{}, def ...uint64) uint64 {
return 0
}

// Uint64P convert and store in a new uint64 value, and returns a pointer to it
func Uint64P(v interface{}, def ...uint64) *uint64 {
i := Uint64(v, def...)
return &i
}

// Uint64E convert an interface to an uint64 type
func Uint64E(val interface{}) (uint64, error) {
v, e := convUint64(val)
Expand All @@ -43,6 +49,12 @@ func Uint32(v interface{}, def ...uint32) uint32 {
return 0
}

// Uint32P convert and store in a new uint32 value, and returns a pointer to it
func Uint32P(v interface{}, def ...uint32) *uint32 {
i := Uint32(v, def...)
return &i
}

// Uint32E convert an interface to an uint32 type
func Uint32E(val interface{}) (uint32, error) {
v, e := convUint64(val)
Expand All @@ -69,6 +81,12 @@ func Uint16(v interface{}, def ...uint16) uint16 {
return 0
}

// Uint16P convert and store in a new uint16 value, and returns a pointer to it
func Uint16P(v interface{}, def ...uint16) *uint16 {
i := Uint16(v, def...)
return &i
}

// Uint16E convert an interface to an uint16 type
func Uint16E(val interface{}) (uint16, error) {
v, e := convUint64(val)
Expand All @@ -95,6 +113,12 @@ func Uint8(v interface{}, def ...uint8) uint8 {
return 0
}

// Uint8P convert and store in a new uint8 value, and returns a pointer to it
func Uint8P(v interface{}, def ...uint8) *uint8 {
i := Uint8(v, def...)
return &i
}

// Uint8E convert an interface to an uint8 type
func Uint8E(val interface{}) (uint8, error) {
v, e := convUint64(val)
Expand All @@ -121,6 +145,12 @@ func Uint(v interface{}, def ...uint) uint {
return 0
}

// UintP convert and store in a new uint value, and returns a pointer to it
func UintP(v interface{}, def ...uint) *uint {
i := Uint(v, def...)
return &i
}

// UintE convert an interface to an uint type
func UintE(val interface{}) (uint, error) {
v, e := convUint64(val)
Expand All @@ -147,6 +177,12 @@ func Int64(v interface{}, def ...int64) int64 {
return 0
}

// Int64P convert and store in a new int64 value, and returns a pointer to it
func Int64P(v interface{}, def ...int64) *int64 {
i := Int64(v, def...)
return &i
}

// Int64E convert an interface to an int64 type
func Int64E(val interface{}) (int64, error) {
v, e := convInt64(val)
Expand All @@ -170,6 +206,12 @@ func Int32(v interface{}, def ...int32) int32 {
return 0
}

// Int32P convert and store in a new int32 value, and returns a pointer to it
func Int32P(v interface{}, def ...int32) *int32 {
i := Int32(v, def...)
return &i
}

// Int32E convert an interface to an int32 type
func Int32E(val interface{}) (int32, error) {
v, e := convInt64(val)
Expand All @@ -196,6 +238,12 @@ func Int16(v interface{}, def ...int16) int16 {
return 0
}

// Int16P convert and store in a new int16 value, and returns a pointer to it
func Int16P(v interface{}, def ...int16) *int16 {
i := Int16(v, def...)
return &i
}

// Int16E convert an interface to an int16 type
func Int16E(val interface{}) (int16, error) {
v, e := convInt64(val)
Expand All @@ -222,6 +270,12 @@ func Int8(v interface{}, def ...int8) int8 {
return 0
}

// Int8P convert and store in a new int8 value, and returns a pointer to it
func Int8P(v interface{}, def ...int8) *int8 {
i := Int8(v, def...)
return &i
}

// Int8E convert an interface to an int8 type
func Int8E(val interface{}) (int8, error) {
v, e := convInt64(val)
Expand All @@ -248,6 +302,12 @@ func Int(v interface{}, def ...int) int {
return 0
}

// IntP convert and store in a new int value, and returns a pointer to it
func IntP(v interface{}, def ...int) *int {
i := Int(v, def...)
return &i
}

// IntE convert an interface to an int type
func IntE(val interface{}) (int, error) {
v, e := convInt64(val)
Expand Down
Loading

0 comments on commit dbfa185

Please sign in to comment.