@@ -2,6 +2,7 @@ package sheriff
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"net"
6
7
"reflect"
7
8
"testing"
@@ -579,14 +580,46 @@ type TestMarshal_Embedded struct {
579
580
Foo string `json:"foo" groups:"test"`
580
581
}
581
582
583
+ // TestMarshal_EmbeddedCustom is used to test an embedded struct with a custom marshaler that is not a pointer.
584
+ type TestMarshal_EmbeddedCustom struct {
585
+ Val int
586
+ Set bool
587
+ }
588
+
589
+ func (t TestMarshal_EmbeddedCustom ) MarshalJSON () ([]byte , error ) {
590
+ if t .Set {
591
+ return []byte (fmt .Sprintf ("%d" , t .Val )), nil
592
+ }
593
+
594
+ return nil , nil
595
+ }
596
+
597
+ // TestMarshal_EmbeddedCustomPtr is used to test an embedded struct with a custom marshaler that is a pointer.
598
+ type TestMarshal_EmbeddedCustomPtr struct {
599
+ Val int
600
+ Set bool
601
+ }
602
+
603
+ func (t * TestMarshal_EmbeddedCustomPtr ) MarshalJSON () ([]byte , error ) {
604
+ if t .Set {
605
+ return []byte (fmt .Sprintf ("%d" , t .Val )), nil
606
+ }
607
+
608
+ return nil , nil
609
+ }
610
+
582
611
type TestMarshal_EmbeddedParent struct {
583
612
* TestMarshal_Embedded
584
- Bar string `json:"bar" groups:"test"`
613
+ * TestMarshal_EmbeddedCustom `json:"value"`
614
+ * TestMarshal_EmbeddedCustomPtr `json:"value_ptr"`
615
+ Bar string `json:"bar" groups:"test"`
585
616
}
586
617
587
618
func TestMarshal_EmbeddedField (t * testing.T ) {
588
619
v := TestMarshal_EmbeddedParent {
589
620
& TestMarshal_Embedded {"Hello" },
621
+ & TestMarshal_EmbeddedCustom {10 , true },
622
+ & TestMarshal_EmbeddedCustomPtr {20 , true },
590
623
"World" ,
591
624
}
592
625
o := & Options {Groups : []string {"test" }}
@@ -598,8 +631,10 @@ func TestMarshal_EmbeddedField(t *testing.T) {
598
631
assert .NoError (t , err )
599
632
600
633
expected , err := json .Marshal (map [string ]interface {}{
601
- "bar" : "World" ,
602
- "foo" : "Hello" ,
634
+ "bar" : "World" ,
635
+ "foo" : "Hello" ,
636
+ "value" : 10 ,
637
+ "value_ptr" : 20 ,
603
638
})
604
639
assert .NoError (t , err )
605
640
0 commit comments