-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix reflect HasField() with anonymous pointer field. (#736)
Signed-off-by: Jeff Ortel <[email protected]>
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package reflect | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
) | ||
|
||
func TestHasField(t *testing.T) { | ||
g := gomega.NewGomegaWithT(t) | ||
type B struct { | ||
Name string | ||
Age string | ||
} | ||
type B2 struct { | ||
Name2 string | ||
Age2 string | ||
} | ||
type M struct { | ||
B | ||
*B2 | ||
Ptr *B | ||
Object B | ||
Int int | ||
IntPtr *int | ||
List []string | ||
} | ||
|
||
// Test expected. | ||
_, err := HasFields( | ||
&M{B2: &B2{}}, | ||
"Name", | ||
"Age", | ||
"Name2", | ||
"Age2", | ||
"Ptr", | ||
"Object", | ||
"Int", | ||
"IntPtr", | ||
"List") | ||
g.Expect(err).To(gomega.BeNil()) | ||
|
||
// Test anonymous NIL pointer. | ||
_, err = HasFields( | ||
&M{}, // PROBLEM HERE. | ||
"Name", | ||
"Age", | ||
"Name2", | ||
"Age2", | ||
"Ptr", | ||
"Object", | ||
"Int", | ||
"IntPtr", | ||
"List") | ||
g.Expect(err).ToNot(gomega.BeNil()) | ||
|
||
// Invalid field. | ||
_, err = HasFields( | ||
&M{B2: &B2{}}, | ||
"Name", | ||
"Age", | ||
"Name2", | ||
"Age2", | ||
"Ptr", | ||
"NOT-VALID", // PROBLEM HERE | ||
"Object", | ||
"Int", | ||
"IntPtr", | ||
"List") | ||
g.Expect(errors.Is(err, &FieldNotValid{})).To(gomega.BeTrue()) | ||
} |