7
7
"math"
8
8
"net"
9
9
"os"
10
+ "strconv"
10
11
"strings"
11
12
"testing"
12
13
"time"
@@ -399,11 +400,13 @@ type (
399
400
food struct { F []string }
400
401
fun func ()
401
402
cplx complex128
403
+ ints []int
402
404
403
405
sound2 struct { S string }
404
406
food2 struct { F []string }
405
407
fun2 func ()
406
408
cplx2 complex128
409
+ ints2 []int
407
410
)
408
411
409
412
// This is intentionally wrong (pointer receiver)
@@ -415,6 +418,26 @@ func (c cplx) MarshalText() ([]byte, error) {
415
418
return []byte (fmt .Sprintf ("(%f+%fi)" , real (cplx ), imag (cplx ))), nil
416
419
}
417
420
421
+ func intsValue (is []int ) []byte {
422
+ var buf bytes.Buffer
423
+ buf .WriteByte ('<' )
424
+ for i , v := range is {
425
+ if i > 0 {
426
+ buf .WriteByte (',' )
427
+ }
428
+ buf .WriteString (strconv .Itoa (v ))
429
+ }
430
+ buf .WriteByte ('>' )
431
+ return buf .Bytes ()
432
+ }
433
+
434
+ func (is * ints ) MarshalText () ([]byte , error ) {
435
+ if is == nil {
436
+ return []byte ("[]" ), nil
437
+ }
438
+ return intsValue (* is ), nil
439
+ }
440
+
418
441
func (s * sound2 ) MarshalTOML () ([]byte , error ) { return []byte ("\" " + s .S + "\" " ), nil }
419
442
func (f food2 ) MarshalTOML () ([]byte , error ) {
420
443
return []byte ("[\" " + strings .Join (f .F , "\" , \" " ) + "\" ]" ), nil
@@ -424,6 +447,13 @@ func (c cplx2) MarshalTOML() ([]byte, error) {
424
447
cplx := complex128 (c )
425
448
return []byte (fmt .Sprintf ("\" (%f+%fi)\" " , real (cplx ), imag (cplx ))), nil
426
449
}
450
+ func (is * ints2 ) MarshalTOML () ([]byte , error ) {
451
+ // MarshalTOML must quote by self
452
+ if is == nil {
453
+ return []byte (`"[]"` ), nil
454
+ }
455
+ return []byte (fmt .Sprintf (`"%s"` , intsValue (* is ))), nil
456
+ }
427
457
428
458
func TestEncodeTextMarshaler (t * testing.T ) {
429
459
x := struct {
@@ -435,6 +465,8 @@ func TestEncodeTextMarshaler(t *testing.T) {
435
465
Food2 * food
436
466
Complex cplx
437
467
Fun fun
468
+ Ints ints
469
+ Ints2 * ints2
438
470
}{
439
471
Name : "Goblok" ,
440
472
Sound : sound {"miauw" },
@@ -447,26 +479,28 @@ func TestEncodeTextMarshaler(t *testing.T) {
447
479
Food2 : & food {[]string {"chicken" , "fish" }},
448
480
Complex : complex (42 , 666 ),
449
481
Fun : func () { panic ("x" ) },
482
+ Ints : ints {1 , 2 , 3 , 4 },
483
+ Ints2 : & ints2 {1 , 2 , 3 , 4 },
450
484
}
451
485
452
486
var buf bytes.Buffer
453
- if err := NewEncoder (& buf ).Encode (x ); err != nil {
487
+ if err := NewEncoder (& buf ).Encode (& x ); err != nil {
454
488
t .Fatal (err )
455
489
}
456
490
457
491
want := `Name = "Goblok"
492
+ Sound = "miauw"
458
493
Sound2 = "miauw"
459
494
Food = "chicken, fish"
460
495
Food2 = "chicken, fish"
461
496
Complex = "(42.000000+666.000000i)"
462
497
Fun = "why would you do this?"
498
+ Ints = "<1,2,3,4>"
499
+ Ints2 = "<1,2,3,4>"
463
500
464
501
[Labels]
465
502
color = "black"
466
503
type = "cat"
467
-
468
- [Sound]
469
- S = "miauw"
470
504
`
471
505
472
506
if buf .String () != want {
0 commit comments