mirror of https://github.com/emirpasic/gods
Merge pull request #53 from emirpasic/json
JSON serialization (marshaling) / deserialization (unmarshaling)pull/55/head v1.9.0
commit
f6c17b5248
@ -0,0 +1,17 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package containers
|
||||
|
||||
// JSONSerializer provides JSON serialization
|
||||
type JSONSerializer interface {
|
||||
// ToJSON outputs the JSON representation of containers's elements.
|
||||
ToJSON() ([]byte, error)
|
||||
}
|
||||
|
||||
// JSONDeserializer provides JSON deserialization
|
||||
type JSONDeserializer interface {
|
||||
// FromJSON populates containers's elements from the input JSON representation.
|
||||
FromJSON([]byte) error
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package examples
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/emirpasic/gods/lists/arraylist"
|
||||
"github.com/emirpasic/gods/maps/hashmap"
|
||||
)
|
||||
|
||||
// ListSerializationExample demonstrates how to serialize and deserialize lists to and from JSON
|
||||
func ListSerializationExample() {
|
||||
list := arraylist.New()
|
||||
list.Add("a", "b", "c")
|
||||
|
||||
// Serialization (marshalling)
|
||||
json, err := list.ToJSON()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(string(json)) // ["a","b","c"]
|
||||
|
||||
// Deserialization (unmarshalling)
|
||||
json = []byte(`["a","b"]`)
|
||||
err = list.FromJSON(json)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(list) // ArrayList ["a","b"]
|
||||
}
|
||||
|
||||
// MapSerializationExample demonstrates how to serialize and deserialize maps to and from JSON
|
||||
func MapSerializationExample() {
|
||||
m := hashmap.New()
|
||||
m.Put("a", "1")
|
||||
m.Put("b", "2")
|
||||
m.Put("c", "3")
|
||||
|
||||
// Serialization (marshalling)
|
||||
json, err := m.ToJSON()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(string(json)) // {"a":"1","b":"2","c":"3"}
|
||||
|
||||
// Deserialization (unmarshalling)
|
||||
json = []byte(`{"a":"1","b":"2"}`)
|
||||
err = m.FromJSON(json)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(m) // HashMap {"a":"1","b":"2"}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package arraylist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*List)(nil)
|
||||
var _ containers.JSONDeserializer = (*List)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (list *List) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(list.elements[:list.size])
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (list *List) FromJSON(data []byte) error {
|
||||
err := json.Unmarshal(data, &list.elements)
|
||||
if err == nil {
|
||||
list.size = len(list.elements)
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package doublylinkedlist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*List)(nil)
|
||||
var _ containers.JSONDeserializer = (*List)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (list *List) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(list.Values())
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (list *List) FromJSON(data []byte) error {
|
||||
elements := []interface{}{}
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
list.Clear()
|
||||
list.Add(elements...)
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package singlylinkedlist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*List)(nil)
|
||||
var _ containers.JSONDeserializer = (*List)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (list *List) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(list.Values())
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (list *List) FromJSON(data []byte) error {
|
||||
elements := []interface{}{}
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
list.Clear()
|
||||
list.Add(elements...)
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package hashbidimap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Map)(nil)
|
||||
var _ containers.JSONDeserializer = (*Map)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (m *Map) ToJSON() ([]byte, error) {
|
||||
return m.forwardMap.ToJSON()
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (m *Map) FromJSON(data []byte) error {
|
||||
elements := make(map[string]interface{})
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
m.Clear()
|
||||
for key, value := range elements {
|
||||
m.Put(key, value)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package hashmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Map)(nil)
|
||||
var _ containers.JSONDeserializer = (*Map)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (m *Map) ToJSON() ([]byte, error) {
|
||||
elements := make(map[string]interface{})
|
||||
for key, value := range m.m {
|
||||
elements[utils.ToString(key)] = value
|
||||
}
|
||||
return json.Marshal(&elements)
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (m *Map) FromJSON(data []byte) error {
|
||||
elements := make(map[string]interface{})
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
m.Clear()
|
||||
for key, value := range elements {
|
||||
m.m[key] = value
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package treebidimap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Map)(nil)
|
||||
var _ containers.JSONDeserializer = (*Map)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (m *Map) ToJSON() ([]byte, error) {
|
||||
elements := make(map[string]interface{})
|
||||
it := m.Iterator()
|
||||
for it.Next() {
|
||||
elements[utils.ToString(it.Key())] = it.Value()
|
||||
}
|
||||
return json.Marshal(&elements)
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (m *Map) FromJSON(data []byte) error {
|
||||
elements := make(map[string]interface{})
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
m.Clear()
|
||||
for key, value := range elements {
|
||||
m.Put(key, value)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package treemap
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Map)(nil)
|
||||
var _ containers.JSONDeserializer = (*Map)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (m *Map) ToJSON() ([]byte, error) {
|
||||
return m.tree.ToJSON()
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (m *Map) FromJSON(data []byte) error {
|
||||
return m.tree.FromJSON(data)
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package hashset
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Set)(nil)
|
||||
var _ containers.JSONDeserializer = (*Set)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (set *Set) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(set.Values())
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (set *Set) FromJSON(data []byte) error {
|
||||
elements := []interface{}{}
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
set.Clear()
|
||||
set.Add(elements...)
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package treeset
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Set)(nil)
|
||||
var _ containers.JSONDeserializer = (*Set)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (set *Set) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(set.Values())
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (set *Set) FromJSON(data []byte) error {
|
||||
elements := []interface{}{}
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
set.Clear()
|
||||
set.Add(elements...)
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package arraystack
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Stack)(nil)
|
||||
var _ containers.JSONDeserializer = (*Stack)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (stack *Stack) ToJSON() ([]byte, error) {
|
||||
return stack.list.ToJSON()
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (stack *Stack) FromJSON(data []byte) error {
|
||||
return stack.list.FromJSON(data)
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package linkedliststack
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Stack)(nil)
|
||||
var _ containers.JSONDeserializer = (*Stack)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (stack *Stack) ToJSON() ([]byte, error) {
|
||||
return stack.list.ToJSON()
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (stack *Stack) FromJSON(data []byte) error {
|
||||
return stack.list.FromJSON(data)
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package avltree
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Tree)(nil)
|
||||
var _ containers.JSONDeserializer = (*Tree)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (tree *Tree) ToJSON() ([]byte, error) {
|
||||
elements := make(map[string]interface{})
|
||||
it := tree.Iterator()
|
||||
for it.Next() {
|
||||
elements[utils.ToString(it.Key())] = it.Value()
|
||||
}
|
||||
return json.Marshal(&elements)
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (tree *Tree) FromJSON(data []byte) error {
|
||||
elements := make(map[string]interface{})
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
tree.Clear()
|
||||
for key, value := range elements {
|
||||
tree.Put(key, value)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binaryheap
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Heap)(nil)
|
||||
var _ containers.JSONDeserializer = (*Heap)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (heap *Heap) ToJSON() ([]byte, error) {
|
||||
return heap.list.ToJSON()
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (heap *Heap) FromJSON(data []byte) error {
|
||||
return heap.list.FromJSON(data)
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btree
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Tree)(nil)
|
||||
var _ containers.JSONDeserializer = (*Tree)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (tree *Tree) ToJSON() ([]byte, error) {
|
||||
elements := make(map[string]interface{})
|
||||
it := tree.Iterator()
|
||||
for it.Next() {
|
||||
elements[utils.ToString(it.Key())] = it.Value()
|
||||
}
|
||||
return json.Marshal(&elements)
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (tree *Tree) FromJSON(data []byte) error {
|
||||
elements := make(map[string]interface{})
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
tree.Clear()
|
||||
for key, value := range elements {
|
||||
tree.Put(key, value)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package redblacktree
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/emirpasic/gods/containers"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Tree)(nil)
|
||||
var _ containers.JSONDeserializer = (*Tree)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (tree *Tree) ToJSON() ([]byte, error) {
|
||||
elements := make(map[string]interface{})
|
||||
it := tree.Iterator()
|
||||
for it.Next() {
|
||||
elements[utils.ToString(it.Key())] = it.Value()
|
||||
}
|
||||
return json.Marshal(&elements)
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (tree *Tree) FromJSON(data []byte) error {
|
||||
elements := make(map[string]interface{})
|
||||
err := json.Unmarshal(data, &elements)
|
||||
if err == nil {
|
||||
tree.Clear()
|
||||
for key, value := range elements {
|
||||
tree.Put(key, value)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToStringInts(t *testing.T) {
|
||||
var value interface{}
|
||||
|
||||
value = int8(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = int16(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = int32(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = int64(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = rune(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringUInts(t *testing.T) {
|
||||
var value interface{}
|
||||
|
||||
value = uint8(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = uint16(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = uint32(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = uint64(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = byte(1)
|
||||
if actualValue, expectedValue := ToString(value), "1"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringFloats(t *testing.T) {
|
||||
var value interface{}
|
||||
|
||||
value = float32(1.123456)
|
||||
if actualValue, expectedValue := ToString(value), "1.123456"; !strings.HasPrefix(actualValue, expectedValue) {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
value = float32(1.123456)
|
||||
if actualValue, expectedValue := ToString(value), "1.123456"; !strings.HasPrefix(actualValue, expectedValue) {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringOther(t *testing.T) {
|
||||
var value interface{}
|
||||
|
||||
value = "abc"
|
||||
if actualValue, expectedValue := ToString(value), "abc"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
value = true
|
||||
if actualValue, expectedValue := ToString(value), "true"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
|
||||
type T struct {
|
||||
id int
|
||||
name string
|
||||
}
|
||||
|
||||
if actualValue, expectedValue := ToString(T{1, "abc"}), "{id:1 name:abc}"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue