diff --git a/bool.go b/bool.go index 30b7eba..5e49ed2 100644 --- a/bool.go +++ b/bool.go @@ -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) diff --git a/bool_test.go b/bool_test.go index aa9055e..4cbc948 100644 --- a/bool_test.go +++ b/bool_test.go @@ -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{} diff --git a/float.go b/float.go index c0b7e28..2c3f2e2 100644 --- a/float.go +++ b/float.go @@ -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) @@ -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) diff --git a/float_test.go b/float_test.go index 5c61481..fa332a6 100644 --- a/float_test.go +++ b/float_test.go @@ -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{} @@ -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{} diff --git a/int.go b/int.go index 0ab26ad..e121c34 100644 --- a/int.go +++ b/int.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/int_test.go b/int_test.go index bf57efe..7aed384 100644 --- a/int_test.go +++ b/int_test.go @@ -104,6 +104,24 @@ func TestUint64_BaseLine(t *testing.T) { } } +func TestUint64P(t *testing.T) { + tests := []struct { + input interface{} + expect uint64 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Uint64P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestUint32_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -199,6 +217,24 @@ func TestUint32_BaseLine(t *testing.T) { } } +func TestUint32P(t *testing.T) { + tests := []struct { + input interface{} + expect uint32 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Uint32P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestUint16_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -294,6 +330,24 @@ func TestUint16_BaseLine(t *testing.T) { } } +func TestUint16P(t *testing.T) { + tests := []struct { + input interface{} + expect uint16 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Uint16P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestUint8_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -389,6 +443,24 @@ func TestUint8_BaseLine(t *testing.T) { } } +func TestUint8P(t *testing.T) { + tests := []struct { + input interface{} + expect uint8 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Uint8P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestUint_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -484,6 +556,24 @@ func TestUint_BaseLine(t *testing.T) { } } +func TestUintP(t *testing.T) { + tests := []struct { + input interface{} + expect uint + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.UintP(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestInt64_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -577,6 +667,24 @@ func TestInt64_BaseLine(t *testing.T) { } } +func TestInt64P(t *testing.T) { + tests := []struct { + input interface{} + expect int64 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int64P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestInt32_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -670,6 +778,24 @@ func TestInt32_BaseLine(t *testing.T) { } } +func TestInt32P(t *testing.T) { + tests := []struct { + input interface{} + expect int32 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int32P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestInt16_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -763,6 +889,24 @@ func TestInt16_BaseLine(t *testing.T) { } } +func TestInt16P(t *testing.T) { + tests := []struct { + input interface{} + expect int16 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int16P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestInt8_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -856,6 +1000,24 @@ func TestInt8_BaseLine(t *testing.T) { } } +func TestInt8P(t *testing.T) { + tests := []struct { + input interface{} + expect int8 + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int8P(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestInt_HasDefault(t *testing.T) { tests := []struct { input interface{} @@ -949,6 +1111,24 @@ func TestInt_BaseLine(t *testing.T) { } } +func TestIntP(t *testing.T) { + tests := []struct { + input interface{} + expect int + }{ + {"123", 123}, + {123, 123}, + {123.01, 123}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.IntP(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestUint64E(t *testing.T) { tests := []struct { input interface{} diff --git a/string.go b/string.go index ba6b5e4..7b2af98 100644 --- a/string.go +++ b/string.go @@ -19,6 +19,12 @@ func String(v interface{}, def ...string) string { return "" } +// StringP convert and store in a new string value, and returns a pointer to it +func StringP(v interface{}, def ...string) *string { + s := String(v, def...) + return &s +} + // StringE convert an interface to a string type func StringE(val interface{}) (string, error) { // interface implements diff --git a/string_test.go b/string_test.go index a3ddcdc..4dc412c 100644 --- a/string_test.go +++ b/string_test.go @@ -75,6 +75,24 @@ func TestString_BaseLine(t *testing.T) { } } +func TestStringP(t *testing.T) { + tests := []struct { + input interface{} + expect string + }{ + {"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.StringP(tt.input) + assertEqual(t, tt.expect, *v, "[NonE] "+msg) + } +} + func TestStringE(t *testing.T) { tests := []struct { input interface{}