1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
// Copyright 2023 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package transform_test
import (
"fmt"
"golang.org/x/exp/slices"
. "gopkg.in/check.v1"
"github.com/juju/collections/transform"
)
type mapSuite struct{}
var _ = Suite(mapSuite{})
func ExampleMapToSlice() {
peopleStatus := map[string]string{
"wallyworld": "peachy",
"bob": "happy",
}
flat := transform.MapToSlice(peopleStatus, func(k, v string) []string {
return []string{k, v}
})
slices.Sort(flat)
fmt.Println(flat)
// Output:
// [bob happy peachy wallyworld]
}
func (mapSuite) TestEmptyMapToSlice(c *C) {
m := map[string]string{}
to := transform.MapToSlice(m, func(k, v string) []any { return []any{k, v} })
c.Assert(len(to), Equals, 0)
}
func (mapSuite) TestMapToSlice(c *C) {
m := map[string]string{
"a": "b",
"c": "d",
}
to := transform.MapToSlice(m, func(k, v string) []string { return []string{k, v} })
slices.Sort(to)
c.Assert(to, DeepEquals, []string{"a", "b", "c", "d"})
}
func (mapSuite) TestEmptyMapTransformEmpty(c *C) {
m := map[string]string{}
to := transform.Map(m, func(k, v string) (string, any) {
return k, v
})
c.Assert(len(to), Equals, 0)
}
func (mapSuite) TestEmptyMapTransform(c *C) {
m := map[string]string{
"one": "two",
"three": "four",
}
to := transform.Map(m, func(k, v string) (string, int) {
if v == "two" {
return k, 2
}
return k, 4
})
c.Assert(to, DeepEquals, map[string]int{
"one": 2,
"three": 4,
})
}
|