From 5e0f259befb98d6a4848eb1ab487a1a0ed2e7d94 Mon Sep 17 00:00:00 2001 From: chenmingyong0423 <474571928@qq.com> Date: Thu, 22 Feb 2024 21:04:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=B3=9B=E5=9E=8B=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- options/options_generator.go | 7 +++++-- templates/options_templates.go | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) 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 }} } }