github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/cli/util/dynamic_test.go (about) 1 package util 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/davecgh/go-spew/spew" 9 goregistry "github.com/tickoalcantara12/micro/v3/service/registry" 10 ) 11 12 type parseCase struct { 13 args []string 14 values *goregistry.Value 15 expected map[string]interface{} 16 } 17 18 func TestDynamicFlagParsing(t *testing.T) { 19 cases := []parseCase{ 20 { 21 args: []string{"--ss=a,b"}, 22 values: &goregistry.Value{ 23 Values: []*goregistry.Value{ 24 { 25 Name: "ss", 26 Type: "[]string", 27 }, 28 }, 29 }, 30 expected: map[string]interface{}{ 31 "ss": []interface{}{"a", "b"}, 32 }, 33 }, 34 { 35 args: []string{"--ss", "a,b"}, 36 values: &goregistry.Value{ 37 Values: []*goregistry.Value{ 38 { 39 Name: "ss", 40 Type: "[]string", 41 }, 42 }, 43 }, 44 expected: map[string]interface{}{ 45 "ss": []interface{}{"a", "b"}, 46 }, 47 }, 48 { 49 args: []string{"--ss=a", "--ss=b"}, 50 values: &goregistry.Value{ 51 Values: []*goregistry.Value{ 52 { 53 Name: "ss", 54 Type: "[]string", 55 }, 56 }, 57 }, 58 expected: map[string]interface{}{ 59 "ss": []interface{}{"a", "b"}, 60 }, 61 }, 62 { 63 args: []string{"--ss", "a", "--ss", "b"}, 64 values: &goregistry.Value{ 65 Values: []*goregistry.Value{ 66 { 67 Name: "ss", 68 Type: "[]string", 69 }, 70 }, 71 }, 72 expected: map[string]interface{}{ 73 "ss": []interface{}{"a", "b"}, 74 }, 75 }, 76 { 77 args: []string{"--bs=true,false"}, 78 values: &goregistry.Value{ 79 Values: []*goregistry.Value{ 80 { 81 Name: "bs", 82 Type: "[]bool", 83 }, 84 }, 85 }, 86 expected: map[string]interface{}{ 87 "bs": []interface{}{true, false}, 88 }, 89 }, 90 { 91 args: []string{"--bs", "true,false"}, 92 values: &goregistry.Value{ 93 Values: []*goregistry.Value{ 94 { 95 Name: "bs", 96 Type: "[]bool", 97 }, 98 }, 99 }, 100 expected: map[string]interface{}{ 101 "bs": []interface{}{true, false}, 102 }, 103 }, 104 { 105 args: []string{"--bs=true", "--bs=false"}, 106 values: &goregistry.Value{ 107 Values: []*goregistry.Value{ 108 { 109 Name: "bs", 110 Type: "[]bool", 111 }, 112 }, 113 }, 114 expected: map[string]interface{}{ 115 "bs": []interface{}{true, false}, 116 }, 117 }, 118 { 119 args: []string{"--bs", "true", "--bs", "false"}, 120 values: &goregistry.Value{ 121 Values: []*goregistry.Value{ 122 { 123 Name: "bs", 124 Type: "[]bool", 125 }, 126 }, 127 }, 128 expected: map[string]interface{}{ 129 "bs": []interface{}{true, false}, 130 }, 131 }, 132 { 133 args: []string{"--is=10,20"}, 134 values: &goregistry.Value{ 135 Values: []*goregistry.Value{ 136 { 137 Name: "is", 138 Type: "[]int32", 139 }, 140 }, 141 }, 142 expected: map[string]interface{}{ 143 "is": []interface{}{int32(10), int32(20)}, 144 }, 145 }, 146 { 147 args: []string{"--is", "10,20"}, 148 values: &goregistry.Value{ 149 Values: []*goregistry.Value{ 150 { 151 Name: "is", 152 Type: "[]int32", 153 }, 154 }, 155 }, 156 expected: map[string]interface{}{ 157 "is": []interface{}{int32(10), int32(20)}, 158 }, 159 }, 160 { 161 args: []string{"--is=10", "--is=20"}, 162 values: &goregistry.Value{ 163 Values: []*goregistry.Value{ 164 { 165 Name: "is", 166 Type: "[]int32", 167 }, 168 }, 169 }, 170 expected: map[string]interface{}{ 171 "is": []interface{}{int32(10), int32(20)}, 172 }, 173 }, 174 { 175 args: []string{"--is", "10", "--is", "20"}, 176 values: &goregistry.Value{ 177 Values: []*goregistry.Value{ 178 { 179 Name: "is", 180 Type: "[]int32", 181 }, 182 }, 183 }, 184 expected: map[string]interface{}{ 185 "is": []interface{}{int32(10), int32(20)}, 186 }, 187 }, 188 { 189 args: []string{"--is=10,20"}, 190 values: &goregistry.Value{ 191 Values: []*goregistry.Value{ 192 { 193 Name: "is", 194 Type: "[]int64", 195 }, 196 }, 197 }, 198 expected: map[string]interface{}{ 199 "is": []interface{}{int64(10), int64(20)}, 200 }, 201 }, 202 { 203 args: []string{"--is", "10,20"}, 204 values: &goregistry.Value{ 205 Values: []*goregistry.Value{ 206 { 207 Name: "is", 208 Type: "[]int64", 209 }, 210 }, 211 }, 212 expected: map[string]interface{}{ 213 "is": []interface{}{int64(10), int64(20)}, 214 }, 215 }, 216 { 217 args: []string{"--is=10", "--is=20"}, 218 values: &goregistry.Value{ 219 Values: []*goregistry.Value{ 220 { 221 Name: "is", 222 Type: "[]int64", 223 }, 224 }, 225 }, 226 expected: map[string]interface{}{ 227 "is": []interface{}{int64(10), int64(20)}, 228 }, 229 }, 230 { 231 args: []string{"--is", "10", "--is", "20"}, 232 values: &goregistry.Value{ 233 Values: []*goregistry.Value{ 234 { 235 Name: "is", 236 Type: "[]int64", 237 }, 238 }, 239 }, 240 expected: map[string]interface{}{ 241 "is": []interface{}{int64(10), int64(20)}, 242 }, 243 }, 244 { 245 args: []string{"--fs=10.1,20.2"}, 246 values: &goregistry.Value{ 247 Values: []*goregistry.Value{ 248 { 249 Name: "fs", 250 Type: "[]float64", 251 }, 252 }, 253 }, 254 expected: map[string]interface{}{ 255 "fs": []interface{}{float64(10.1), float64(20.2)}, 256 }, 257 }, 258 { 259 args: []string{"--fs", "10.1,20.2"}, 260 values: &goregistry.Value{ 261 Values: []*goregistry.Value{ 262 { 263 Name: "fs", 264 Type: "[]float64", 265 }, 266 }, 267 }, 268 expected: map[string]interface{}{ 269 "fs": []interface{}{float64(10.1), float64(20.2)}, 270 }, 271 }, 272 { 273 args: []string{"--fs=10.1", "--fs=20.2"}, 274 values: &goregistry.Value{ 275 Values: []*goregistry.Value{ 276 { 277 Name: "fs", 278 Type: "[]float64", 279 }, 280 }, 281 }, 282 expected: map[string]interface{}{ 283 "fs": []interface{}{float64(10.1), float64(20.2)}, 284 }, 285 }, 286 { 287 args: []string{"--fs", "10.1", "--fs", "20.2"}, 288 values: &goregistry.Value{ 289 Values: []*goregistry.Value{ 290 { 291 Name: "fs", 292 Type: "[]float64", 293 }, 294 }, 295 }, 296 expected: map[string]interface{}{ 297 "fs": []interface{}{float64(10.1), float64(20.2)}, 298 }, 299 }, 300 { 301 args: []string{"--user_email=someemail"}, 302 values: &goregistry.Value{ 303 Values: []*goregistry.Value{ 304 { 305 Name: "user_email", 306 Type: "string", 307 }, 308 }, 309 }, 310 expected: map[string]interface{}{ 311 "user_email": "someemail", 312 }, 313 }, 314 { 315 args: []string{"--user_email=someemail", "--user_name=somename"}, 316 values: &goregistry.Value{ 317 Values: []*goregistry.Value{ 318 { 319 Name: "user_email", 320 Type: "string", 321 }, 322 { 323 Name: "user_name", 324 Type: "string", 325 }, 326 }, 327 }, 328 expected: map[string]interface{}{ 329 "user_email": "someemail", 330 "user_name": "somename", 331 }, 332 }, 333 { 334 args: []string{"--b"}, 335 values: &goregistry.Value{ 336 Values: []*goregistry.Value{ 337 { 338 Name: "b", 339 Type: "bool", 340 }, 341 }, 342 }, 343 expected: map[string]interface{}{ 344 "b": true, 345 }, 346 }, 347 { 348 args: []string{"--user_friend_email=hi"}, 349 values: &goregistry.Value{ 350 Values: []*goregistry.Value{ 351 { 352 Name: "user_friend_email", 353 Type: "string", 354 }, 355 }, 356 }, 357 expected: map[string]interface{}{ 358 "user_friend_email": "hi", 359 }, 360 }, 361 } 362 for _, c := range cases { 363 t.Run(strings.Join(c.args, " "), func(t *testing.T) { 364 _, flags, err := splitCmdArgs(c.args) 365 if err != nil { 366 t.Fatal(err) 367 } 368 req, err := FlagsToRequest(flags, c.values) 369 if err != nil { 370 t.Fatal(err) 371 } 372 if !reflect.DeepEqual(c.expected, req) { 373 spew.Dump("Expected:", c.expected, "got: ", req) 374 t.Fatalf("Expected %v, got %v", c.expected, req) 375 } 376 }) 377 378 } 379 }