Skip to content

Commit

Permalink
Merge pull request #5 from chenmingyong0423/feature/enhancement
Browse files Browse the repository at this point in the history
支持内嵌的匿名指针结构体属性
  • Loading branch information
chenmingyong0423 authored Feb 22, 2024
2 parents aa14188 + d8e6cee commit e895755
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions example/generic_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 31 additions & 2 deletions example/opt_user_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions example/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions options/options_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e895755

Please sign in to comment.