diff --git a/options/options_generator.go b/options/options_generator.go index a9f78d8..e8c306c 100644 --- a/options/options_generator.go +++ b/options/options_generator.go @@ -17,8 +17,6 @@ package options import ( "bytes" "fmt" - "github.com/chenmingyong0423/gkit/stringx" - "github.com/chenmingyong0423/go-optioner/templates" "go/ast" "go/build" "go/format" @@ -30,6 +28,9 @@ import ( "reflect" "strconv" "strings" + + "github.com/chenmingyong0423/gkit/stringx" + "github.com/chenmingyong0423/go-optioner/templates" ) type Generator struct { @@ -226,6 +227,8 @@ func (g *Generator) getTypeName(expr ast.Expr) string { return g.parseFuncType(t) case *ast.ChanType: return "chan " + g.getTypeName(t.Value) + case *ast.UnaryExpr: + return "~" + g.getTypeName(t.X) default: log.Fatalf("Unsupported type for field: %T", t) return "" diff --git a/templates/options_templates.go b/templates/options_templates.go index 6dfe938..49532a2 100644 --- a/templates/options_templates.go +++ b/templates/options_templates.go @@ -46,8 +46,8 @@ func New{{ .StructName }}{{if .GenericParams}}[{{range $index, $param := .Generi {{ if .OptionalFields }} {{ range $field := .OptionalFields }} -func With{{ $field.Name | capitalizeFirstLetter }}{{if $.GenericParams}}[{{range $index, $param := $.GenericParams}}{{if $index}}, {{end}}{{$param.Name}} {{$param.Type}}{{end}}]{{end}}({{ $field.Name | bigCamelToSmallCamel }} {{ $field.Type }}) {{ $.StructName }}Option { - return func({{ $.NewStructName }} *{{ $.StructName }}) { +func With{{ $field.Name | capitalizeFirstLetter }}{{if $.GenericParams}}[{{range $index, $param := $.GenericParams}}{{if $index}}, {{end}}{{$param.Name}} {{$param.Type}}{{end}}]{{end}}({{ $field.Name | bigCamelToSmallCamel }} {{ $field.Type }}) {{ $.StructName }}Option{{if $.GenericParams}}[{{range $index, $param := $.GenericParams}}{{if $index}}, {{end}}{{$param.Name}}{{end}}]{{end}} { + return func({{ $.NewStructName }} *{{ $.StructName }}{{if $.GenericParams}}[{{range $index, $param := $.GenericParams}}{{if $index}}, {{end}}{{$param.Name}}{{end}}]{{end}}) { {{ $.NewStructName }}.{{ $field.Name }} = {{ $field.Name | bigCamelToSmallCamel }} } }