mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [structs] recognise directly embedded fields (#403)
This commit is contained in:
1 parent
3b94ec1932
commit
ef22b5b339
2 files changed
+24
-1
No files matched your search
@@ -0,0 +1,23 @@
|
|||||||
|
package structs
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestFieldIsEmbeddedUsesAnonymousFlag(t *testing.T) {
|
||||||
|
type Embedded struct{ Promoted int }
|
||||||
|
s := New(struct {
|
||||||
|
Embedded
|
||||||
|
Named Embedded
|
||||||
|
}{})
|
||||||
|
for _, tt := range []struct {
|
||||||
|
name string
|
||||||
|
want bool
|
||||||
|
}{{"Embedded", true}, {"Promoted", true}, {"Named", false}} {
|
||||||
|
field, ok := s.Field(tt.name)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("field %s missing", tt.name)
|
||||||
|
}
|
||||||
|
if got := field.IsEmbedded(); got != tt.want {
|
||||||
|
t.Errorf("%s: got %v, want %v", tt.name, got, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -40,7 +40,7 @@ func (f *Field) Value() any {
|
|||||||
// IsEmbedded returns true if the given field is an embedded field.
|
// IsEmbedded returns true if the given field is an embedded field.
|
||||||
// Play: https://go.dev/play/p/wV2PrbYm3Ec
|
// Play: https://go.dev/play/p/wV2PrbYm3Ec
|
||||||
func (f *Field) IsEmbedded() bool {
|
func (f *Field) IsEmbedded() bool {
|
||||||
return len(f.field.Index) > 1
|
return f.field.Anonymous || len(f.field.Index) > 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsExported returns true if the given field is exported.
|
// IsExported returns true if the given field is exported.
|
||||||
|
|||||||
Reference in new issue
Block a user