From b7430916ba9760397da310d290ee785cba85e737 Mon Sep 17 00:00:00 2001 From: Zamicol Date: Mon, 13 Jun 2022 11:59:58 -0600 Subject: [PATCH 1/5] use tick for json string; add test examples --- mapslice_test.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/mapslice_test.go b/mapslice_test.go index f451a62..789dfa4 100644 --- a/mapslice_test.go +++ b/mapslice_test.go @@ -18,7 +18,7 @@ func TestMarshal(t *testing.T) { t.Fatal(err) } - e := "{\"abc\":123,\"def\":456,\"ghi\":789}" + e := `{"abc":123,"def":456,"ghi":789}` r := string(b) if r != e { @@ -39,7 +39,7 @@ func TestMarshalError(t *testing.T) { func TestUnmarshal(t *testing.T) { ms := MapSlice{} - if err := json.Unmarshal([]byte("{\"abc\":123,\"def\":456,\"ghi\":789}"), &ms); err != nil { + if err := json.Unmarshal([]byte(`{"abc":123,"def":456,"ghi":789}`), &ms); err != nil { t.Fatal(err) } @@ -50,3 +50,33 @@ func TestUnmarshal(t *testing.T) { t.Errorf("expected: %s\ngot: %s", e, r) } } + +func ExampleMapSlice_MarshalJSON() { + ms := MapSlice{ + MapItem{"abc", 123, 0}, + MapItem{"def", 456, 0}, + MapItem{"ghi", 789, 0}, + } + + b, err := json.Marshal(ms) + if err != nil { + fmt.Println(err) + } + + fmt.Printf("%s", b) + + // Output: + // {"abc":123,"def":456,"ghi":789} +} + +func ExampleMapSlice_UnmarshalJSON() { + var ms = MapSlice{} + + if err := json.Unmarshal([]byte(`{"abc":123,"def":456,"ghi":789}`), &ms); err != nil { + fmt.Println(err) + } + + fmt.Println(ms) + // Output: + // [{abc 123} {def 456} {ghi 789}] +} From 89b7b48c5db9db469cd26168763e2d348e6e186e Mon Sep 17 00:00:00 2001 From: Zamicol Date: Fri, 22 Jul 2022 14:54:50 -0600 Subject: [PATCH 2/5] Update examples according to feedback on #1 --- mapslice_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mapslice_test.go b/mapslice_test.go index 789dfa4..aa2cc2e 100644 --- a/mapslice_test.go +++ b/mapslice_test.go @@ -53,14 +53,14 @@ func TestUnmarshal(t *testing.T) { func ExampleMapSlice_MarshalJSON() { ms := MapSlice{ - MapItem{"abc", 123, 0}, - MapItem{"def", 456, 0}, - MapItem{"ghi", 789, 0}, + MapItem{Key: "abc", Value: 123}, + MapItem{Key: "def", Value: 456}, + MapItem{Key: "ghi", Value: 789}, } b, err := json.Marshal(ms) if err != nil { - fmt.Println(err) + panic(err) } fmt.Printf("%s", b) @@ -73,10 +73,11 @@ func ExampleMapSlice_UnmarshalJSON() { var ms = MapSlice{} if err := json.Unmarshal([]byte(`{"abc":123,"def":456,"ghi":789}`), &ms); err != nil { - fmt.Println(err) + panic(err) } - fmt.Println(ms) + fmt.Printf("%s", ms) + // Output: // [{abc 123} {def 456} {ghi 789}] } From 38ce4e321e9f9f7ac36e4ce29bf132742ad6bbd6 Mon Sep 17 00:00:00 2001 From: Zamicol Date: Fri, 22 Jul 2022 14:54:50 -0600 Subject: [PATCH 3/5] Update examples according to feedback on #1 --- README.md | 2 ++ mapslice_test.go | 13 +++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e296a19..3a2f1e2 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ Go MapSlice for ordered marshal/ unmarshal of maps in JSON # Example +https://go.dev/play/p/wzWP5vufCvL + ```go package main diff --git a/mapslice_test.go b/mapslice_test.go index 789dfa4..aa2cc2e 100644 --- a/mapslice_test.go +++ b/mapslice_test.go @@ -53,14 +53,14 @@ func TestUnmarshal(t *testing.T) { func ExampleMapSlice_MarshalJSON() { ms := MapSlice{ - MapItem{"abc", 123, 0}, - MapItem{"def", 456, 0}, - MapItem{"ghi", 789, 0}, + MapItem{Key: "abc", Value: 123}, + MapItem{Key: "def", Value: 456}, + MapItem{Key: "ghi", Value: 789}, } b, err := json.Marshal(ms) if err != nil { - fmt.Println(err) + panic(err) } fmt.Printf("%s", b) @@ -73,10 +73,11 @@ func ExampleMapSlice_UnmarshalJSON() { var ms = MapSlice{} if err := json.Unmarshal([]byte(`{"abc":123,"def":456,"ghi":789}`), &ms); err != nil { - fmt.Println(err) + panic(err) } - fmt.Println(ms) + fmt.Printf("%s", ms) + // Output: // [{abc 123} {def 456} {ghi 789}] } From dfa5bc487f14ad272968c9e036b2bd1a71f9a843 Mon Sep 17 00:00:00 2001 From: Zamicol Date: Tue, 26 Jul 2022 15:29:37 -0600 Subject: [PATCH 4/5] remove _example directoy; add example_test.go --- _example/example.go | 30 ---------------------- example_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 30 deletions(-) delete mode 100644 _example/example.go create mode 100644 example_test.go diff --git a/_example/example.go b/_example/example.go deleted file mode 100644 index ee5e1f2..0000000 --- a/_example/example.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "log" - - "github.com/ake-persson/mapslice-json" -) - -func main() { - ms := mapslice.MapSlice{ - mapslice.MapItem{Key: "abc", Value: 123}, - mapslice.MapItem{Key: "def", Value: 456}, - mapslice.MapItem{Key: "ghi", Value: 789}, - } - - b, err := json.Marshal(ms) - if err != nil { - log.Fatal(err) - } - fmt.Println(string(b)) - - ms = mapslice.MapSlice{} - if err := json.Unmarshal(b, &ms); err != nil { - log.Fatal(err) - } - - fmt.Println(ms) -} diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..3e5775b --- /dev/null +++ b/example_test.go @@ -0,0 +1,61 @@ +package mapslice_test + +import ( + "encoding/json" + "fmt" + "log" + + "github.com/ake-persson/mapslice-json" +) + +func main() { + ms := mapslice.MapSlice{ + mapslice.MapItem{Key: "abc", Value: 123}, + mapslice.MapItem{Key: "def", Value: 456}, + mapslice.MapItem{Key: "ghi", Value: 789}, + } + + b, err := json.Marshal(ms) + if err != nil { + log.Fatal(err) + } + fmt.Println(string(b)) + + ms = mapslice.MapSlice{} + if err := json.Unmarshal(b, &ms); err != nil { + log.Fatal(err) + } + + fmt.Println(ms) +} + +func ExampleMapSlice_MarshalJSON() { + ms := mapslice.MapSlice{ + mapslice.MapItem{Key: "abc", Value: 123}, + mapslice.MapItem{Key: "def", Value: 456}, + mapslice.MapItem{Key: "ghi", Value: 789}, + } + + b, err := json.Marshal(ms) + if err != nil { + panic(err) + } + + fmt.Printf("%s", b) + + // Output: + // {"abc":123,"def":456,"ghi":789} +} + +func ExampleMapSlice_UnmarshalJSON() { + var ms = mapslice.MapSlice{} + + if err := json.Unmarshal([]byte(`{"abc":123,"def":456,"ghi":789}`), &ms); err != nil { + panic(err) + } + + fmt.Printf("%s", ms) + + // Output: + // [{abc 123} {def 456} {ghi 789}] +} From 99f7ec05f8278f30ff7ae9b26107ceb21e881a5e Mon Sep 17 00:00:00 2001 From: Zamicol Date: Tue, 26 Jul 2022 15:36:39 -0600 Subject: [PATCH 5/5] remove examples from mapslice_test.go --- mapslice_test.go | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/mapslice_test.go b/mapslice_test.go index aa2cc2e..2d238e0 100644 --- a/mapslice_test.go +++ b/mapslice_test.go @@ -50,34 +50,3 @@ func TestUnmarshal(t *testing.T) { t.Errorf("expected: %s\ngot: %s", e, r) } } - -func ExampleMapSlice_MarshalJSON() { - ms := MapSlice{ - MapItem{Key: "abc", Value: 123}, - MapItem{Key: "def", Value: 456}, - MapItem{Key: "ghi", Value: 789}, - } - - b, err := json.Marshal(ms) - if err != nil { - panic(err) - } - - fmt.Printf("%s", b) - - // Output: - // {"abc":123,"def":456,"ghi":789} -} - -func ExampleMapSlice_UnmarshalJSON() { - var ms = MapSlice{} - - if err := json.Unmarshal([]byte(`{"abc":123,"def":456,"ghi":789}`), &ms); err != nil { - panic(err) - } - - fmt.Printf("%s", ms) - - // Output: - // [{abc 123} {def 456} {ghi 789}] -}