diff --git a/dynamic_benchmark_test.go b/benchmark_test.go similarity index 100% rename from dynamic_benchmark_test.go rename to benchmark_test.go diff --git a/dynamic_test.go b/dynamic_test.go index 41e7a35..b564224 100644 --- a/dynamic_test.go +++ b/dynamic_test.go @@ -2,13 +2,11 @@ package dynamic import ( "encoding/json" - "fmt" "testing" ) var _ (json.Marshaler) = (*Type)(nil) var _ (json.Unmarshaler) = (*Type)(nil) - var _ (json.Unmarshaler) = (*Data)(nil) type a struct { @@ -43,101 +41,6 @@ func TestNameDuplicated(t *testing.T) { Register("a", createB) } -func ExampleType_unmarshal() { - var test Type - data := ` -{ - "type": "a", - "A": "This is A" -}` - - if err := json.Unmarshal([]byte(data), &test); err != nil { - fmt.Printf("%v\n", err) - return - } - fmt.Printf("%#v\n", test.Value()) - // Output: - // &dynamic.a{A:"This is A"} -} - -func ExampleType_unmarshalStruct() { - var test struct { - X Type - } - data := ` -{ - "X": { - "type": "a", - "A": "This is A" - } -}` - if err := json.Unmarshal([]byte(data), &test); err != nil { - fmt.Printf("%v\n", err) - return - } - fmt.Printf("%#v\n", test.X.Value()) - // Output: - // &dynamic.a{A:"This is A"} -} - -func ExampleType_unmarshalList() { - var test []Type - data := ` -[ - { - "type": "a", - "A": "This is A", - "C": "This is C" - }, - { - "Type": "b", - "A": "This is A", - "B": "This is B" - } -]` - - if err := json.Unmarshal([]byte(data), &test); err != nil { - fmt.Printf("%v\n", err) - return - } - for _, t := range test { - fmt.Printf("%#v\n", t.Value()) - } - // Output: - // &dynamic.a{A:"This is A"} - // &dynamic.b{B:"This is B"} -} - -func ExampleType_unmarshalListInStruct() { - var test struct { - X []Type - } - data := ` -{ - "X": [ - { - "type": "a", - "A": "This is A" - }, - { - "Type": "b", - "A": "This is A", - "B": "This is B" - } - ] -}` - if err := json.Unmarshal([]byte(data), &test); err != nil { - fmt.Printf("%v\n", err) - return - } - for _, t := range test.X { - fmt.Printf("%#v\n", t.Value()) - } - // Output: - // &dynamic.a{A:"This is A"} - // &dynamic.b{B:"This is B"} -} - func TestUnmarshalUnsupportedType(t *testing.T) { var test Type data := ` @@ -207,55 +110,3 @@ func TestUnmarshalEmbeddedStruct(t *testing.T) { } } } - -func ExampleType_marshal() { - var test Type - - test.SetValue(&a{"This is A"}) - data, err := json.Marshal(&test) - if err != nil { - fmt.Printf("%v\n", err) - return - } - fmt.Printf("%s\n", data) - // Output: - // {"A":"This is A"} -} - -func ExampleType_marshalStruct() { - var test struct { - X Type - Y Type - } - - test.X.SetValue(&a{"This is A"}) - test.Y.SetValue(&b{"This is B"}) - data, err := json.Marshal(&test) - if err != nil { - fmt.Printf("%v\n", err) - return - } - fmt.Printf("%s\n", data) - // Output: - // {"X":{"A":"This is A"},"Y":{"B":"This is B"}} -} - -func ExampleType_marshalListInStruct() { - var test struct { - X []Type - } - - test.X = []Type{ - Type{&a{"This is A"}}, - Type{&b{"This is B"}}, - } - - data, err := json.Marshal(&test) - if err != nil { - fmt.Printf("%v\n", err) - return - } - fmt.Printf("%s\n", data) - // Output: - // {"X":[{"A":"This is A"},{"B":"This is B"}]} -} diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..3a36ff5 --- /dev/null +++ b/example_test.go @@ -0,0 +1,165 @@ +package dynamic_test + +import ( + "encoding/json" + "fmt" + + "github.com/goburrow/dynamic" +) + +type Gopher struct { + G string +} + +type Breaver struct { + B string +} + +func init() { + dynamic.Register("go", func() interface{} { return &Gopher{} }) + dynamic.Register("br", func() interface{} { return &Breaver{} }) +} + +func ExampleType_unmarshal() { + var test dynamic.Type + data := ` +{ + "type": "go", + "G": "gopher" +}` + + if err := json.Unmarshal([]byte(data), &test); err != nil { + fmt.Printf("%v\n", err) + return + } + fmt.Printf("%#v\n", test.Value()) + // Output: + // &dynamic_test.Gopher{G:"gopher"} +} + +func ExampleType_unmarshalStruct() { + var test struct { + X dynamic.Type + } + data := ` +{ + "X": { + "type": "go", + "G": "gopher" + } +}` + if err := json.Unmarshal([]byte(data), &test); err != nil { + fmt.Printf("%v\n", err) + return + } + fmt.Printf("%#v\n", test.X.Value()) + // Output: + // &dynamic_test.Gopher{G:"gopher"} +} + +func ExampleType_unmarshalList() { + var test []dynamic.Type + data := ` +[ + { + "type": "go", + "G": "gopher" + }, + { + "Type": "br", + "B": "breaver" + } +]` + + if err := json.Unmarshal([]byte(data), &test); err != nil { + fmt.Printf("%v\n", err) + return + } + for _, t := range test { + fmt.Printf("%#v\n", t.Value()) + } + // Output: + // &dynamic_test.Gopher{G:"gopher"} + // &dynamic_test.Breaver{B:"breaver"} +} + +func ExampleType_unmarshalListInStruct() { + var test struct { + X []dynamic.Type + } + data := ` +{ + "X": [ + { + "Type": "go", + "G": "gopher" + }, + { + "type": "br", + "B": "breaver" + } + ] +}` + if err := json.Unmarshal([]byte(data), &test); err != nil { + fmt.Printf("%v\n", err) + return + } + for _, t := range test.X { + fmt.Printf("%#v\n", t.Value()) + } + // Output: + // &dynamic_test.Gopher{G:"gopher"} + // &dynamic_test.Breaver{B:"breaver"} +} + +func ExampleType_marshal() { + var test dynamic.Type + + test.SetValue(&Gopher{"gopher"}) + data, err := json.Marshal(&test) + if err != nil { + fmt.Printf("%v\n", err) + return + } + fmt.Printf("%s\n", data) + // Output: + // {"G":"gopher"} +} + +func ExampleType_marshalStruct() { + var test struct { + X dynamic.Type + Y dynamic.Type + } + + test.X.SetValue(&Gopher{"gopher"}) + test.Y.SetValue(&Breaver{"breaver"}) + data, err := json.Marshal(&test) + if err != nil { + fmt.Printf("%v\n", err) + return + } + fmt.Printf("%s\n", data) + // Output: + // {"X":{"G":"gopher"},"Y":{"B":"breaver"}} +} + +func ExampleType_marshalListInStruct() { + var test struct { + X []dynamic.Type + } + + test.X = []dynamic.Type{ + dynamic.Type{&Gopher{"gopher"}}, + dynamic.Type{&Breaver{"breaver"}}, + } + + data, err := json.Marshal(&test) + if err != nil { + fmt.Printf("%v\n", err) + return + } + fmt.Printf("%s\n", data) + // Output: + // {"X":[{"G":"gopher"},{"B":"breaver"}]} +}