-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLOUDTRUST-4637] Add conversion functions
- Loading branch information
Showing
3 changed files
with
228 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package commonservice | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
layout = "02.01.2006" | ||
) | ||
|
||
// ToTimePtr converts an interface{} value as a Time pointer. Error is not possible, defaults to nil | ||
func ToTimePtr(value interface{}) *time.Time { | ||
if value == nil { | ||
return nil | ||
} | ||
switch v := value.(type) { | ||
case time.Time: | ||
return &v | ||
case *time.Time: | ||
return v | ||
case string: | ||
var t, _ = time.Parse(layout, v) | ||
return &t | ||
case *string: | ||
var t, _ = time.Parse(layout, *v) | ||
return &t | ||
} | ||
return nil | ||
} | ||
|
||
// ToStringPtr converts an interface{} value as a string pointer | ||
func ToStringPtr(value interface{}) *string { | ||
if value == nil { | ||
return nil | ||
} | ||
switch v := value.(type) { | ||
case *string: | ||
return v | ||
} | ||
var res = fmt.Sprintf("%v", value) | ||
return &res | ||
} | ||
|
||
// ToInt converts an interface{} value as int and returns a default value in case of error | ||
func ToInt(value interface{}, defaultValue int) int { | ||
switch v := value.(type) { | ||
case int: | ||
return v | ||
case *int: | ||
return *v | ||
case int64: | ||
return int(v) | ||
case *int64: | ||
return int(*v) | ||
case int32: | ||
return int(v) | ||
case *int32: | ||
return int(*v) | ||
case float32: | ||
return int(v) | ||
case *float32: | ||
return int(*v) | ||
case float64: | ||
return int(v) | ||
case *float64: | ||
return int(*v) | ||
case string: | ||
return atoi(v, defaultValue) | ||
case *string: | ||
return atoi(*v, defaultValue) | ||
default: | ||
return defaultValue | ||
} | ||
} | ||
|
||
// ToFloat converts an interface{} value as float64 and returns a default value in case of error | ||
func ToFloat(value interface{}, defaultValue float64) float64 { | ||
switch v := value.(type) { | ||
case int: | ||
return float64(v) | ||
case *int: | ||
return float64(*v) | ||
case int64: | ||
return float64(v) | ||
case *int64: | ||
return float64(*v) | ||
case int32: | ||
return float64(v) | ||
case *int32: | ||
return float64(*v) | ||
case float32: | ||
return float64(v) | ||
case *float32: | ||
return float64(*v) | ||
case float64: | ||
return v | ||
case *float64: | ||
return *v | ||
case string: | ||
return atof(v, defaultValue) | ||
case *string: | ||
return atof(*v, defaultValue) | ||
default: | ||
return defaultValue | ||
} | ||
} | ||
|
||
func atoi(value string, defaultValue int) int { | ||
var res, err = strconv.Atoi(value) | ||
if err != nil { | ||
return defaultValue | ||
} | ||
return res | ||
} | ||
|
||
func atof(value string, defaultValue float64) float64 { | ||
var res, err = strconv.ParseFloat(value, 64) | ||
if err != nil { | ||
return defaultValue | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package commonservice | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type dummy struct { | ||
} | ||
|
||
func ptr(value string) *string { | ||
return &value | ||
} | ||
|
||
func toStr(value *time.Time) string { | ||
return value.Format(layout) | ||
} | ||
|
||
func TestToTimePtr(t *testing.T) { | ||
var anyTime = time.Now().Add(-2500 * time.Hour) // Why not | ||
var anyTimeAsString = anyTime.Format(layout) | ||
assert.Nil(t, ToTimePtr(nil)) | ||
assert.Nil(t, ToTimePtr(dummy{})) | ||
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(anyTime))) | ||
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(&anyTime))) | ||
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(anyTimeAsString))) | ||
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(&anyTimeAsString))) | ||
assert.Equal(t, toStr(&anyTime), toStr(ToTimePtr(anyTime))) | ||
} | ||
|
||
func TestToStringPtr(t *testing.T) { | ||
var str = "abc" | ||
assert.Nil(t, ToStringPtr(nil)) | ||
assert.Equal(t, "2", *ToStringPtr(2)) | ||
assert.Equal(t, "2", *ToStringPtr(2.0)) | ||
assert.Equal(t, "abc", *ToStringPtr(str)) | ||
assert.Equal(t, "abc", *ToStringPtr(&str)) | ||
} | ||
|
||
func TestToInt(t *testing.T) { | ||
var invalid = -99999 | ||
var vInt = 2 | ||
var vInt32 = int32(2) | ||
var vInt64 = int64(2) | ||
var vFloat32 = float32(2.0) | ||
var vFloat64 = float64(2.0) | ||
assert.Equal(t, 2, ToInt(ptr("2"), invalid)) | ||
assert.Equal(t, 2, ToInt("2", invalid)) | ||
assert.Equal(t, invalid, ToInt("2x", invalid)) | ||
assert.Equal(t, 2, ToInt(vInt, invalid)) | ||
assert.Equal(t, 2, ToInt(&vInt, invalid)) | ||
assert.Equal(t, 2, ToInt(vInt32, invalid)) | ||
assert.Equal(t, 2, ToInt(&vInt32, invalid)) | ||
assert.Equal(t, 2, ToInt(vInt64, invalid)) | ||
assert.Equal(t, 2, ToInt(&vInt64, invalid)) | ||
assert.Equal(t, 2, ToInt(vFloat32, invalid)) | ||
assert.Equal(t, 2, ToInt(&vFloat32, invalid)) | ||
assert.Equal(t, 2, ToInt(vFloat64, invalid)) | ||
assert.Equal(t, 2, ToInt(&vFloat64, invalid)) | ||
assert.Equal(t, invalid, ToInt(time.Now(), invalid)) | ||
} | ||
|
||
func TestToFloat(t *testing.T) { | ||
var invalid = float64(-99999) | ||
var expected = float64(3.14) | ||
var vInt = 3 | ||
var vInt32 = int32(3) | ||
var vInt64 = int64(3) | ||
var vFloat32 = float32(3.14) | ||
var vFloat64 = float64(3.14) | ||
assert.Equal(t, invalid, ToFloat("xxx", invalid)) | ||
assert.Equal(t, float64(3), ToFloat(vInt, invalid)) | ||
assert.Equal(t, float64(3), ToFloat(&vInt, invalid)) | ||
assert.Equal(t, float64(3), ToFloat(vInt32, invalid)) | ||
assert.Equal(t, float64(3), ToFloat(&vInt32, invalid)) | ||
assert.Equal(t, float64(3), ToFloat(vInt64, invalid)) | ||
assert.Equal(t, float64(3), ToFloat(&vInt64, invalid)) | ||
var equals = func(a, b float64) bool { | ||
return int64(a*100.0) == int64(b*100) | ||
} | ||
assert.True(t, equals(expected, ToFloat(ptr("3.14"), invalid))) | ||
assert.True(t, equals(expected, ToFloat("3.14", invalid))) | ||
assert.True(t, equals(expected, ToFloat(vFloat32, invalid))) | ||
assert.True(t, equals(expected, ToFloat(&vFloat32, invalid))) | ||
assert.True(t, equals(expected, ToFloat(vFloat64, invalid))) | ||
assert.True(t, equals(expected, ToFloat(&vFloat64, invalid))) | ||
assert.Equal(t, invalid, ToFloat(time.Now(), invalid)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters