初始化项目
This commit is contained in:
25
trunk/goutil/typeUtil/bool.go
Normal file
25
trunk/goutil/typeUtil/bool.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package typeUtil
|
||||
|
||||
// bool值转换为int
|
||||
// value:待转换的值
|
||||
// 返回值:
|
||||
// bool:转换结果
|
||||
func BoolToInt(value bool) int {
|
||||
if value {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// int转换为bool值
|
||||
// value:待转换的值
|
||||
// 返回值:
|
||||
// bool:转换结果
|
||||
func IntToBool(value int) bool {
|
||||
if value > 0 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
63
trunk/goutil/typeUtil/bool_test.go
Normal file
63
trunk/goutil/typeUtil/bool_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package typeUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBoolToInt(t *testing.T) {
|
||||
// Test with true value
|
||||
value := true
|
||||
expected := 1
|
||||
got := BoolToInt(value)
|
||||
if expected != got {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
// Test with false value
|
||||
value = false
|
||||
expected = 0
|
||||
got = BoolToInt(value)
|
||||
if expected != got {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntToBool(t *testing.T) {
|
||||
// Test with 0 value
|
||||
value := 0
|
||||
expected := false
|
||||
got := IntToBool(value)
|
||||
if expected != got {
|
||||
t.Errorf("Expected %t, but got %t", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
// Test with negative value
|
||||
value = -1
|
||||
expected = false
|
||||
got = IntToBool(value)
|
||||
if expected != got {
|
||||
t.Errorf("Expected %t, but got %t", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
// Test with positive value
|
||||
value = 1
|
||||
expected = true
|
||||
got = IntToBool(value)
|
||||
if expected != got {
|
||||
t.Errorf("Expected %t, but got %t", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
// Test with bigger positive value
|
||||
value = 100
|
||||
expected = true
|
||||
got = IntToBool(value)
|
||||
if expected != got {
|
||||
t.Errorf("Expected %t, but got %t", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
4
trunk/goutil/typeUtil/doc.go
Normal file
4
trunk/goutil/typeUtil/doc.go
Normal file
@@ -0,0 +1,4 @@
|
||||
/*
|
||||
数据类型转换类(主要针对interface)
|
||||
*/
|
||||
package typeUtil
|
||||
1229
trunk/goutil/typeUtil/interface.go
Normal file
1229
trunk/goutil/typeUtil/interface.go
Normal file
File diff suppressed because it is too large
Load Diff
3487
trunk/goutil/typeUtil/interface_test.go
Normal file
3487
trunk/goutil/typeUtil/interface_test.go
Normal file
File diff suppressed because it is too large
Load Diff
265
trunk/goutil/typeUtil/mapData.go
Normal file
265
trunk/goutil/typeUtil/mapData.go
Normal file
@@ -0,0 +1,265 @@
|
||||
package typeUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// KeyValue数据集合
|
||||
type MapData map[string]interface{}
|
||||
|
||||
// 创建新的MapData
|
||||
// mapData:原有的map数据
|
||||
// 返回
|
||||
// 新的Map对象
|
||||
func NewMapData(mapData map[string]interface{}) MapData {
|
||||
return MapData(mapData)
|
||||
}
|
||||
|
||||
// 类型转换为byte
|
||||
// 返回值:
|
||||
// byte:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Byte(key string) (value byte, err error) {
|
||||
return this.Uint8(key)
|
||||
}
|
||||
|
||||
// 类型转换为int
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Int(key string) (value int, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Int(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为int8
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Int8(key string) (value int8, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Int8(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为int16
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Int16(key string) (value int16, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Int16(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为int32
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Int32(key string) (value int32, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Int32(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为int64
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Int64(key string) (value int64, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Int64(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为uint
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Uint(key string) (value uint, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Uint(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为uint8
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Uint8(key string) (value uint8, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Uint8(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为uint16
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Uint16(key string) (value uint16, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Uint16(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为uint32
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Uint32(key string) (value uint32, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Uint32(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为uint64
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Uint64(key string) (value uint64, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Uint64(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为float32
|
||||
// 返回值:
|
||||
// float64:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Float32(key string) (value float32, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Float32(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为float64
|
||||
// 返回值:
|
||||
// float64:结果
|
||||
// error:错误数据
|
||||
func (this MapData) Float64(key string) (value float64, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Float64(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为bool
|
||||
// 返回值:
|
||||
// bool:结果
|
||||
// error:错误信息
|
||||
func (this MapData) Bool(key string) (value bool, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = Bool(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 类型转换为字符串
|
||||
// 返回值:
|
||||
// string:结果
|
||||
// error:错误信息
|
||||
func (this MapData) String(key string) (value string, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = String(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为时间格式,如果是字符串,则要求内容格式形如:2017-02-14 05:20:00
|
||||
// 返回值:
|
||||
// bool:结果
|
||||
// error:错误信息
|
||||
func (this MapData) DateTime(key string) (value time.Time, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value, err = DateTime(val)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取指定的值
|
||||
// 返回值:
|
||||
// interface{}:结果
|
||||
// error:错误信息
|
||||
func (this MapData) Interface(key string) (value interface{}, err error) {
|
||||
val, exist := this[key]
|
||||
if exist == false || val == nil {
|
||||
err = fmt.Errorf("Target key: [%s] doesn't exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
value = val
|
||||
return
|
||||
}
|
||||
587
trunk/goutil/typeUtil/mapData_test.go
Normal file
587
trunk/goutil/typeUtil/mapData_test.go
Normal file
@@ -0,0 +1,587 @@
|
||||
package typeUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMapDataByte(t *testing.T) {
|
||||
TestMapDataUint8(t)
|
||||
}
|
||||
|
||||
func TestMapDataInt(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected int = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Int(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Int(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Int(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataInt8(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected int8 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Int8(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Int8(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Int8(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataInt16(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected int16 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Int16(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Int16(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Int16(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataInt32(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected int32 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Int32(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Int32(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Int32(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataInt64(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected int64 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Int64(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Int64(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Int64(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataUint(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected uint = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Uint(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Uint(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Uint(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataUint8(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected uint8 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Uint8(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Uint8(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Uint8(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataUint16(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected uint16 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Uint16(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Uint16(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Uint16(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataUint32(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected uint32 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Uint32(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Uint32(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Uint32(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataUint64(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected uint64 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Uint64(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Uint64(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Uint64(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataFloat32(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected float32 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Float32(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Float32(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Float32(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %f, but got %f", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataFloat64(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected float64 = 0
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Float64(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Float64(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = 1
|
||||
expected = 1
|
||||
got, err = mapData.Float64(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %f, but got %f", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataBool(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected bool = true
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Bool(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = "abc"
|
||||
got, err = mapData.Bool(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = true
|
||||
expected = true
|
||||
got, err = mapData.Bool(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %t, but got %t", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataString(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected string = ""
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.String(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist, but type doesn't match
|
||||
mapData[key] = 123
|
||||
expected = "123"
|
||||
got, err = mapData.String(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = "hello"
|
||||
expected = "hello"
|
||||
got, err = mapData.String(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
|
||||
func TestMapDataDateTime(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected time.Time = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.DateTime(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist but value doesn't match
|
||||
mapData[key] = "123"
|
||||
expected = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
|
||||
got, err = mapData.DateTime(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
|
||||
expected = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
|
||||
got, err = mapData.DateTime(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if got != expected {
|
||||
t.Errorf("Expected %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapDataInterface(t *testing.T) {
|
||||
data := make(map[string]interface{})
|
||||
mapData := NewMapData(data)
|
||||
key := "key"
|
||||
var expected *Person
|
||||
|
||||
// Test when key doesn't exist
|
||||
got, err := mapData.Interface(key)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
// Test when key exist and value matches
|
||||
mapData[key] = NewPerson("Jordan", 34)
|
||||
expected = NewPerson("Jordan", 34)
|
||||
got, err = mapData.Interface(key)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if gotPersonObj, ok := got.(*Person); !ok {
|
||||
t.Errorf("Expected type *Person")
|
||||
} else if gotPersonObj.SameAs(expected) == false {
|
||||
t.Errorf("Expected %v, but got %v", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
func (this *Person) SameAs(other *Person) bool {
|
||||
return this.Name == other.Name && this.Age == other.Age
|
||||
}
|
||||
|
||||
func NewPerson(name string, age int) *Person {
|
||||
return &Person{
|
||||
Name: name,
|
||||
Age: age,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user