1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-21 23:07:33 +02:00

make any easier to work with

This commit is contained in:
Tao Wen
2016-12-11 10:04:26 +08:00
parent e427475e9c
commit 2895fe2215
6 changed files with 188 additions and 64 deletions

View File

@ -6,34 +6,34 @@ import (
)
func Test_get_from_map(t *testing.T) {
any := Any{Val: map[string]interface{}{
any := Any{val: map[string]interface{}{
"hello": "world",
}}
if any.GetString("hello") != "world" {
if any.ToString("hello") != "world" {
t.FailNow()
}
}
func Test_get_from_array(t *testing.T) {
any := Any{Val: []interface{}{
any := Any{val: []interface{}{
"hello", "world",
}}
if any.GetString(1) != "world" {
if any.ToString(1) != "world" {
t.FailNow()
}
}
func Test_get_int(t *testing.T) {
any := Any{Val: []interface{}{
any := Any{val: []interface{}{
1, 2, 3,
}}
if any.GetInt(1) != 2 {
if any.ToInt(1) != 2 {
t.FailNow()
}
}
func Test_is_null(t *testing.T) {
any := Any{Val: []interface{}{
any := Any{val: []interface{}{
1, 2, 3,
}}
if any.IsNull() != false {
@ -42,22 +42,31 @@ func Test_is_null(t *testing.T) {
}
func Test_get_bool(t *testing.T) {
any := Any{Val: []interface{}{
any := Any{val: []interface{}{
true, true, false,
}}
if any.GetBool(1) != true {
if any.ToBool(1) != true {
t.FailNow()
}
}
func Test_nested_read(t *testing.T) {
any := Any{Val: []interface{}{
any := Any{val: []interface{}{
true, map[string]interface{}{
"hello": "world",
}, false,
}}
if any.GetString(1, "hello") != "world" {
if any.ToString(1, "hello") != "world" {
fmt.Println(any.Error)
t.FailNow()
}
}
func Test_int_to_string(t *testing.T) {
any := Any{val: []interface{}{
true, 5, false,
}}
if any.ToString(1) != "5" {
t.FailNow()
}
}