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/_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..1c0d0b8 --- /dev/null +++ b/example_test.go @@ -0,0 +1,100 @@ +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}] +} + +func ExampleMapSlice_Keys() { + ms := mapslice.MapSlice{ + mapslice.MapItem{Key: "abc", Value: 123}, + mapslice.MapItem{Key: "def", Value: 456}, + mapslice.MapItem{Key: "ghi", Value: 789}, + } + + fmt.Println(ms.Keys()) + + // Output: + // [abc def ghi] +} + +func ExampleMapSlice_KeysString() { + ms := mapslice.MapSlice{ + mapslice.MapItem{Key: "abc", Value: 123}, + mapslice.MapItem{Key: "def", Value: 456}, + mapslice.MapItem{Key: "ghi", Value: 789}, + } + + fmt.Println(ms.Keys()) + + // Output: + // [abc def ghi] +} + +func ExampleMapSlice_Values() { + ms := mapslice.MapSlice{ + mapslice.MapItem{Key: "abc", Value: 123}, + mapslice.MapItem{Key: "def", Value: 456}, + mapslice.MapItem{Key: "ghi", Value: 789}, + } + + fmt.Println(ms.Values()) + + // Output: + // [123 456 789] +} diff --git a/mapslice.go b/mapslice.go index cdd4291..78aedf4 100644 --- a/mapslice.go +++ b/mapslice.go @@ -27,6 +27,33 @@ func nextIndex() uint64 { return indexCounter } +// Keys returns a MapSlice's Keys in a slice. +func (ms MapSlice) Keys() []interface{} { + s := make([]interface{}, len(ms)) + for i, k := range ms { + s[i] = k.Key + } + return s +} + +// KeysString returns MapSlice's Keys as []string. +func (ms MapSlice) KeysString() []string { + s := make([]string, len(ms)) + for i, k := range ms { + s[i] = fmt.Sprintf("%s", k.Key) + } + return s +} + +// Values returns a MapSlice's values in a slice. +func (ms MapSlice) Values() []interface{} { + s := make([]interface{}, len(ms)) + for i, k := range ms { + s[i] = k.Value + } + return s +} + // MapItem as a string. func (mi MapItem) String() string { return fmt.Sprintf("{%v %v}", mi.Key, mi.Value) diff --git a/mapslice_test.go b/mapslice_test.go index f451a62..2d238e0 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) }