github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/mapexamples/map_ToSlice_test.go (about)

     1  package mapexamples
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/m4gshm/gollections/map_"
    10  	"github.com/m4gshm/gollections/slice"
    11  )
    12  
    13  func Test_ToSlice(t *testing.T) {
    14  
    15  	var employers = map[string]map[string]string{
    16  		"devops": {"name": "Bob"},
    17  		"jun":    {"name": "Tom"},
    18  	}
    19  
    20  	var users = map_.ToSlice(employers, func(title string, employer map[string]string) User {
    21  		return User{name: employer["name"], roles: []Role{{name: title}}}
    22  	})
    23  	//[{name:Bob age:0 roles:[{name:devops}]} {name:Tom age:0 roles:[{name:jun}]}]
    24  
    25  	fmt.Printf("%+v\n", users)
    26  
    27  	assert.Equal(t,
    28  		[]User{
    29  			{name: "Bob", roles: []Role{{name: "devops"}}},
    30  			{name: "Tom", roles: []Role{{name: "jun"}}},
    31  		},
    32  		slice.SortAsc(users, User.Name),
    33  	)
    34  
    35  }