@@ -348,6 +348,133 @@ func args(m reflect.Method) []reflect.Value {
348
348
return out
349
349
}
350
350
351
+ func TestMarshalJSON (t * testing.T ) {
352
+ for _ , tc := range []struct {
353
+ desc string
354
+ kvs []attribute.KeyValue
355
+ wantJSON string
356
+ }{
357
+ {
358
+ desc : "empty" ,
359
+ kvs : []attribute.KeyValue {},
360
+ wantJSON : `[]` ,
361
+ },
362
+ {
363
+ desc : "single string attribute" ,
364
+ kvs : []attribute.KeyValue {attribute .String ("A" , "a" )},
365
+ wantJSON : `[{"Key":"A","Value":{"Type":"STRING","Value":"a"}}]` ,
366
+ },
367
+ {
368
+ desc : "many mixed attributes" ,
369
+ kvs : []attribute.KeyValue {
370
+ attribute .Bool ("A" , true ),
371
+ attribute .BoolSlice ("B" , []bool {true , false }),
372
+ attribute .Int ("C" , 1 ),
373
+ attribute .IntSlice ("D" , []int {2 , 3 }),
374
+ attribute .Int64 ("E" , 22 ),
375
+ attribute .Int64Slice ("F" , []int64 {33 , 44 }),
376
+ attribute .Float64 ("G" , 1.1 ),
377
+ attribute .Float64Slice ("H" , []float64 {2.2 , 3.3 }),
378
+ attribute .String ("I" , "Z" ),
379
+ attribute .StringSlice ("J" , []string {"X" , "Y" }),
380
+ attribute .Stringer ("K" , & simpleStringer {val : "foo" }),
381
+ },
382
+ wantJSON : `[
383
+ {
384
+ "Key": "A",
385
+ "Value": {
386
+ "Type": "BOOL",
387
+ "Value": true
388
+ }
389
+ },
390
+ {
391
+ "Key": "B",
392
+ "Value": {
393
+ "Type": "BOOLSLICE",
394
+ "Value": [true, false]
395
+ }
396
+ },
397
+ {
398
+ "Key": "C",
399
+ "Value": {
400
+ "Type": "INT64",
401
+ "Value": 1
402
+ }
403
+ },
404
+ {
405
+ "Key": "D",
406
+ "Value": {
407
+ "Type": "INT64SLICE",
408
+ "Value": [2, 3]
409
+ }
410
+ },
411
+ {
412
+ "Key": "E",
413
+ "Value": {
414
+ "Type": "INT64",
415
+ "Value": 22
416
+ }
417
+ },
418
+ {
419
+ "Key": "F",
420
+ "Value": {
421
+ "Type": "INT64SLICE",
422
+ "Value": [33, 44]
423
+ }
424
+ },
425
+ {
426
+ "Key": "G",
427
+ "Value": {
428
+ "Type": "FLOAT64",
429
+ "Value": 1.1
430
+ }
431
+ },
432
+ {
433
+ "Key": "H",
434
+ "Value": {
435
+ "Type": "FLOAT64SLICE",
436
+ "Value": [2.2, 3.3]
437
+ }
438
+ },
439
+ {
440
+ "Key": "I",
441
+ "Value": {
442
+ "Type": "STRING",
443
+ "Value": "Z"
444
+ }
445
+ },
446
+ {
447
+ "Key": "J",
448
+ "Value": {
449
+ "Type": "STRINGSLICE",
450
+ "Value": ["X", "Y"]
451
+ }
452
+ },
453
+ {
454
+ "Key": "K",
455
+ "Value": {
456
+ "Type": "STRING",
457
+ "Value": "foo"
458
+ }
459
+ }
460
+ ]` ,
461
+ },
462
+ } {
463
+ t .Run (tc .desc , func (t * testing.T ) {
464
+ set := attribute .NewSet (tc .kvs ... )
465
+ by , err := set .MarshalJSON ()
466
+ require .NoError (t , err )
467
+ assert .JSONEq (t , tc .wantJSON , string (by ))
468
+ })
469
+ }
470
+ }
471
+
472
+ type simpleStringer struct {
473
+ val string
474
+ }
475
+
476
+ func (s * simpleStringer ) String () string { return s .val }
477
+
351
478
func BenchmarkFiltering (b * testing.B ) {
352
479
var kvs [26 ]attribute.KeyValue
353
480
buf := [1 ]byte {'A' - 1 }
0 commit comments