From d8e6ceeb594a41fbdeccf6c3e9cd4a334ad0c4f4 Mon Sep 17 00:00:00 2001 From: chenmingyong0423 <474571928@qq.com> Date: Thu, 22 Feb 2024 21:47:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=86=85=E5=B5=8C=E7=9A=84?= =?UTF-8?q?=E5=8C=BF=E5=90=8D=E6=8C=87=E9=92=88=E7=BB=93=E6=9E=84=E4=BD=93?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/generic_example.go | 1 + example/opt_user_gen.go | 33 +++++++++++++++++++++++++++++++-- example/user.go | 25 +++++++++++++++++++++++++ options/options_generator.go | 6 ++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/example/generic_example.go b/example/generic_example.go index 3e9bcc1..310a409 100644 --- a/example/generic_example.go +++ b/example/generic_example.go @@ -14,6 +14,7 @@ package example +//go:generate go run ../cmd/optioner/main.go -type GenericExample type GenericExample[T any, U comparable, V ~int] struct { A T `opt:"-"` B U diff --git a/example/opt_user_gen.go b/example/opt_user_gen.go index 2e23ac2..3fd2144 100644 --- a/example/opt_user_gen.go +++ b/example/opt_user_gen.go @@ -10,8 +10,13 @@ import ( type UserOption func(*User) -func NewUser(opts ...UserOption) *User { - user := &User{} +func NewUser(embedded Embedded, embedded2 *Embedded2, e3 Embedded3, e4 *Embedded4, opts ...UserOption) *User { + user := &User{ + Embedded: embedded, + Embedded2: embedded2, + E3: e3, + E4: e4, + } for _, opt := range opts { opt(user) @@ -20,6 +25,30 @@ func NewUser(opts ...UserOption) *User { return user } +func WithEmbedded5(embedded5 Embedded5) UserOption { + return func(user *User) { + user.Embedded5 = embedded5 + } +} + +func WithEmbedded6(embedded6 *Embedded6) UserOption { + return func(user *User) { + user.Embedded6 = embedded6 + } +} + +func WithE7(e7 Embedded7) UserOption { + return func(user *User) { + user.E7 = e7 + } +} + +func WithE8(e8 *Embedded8) UserOption { + return func(user *User) { + user.E8 = e8 + } +} + func WithUsername(username string) UserOption { return func(user *User) { user.Username = username diff --git a/example/user.go b/example/user.go index eb389f7..5cc794f 100644 --- a/example/user.go +++ b/example/user.go @@ -18,7 +18,32 @@ import ( "github.com/chenmingyong0423/go-optioner/example/third_party" ) +type Embedded struct{} + +type Embedded2 struct{} + +type Embedded3 struct{} + +type Embedded4 struct{} + +type Embedded5 struct{} + +type Embedded6 struct{} + +type Embedded7 struct{} + +type Embedded8 struct{} + +//go:generate go run ../cmd/optioner/main.go -type User type User struct { + Embedded `opt:"-"` + *Embedded2 `opt:"-"` + E3 Embedded3 `opt:"-"` + E4 *Embedded4 `opt:"-"` + Embedded5 + *Embedded6 + E7 Embedded7 + E8 *Embedded8 Username string Email string Address // combined struct diff --git a/options/options_generator.go b/options/options_generator.go index e8c306c..11ee864 100644 --- a/options/options_generator.go +++ b/options/options_generator.go @@ -121,6 +121,12 @@ func (g *Generator) parseStruct(fileName string) bool { if len(field.Names) == 0 { if ident, ok := field.Type.(*ast.Ident); ok { // combined struct fieldName = ident.Name + } else if starExpr, ok := field.Type.(*ast.StarExpr); ok { + if ident2, ok := starExpr.X.(*ast.Ident); ok { // combined struct + fieldName = ident2.Name + } else { + continue + } } else { continue }