github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/sliceexamples/complex_slice_examples/complex_slice_examples_test.go (about) 1 package sliceexamples 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/m4gshm/gollections/collection/immutable/set" 10 11 "github.com/m4gshm/gollections/loop" 12 "github.com/m4gshm/gollections/predicate/eq" 13 "github.com/m4gshm/gollections/predicate/match" 14 "github.com/m4gshm/gollections/predicate/not" 15 "github.com/m4gshm/gollections/predicate/where" 16 "github.com/m4gshm/gollections/slice" 17 "github.com/m4gshm/gollections/slice/convert" 18 "github.com/m4gshm/gollections/slice/group" 19 ) 20 21 type User struct { 22 name string 23 age int 24 roles []Role 25 } 26 27 type Role struct { 28 name string 29 } 30 31 func (r Role) Name() string { 32 return r.name 33 } 34 35 func (u User) Name() string { 36 return u.name 37 } 38 39 func (u User) Age() int { 40 return u.age 41 } 42 43 func (u User) Roles() []Role { 44 return u.roles 45 } 46 47 var users = []User{ 48 {name: "Bob", age: 26, roles: []Role{{"Admin"}, {"manager"}}}, 49 {name: "Alice", age: 35, roles: []Role{{"Manager"}}}, 50 {name: "Tom", age: 18}, 51 } 52 53 func Test_GroupBySeveralKeysAndConvertMapValues(t *testing.T) { 54 55 //new 56 namesByRole := group.ByMultipleKeys(users, func(u User) []string { 57 return convert.AndConvert(u.Roles(), Role.Name, strings.ToLower) 58 }, User.Name) 59 60 assert.Equal(t, namesByRole[""], []string{"Tom"}) 61 assert.Equal(t, namesByRole["manager"], []string{"Bob", "Alice"}) 62 assert.Equal(t, namesByRole["admin"], []string{"Bob"}) 63 64 //old 65 legacyNamesByRole := map[string][]string{} 66 for _, u := range users { 67 if roles := u.Roles(); len(roles) == 0 { 68 lr := "" 69 names := legacyNamesByRole[lr] 70 names = append(names, u.Name()) 71 legacyNamesByRole[lr] = names 72 } else { 73 for _, r := range roles { 74 lr := strings.ToLower(r.Name()) 75 names := legacyNamesByRole[lr] 76 names = append(names, u.Name()) 77 legacyNamesByRole[lr] = names 78 } 79 } 80 } 81 82 assert.Equal(t, legacyNamesByRole[""], []string{"Tom"}) 83 assert.Equal(t, legacyNamesByRole["manager"], []string{"Bob", "Alice"}) 84 assert.Equal(t, legacyNamesByRole["admin"], []string{"Bob"}) 85 } 86 87 func Test_FindFirsManager(t *testing.T) { 88 alice, ok := slice.First(users, match.Any(User.Roles, where.Eq(Role.Name, "Manager"))) 89 90 assert.True(t, ok) 91 assert.Equal(t, "Alice", alice.Name()) 92 93 //plain old 94 var legacyAlice *User 95 userLoop: 96 for _, u := range users { 97 for _, r := range u.Roles() { 98 if r.Name() == "Manager" { 99 legacyAlice = &u 100 break userLoop 101 } 102 } 103 } 104 ok = legacyAlice != nil 105 assert.True(t, ok) 106 assert.Equal(t, "Alice", legacyAlice.Name()) 107 } 108 109 func Benchmark_FindFirsManager_Predicate_ContainsConverted(b *testing.B) { 110 for i := 0; i < b.N; i++ { 111 alice, ok := slice.First(users, where.Any(User.Roles, where.Eq(Role.Name, "Manager"))) 112 _, _ = alice, ok 113 } 114 } 115 116 func Benchmark_FindFirsManager_Predicate_HasAnyConverted(b *testing.B) { 117 for i := 0; i < b.N; i++ { 118 alice, ok := slice.First(users, match.Any(User.Roles, match.To(Role.Name, func(roleName string) bool { return roleName == "manager" }))) 119 _, _ = alice, ok 120 } 121 } 122 123 func Benchmark_FindFirsManager_Set(b *testing.B) { 124 for i := 0; i < b.N; i++ { 125 alice, ok := slice.First(users, func(user User) bool { 126 return set.Convert(set.New(user.Roles()), Role.Name).HasAny(eq.To("Manager")) 127 }) 128 _, _ = alice, ok 129 } 130 } 131 132 func Benchmark_FindFirsManager_Slice(b *testing.B) { 133 for i := 0; i < b.N; i++ { 134 alice, ok := slice.First(users, func(user User) bool { 135 return slice.Contains(slice.Convert(user.Roles(), Role.Name), "Manager") 136 }) 137 _, _ = alice, ok 138 } 139 } 140 141 func Benchmark_FindFirsManager_Loop(b *testing.B) { 142 for i := 0; i < b.N; i++ { 143 alice, ok := slice.First(users, func(user User) bool { 144 return loop.Contains(loop.Convert(loop.Of(user.Roles()...), Role.Name), "Manager") 145 }) 146 _, _ = alice, ok 147 } 148 } 149 150 func Benchmark_FindFirsManager_Loop_HasAny(b *testing.B) { 151 for i := 0; i < b.N; i++ { 152 alice, ok := slice.First(users, func(user User) bool { 153 return loop.Convert(loop.Of(user.Roles()...), Role.Name).HasAny(eq.To("Manager")) 154 }) 155 _, _ = alice, ok 156 } 157 } 158 159 func Benchmark_FindFirsManager_Old(b *testing.B) { 160 for i := 0; i < b.N; i++ { 161 legacyAlice := User{} 162 ok := false 163 loopUsers: 164 for _, u := range users { 165 for _, r := range u.Roles() { 166 if r.Name() == "Manager" { 167 legacyAlice = u 168 ok = true 169 break loopUsers 170 } 171 } 172 } 173 _, _ = legacyAlice, ok 174 } 175 } 176 177 func Test_AggregateFilteredRoles(t *testing.T) { 178 //new 179 roles := slice.Flat(users, User.Roles) 180 roleNamesExceptManager := convert.AndFilter(roles, Role.Name, not.Eq("Manager")) 181 182 assert.Equal(t, slice.Of("Admin", "manager"), roleNamesExceptManager) 183 184 //plain old 185 legacyRoleNamesExceptManager := []string{} 186 for _, u := range users { 187 for _, r := range u.Roles() { 188 if n := r.Name(); n != "Manager" { 189 legacyRoleNamesExceptManager = append(legacyRoleNamesExceptManager, n) 190 } 191 } 192 } 193 assert.Equal(t, slice.Of("Admin", "manager"), legacyRoleNamesExceptManager) 194 } 195 196 func Benchmark_AggregateFilteredRoles_Slice(b *testing.B) { 197 for i := 0; i < b.N; i++ { 198 roles := slice.Flat(users, User.Roles) 199 roleNamesExceptManager := convert.AndFilter(roles, Role.Name, not.Eq("Manager")) 200 _ = roleNamesExceptManager 201 } 202 } 203 204 func Benchmark_AggregateFilteredRoles_Loop(b *testing.B) { 205 for i := 0; i < b.N; i++ { 206 roles := loop.Flat(loop.Of(users...), User.Roles) 207 roleNamesExceptManager := loop.Filter(loop.Convert(roles, Role.Name), not.Eq("Manager")) 208 _ = loop.Slice(roleNamesExceptManager) 209 } 210 } 211 212 func Benchmark_AggregateFilteredRoles_Old(b *testing.B) { 213 for i := 0; i < b.N; i++ { 214 legacyRoleNamesExceptManager := []string{} 215 for _, u := range users { 216 for _, r := range u.Roles() { 217 if n := r.Name(); n != "Manager" { 218 legacyRoleNamesExceptManager = append(legacyRoleNamesExceptManager, n) 219 } 220 } 221 } 222 _ = legacyRoleNamesExceptManager 223 } 224 }