Skip to content

Commit

Permalink
Merge pull request #4 from chenmingyong0423/feature/generic
Browse files Browse the repository at this point in the history
支持泛型参数
  • Loading branch information
chenmingyong0423 authored Feb 22, 2024
2 parents 4d27c11 + 5e0f259 commit aa14188
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions options/options_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,6 +28,9 @@ import (
"reflect"
"strconv"
"strings"

"github.com/chenmingyong0423/gkit/stringx"
"github.com/chenmingyong0423/go-optioner/templates"
)

type Generator struct {
Expand Down Expand Up @@ -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 ""
Expand Down
4 changes: 2 additions & 2 deletions templates/options_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}
}
Expand Down

0 comments on commit aa14188

Please sign in to comment.