Skip to content

Add walking for anonymous structs #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ type I struct {
I interface{} `json:"i"`
}

// we have to create an alias for 'any' so that we can embedd it in to J as an exported type
type EmbeddedAlias any

type J struct {
EmbeddedAlias `json:"embedded"`
}

var _ = Describe("JSONPatch", func() {
Context("CreateJsonPatch_pointer_values", func() {
It("pointer", func() {
Expand Down Expand Up @@ -341,6 +348,43 @@ var _ = Describe("JSONPatch", func() {
testPatchWithExpected(H{Ignored: "new", NotIgnored: "new"}, H{Ignored: "old", NotIgnored: "old"}, H{Ignored: "old", NotIgnored: "new"})
})
})
Context("CreateJsonPatch_embedded", func() {
It("primitive", func() {
// add
testPatch(J{"2"}, J{""})
// replace
testPatch(J{"2"}, J{"1"})
// remove // removing an embedded base will remove the whole JSON object
testPatchWithExpected(J{""}, J{"2"}, struct{}{})
// no change
testPatch(J{}, J{})
testPatch(J{""}, J{""})
testPatch(J{"1"}, J{"1"})
})
It("slice", func() {
// add
testPatch(J{[]string{"1"}}, J{[]string{}})
// replace
testPatch(J{[]string{"2"}}, J{[]string{"1"}})
// remove
testPatch(J{[]string{}}, J{[]string{"2"}})
// no change
testPatch(J{[]string{}}, J{[]string{}})
testPatch(J{[]string{""}}, J{[]string{""}})
testPatch(J{[]string{"1"}}, J{[]string{"1"}})
})
It("struct", func() {
// add
testPatch(J{C{Str: "1"}}, J{C{}})
// replace
testPatch(J{C{Str: "2"}}, J{C{Str: "1"}})
// remove
testPatch(J{C{}}, J{C{Str: "1"}})
// no change
testPatch(J{C{}}, J{C{}})
testPatch(J{C{Str: "2"}}, J{C{Str: "2"}})
})
})
Context("CreateJsonPatch_with_predicates", func() {
var predicate jsonpatch.Predicate
BeforeEach(func() {
Expand Down
23 changes: 19 additions & 4 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func (w *walker) processStruct(modified, current reflect.Value, pointer JSONPoin
return nil
}

if modified.Type().PkgPath() == "time" && modified.Type().Name() == "Time" {
var modifiedType = modified.Type()
if modifiedType.PkgPath() == "time" && modifiedType.Name() == "Time" {
m, err := toTimeStrValue(modified)
if err != nil {
return err
Expand All @@ -279,13 +280,27 @@ func (w *walker) processStruct(modified, current reflect.Value, pointer JSONPoin

// process all struct fields, the order of the fields of the modified and current JSON object is identical because their types match
for j := 0; j < modified.NumField(); j++ {
tag := strings.Split(modified.Type().Field(j).Tag.Get(jsonTag), ",")[0]
if tag == "" || tag == "_" || !modified.Field(j).CanInterface() {
var modifiedField = modified.Field(j)
var modifiedFieldType = modifiedType.Field(j)

tag := strings.Split(modifiedFieldType.Tag.Get(jsonTag), ",")[0]

// allow the tag to be missing if the field is anonymous
haveTagOrIsAnonymous := tag != "" || modifiedFieldType.Anonymous

if !haveTagOrIsAnonymous || tag == "_" || !modifiedFieldType.IsExported() {
// struct fields without a JSON tag set or unexported fields are ignored
continue
}

var p2 = pointer
// update the pointer unless the tag is empty
if tag != "" {
p2 = pointer.Add(tag)
}

// process the child's value of the modified and current JSON in a next step
if err := w.walk(modified.Field(j), current.Field(j), pointer.Add(tag)); err != nil {
if err := w.walk(modifiedField, current.Field(j), p2); err != nil {
return err
}
}
Expand Down