Skip to content

Commit 68442bf

Browse files
临时保存
1 parent 59164c0 commit 68442bf

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

example/generic_example.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2024 chenmingyong0423
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package example
16+
17+
type Example[T any] struct {
18+
A T `opt:"-"`
19+
B T
20+
C int
21+
}

example/opt_example_gen.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Generated by optioner -type Example; DO NOT EDIT
2+
// If you have any questions, please create issues and submit contributions at:
3+
// https://github.com/chenmingyong0423/go-optioner
4+
5+
package example
6+
7+
type ExampleOption[T any] func(*Example[T])
8+
9+
func NewExample[T any](a T, opts ...ExampleOption[T]) *Example[T] {
10+
example := &Example[T]{
11+
A: a,
12+
}
13+
14+
for _, opt := range opts {
15+
opt(example)
16+
}
17+
18+
return example
19+
}
20+
21+
func WithB[T any](b T) ExampleOption {
22+
return func(example *Example) {
23+
example.B = b
24+
}
25+
}
26+
27+
func WithC[T any](c int) ExampleOption {
28+
return func(example *Example) {
29+
example.C = c
30+
}
31+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/chenmingyong0423/go-optioner
22

33
go 1.21.0
44

5-
require github.com/chenmingyong0423/gkit v0.0.4 // indirect
5+
require github.com/chenmingyong0423/gkit v0.5.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/chenmingyong0423/gkit v0.0.4 h1:LEToNpfzAN3aCUxCRltfO5j7jhtRntKt9LvwzhPhayc=
2-
github.com/chenmingyong0423/gkit v0.0.4/go.mod h1:vyxci/+5NnMMbp+DQPs94BUwawmbNJEK1o8ezlrS3eg=
1+
github.com/chenmingyong0423/gkit v0.5.0 h1:efthAjro7c5ZwHqMUV0+0CFZZFgsbthYnnUrfsSMdx4=
2+
github.com/chenmingyong0423/gkit v0.5.0/go.mod h1:9MKV0/B3MiBCGf4fssogftFbNIGJk9a4ffm/o3+pkHw=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

options/options_generator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type StructInfo struct {
6060
NewStructName string
6161
Fields []FieldInfo
6262
OptionalFields []FieldInfo
63+
GenericParams []FieldInfo
6364

6465
Imports []string
6566
}
@@ -101,6 +102,19 @@ func (g *Generator) parseStruct(fileName string) bool {
101102
}
102103
if structDecl, ok := typeSpec.Type.(*ast.StructType); ok {
103104
log.Printf("Generating Struct \"%s\" \n", g.StructInfo.StructName)
105+
if typeSpec.TypeParams != nil {
106+
log.Println("This is a struct which contains generic type:", typeSpec.Name)
107+
for _, param := range typeSpec.TypeParams.List {
108+
for _, name := range param.Names {
109+
typ := g.getTypeName(param.Type)
110+
g.StructInfo.GenericParams = append(g.StructInfo.GenericParams, FieldInfo{
111+
Name: name.Name,
112+
Type: typ,
113+
})
114+
log.Printf("Generic parameter: %s %s\n", name.Name, typ)
115+
}
116+
}
117+
}
104118
for _, field := range structDecl.Fields.List {
105119
fieldName := ""
106120
if len(field.Names) == 0 {

templates/options_templates.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import (
2929
)
3030
{{ end }}
3131
32-
type {{ .StructName }}Option func(*{{ .StructName }})
32+
type {{ .StructName }}Option{{if .GenericParams}}[{{range $index, $param := .GenericParams}}{{if $index}}, {{end}}{{$param.Name}} {{$param.Type}}{{end}}]{{end}} func(*{{ .StructName }}{{if .GenericParams}}[{{range $index, $param := .GenericParams}}{{if $index}}, {{end}}{{$param.Name}}{{end}}]{{end}})
3333
34-
func New{{ .StructName }}({{ range $index, $field := .Fields }}{{ $field.Name | bigCamelToSmallCamel }} {{ $field.Type }},{{ end }} opts ...{{ .StructName }}Option) *{{ .StructName }} {
35-
{{ .NewStructName }} := &{{ .StructName }}{
34+
func New{{ .StructName }}{{if .GenericParams}}[{{range $index, $param := .GenericParams}}{{if $index}}, {{end}}{{$param.Name}} {{$param.Type}}{{end}}]{{end}}({{ range $index, $field := .Fields }}{{ $field.Name | bigCamelToSmallCamel }} {{ $field.Type }},{{ end }} opts ...{{ .StructName }}Option{{if .GenericParams}}[{{range $index, $param := .GenericParams}}{{if $index}}, {{end}}{{$param.Name}}{{end}}]{{end}}) *{{ .StructName }}{{if .GenericParams}}[{{range $index, $param := .GenericParams}}{{if $index}}, {{end}}{{$param.Name}}{{end}}]{{end}} {
35+
{{ .NewStructName }} := &{{ .StructName }}{{if .GenericParams}}[{{range $index, $param := .GenericParams}}{{if $index}}, {{end}}{{$param.Name}}{{end}}]{{end}}{
3636
{{ range $index, $field := .Fields }}{{ $field.Name }}: {{ $field.Name | bigCamelToSmallCamel }},
3737
{{ end }}
3838
}
@@ -46,7 +46,7 @@ func New{{ .StructName }}({{ range $index, $field := .Fields }}{{ $field.Name |
4646
4747
{{ if .OptionalFields }}
4848
{{ range $field := .OptionalFields }}
49-
func With{{ $field.Name | capitalizeFirstLetter }}({{ $field.Name | bigCamelToSmallCamel }} {{ $field.Type }}) {{ $.StructName }}Option {
49+
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 {
5050
return func({{ $.NewStructName }} *{{ $.StructName }}) {
5151
{{ $.NewStructName }}.{{ $field.Name }} = {{ $field.Name | bigCamelToSmallCamel }}
5252
}

0 commit comments

Comments
 (0)