Skip to content

Commit 362aca3

Browse files
authored
Add pkg/compare/Path.MarshalJSON function to implement json.Marshaler interface (#18)
Issue aws-controllers-k8s/community#772 Description of changes: - Add `MarshalJSON` function method to help encoding `pkg/comapare.Path` unexported fields By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 0ff9ffa commit 362aca3

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pkg/compare/path.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,32 @@
1313

1414
package compare
1515

16-
import "strings"
16+
import (
17+
"encoding/json"
18+
"strings"
19+
)
1720

1821
// Path provides a JSONPath-like struct and field-member "route" to a
19-
// particular field within a compared struct
22+
// particular field within a compared struct. Path implements json.Marshaler
23+
// interface.
2024
type Path struct {
2125
parts []string
2226
}
2327

28+
// MarshalJSON returns the JSON encoding of a Path object.
29+
func (p Path) MarshalJSON() ([]byte, error) {
30+
// Since json.Marshall doesn't encode unexported struct fields we have to
31+
// copy the Path instance into a new struct object with exported fields.
32+
// See https://github.com/aws-controllers-k8s/community/issues/772
33+
return json.Marshal(
34+
struct {
35+
Parts []string
36+
}{
37+
p.parts,
38+
},
39+
)
40+
}
41+
2442
// Push adds a new part to the Path.
2543
func (p Path) Push(part string) {
2644
p.parts = append(p.parts, part)

0 commit comments

Comments
 (0)