Skip to content

Commit 44cee50

Browse files
feat:
- 新增 mode 参数,指定文件的写入方式:write 和 append - 根据 mode 参数,决定代码的生成位置(支持创建指定文件并写入和追加到指定文件)
1 parent 33a11e8 commit 44cee50

File tree

11 files changed

+554
-30
lines changed

11 files changed

+554
-30
lines changed

cmd/optioner/main.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,33 @@ import (
2323
"os"
2424
)
2525

26+
type ModeValue struct {
27+
value string
28+
validValues []string
29+
}
30+
31+
func (m *ModeValue) String() string {
32+
return m.value
33+
}
34+
35+
func (m *ModeValue) Set(s string) error {
36+
for _, v := range m.validValues {
37+
if s == v {
38+
m.value = s
39+
return nil
40+
}
41+
}
42+
return fmt.Errorf("invalid value %q for mode, valid values are: %v", s, m.validValues)
43+
}
44+
2645
var (
27-
structTypeNameArg = flag.String("type", "", "Struct type name of the functional options struct.")
28-
outputArg = flag.String("output", "", "Output file name, default: srcDir/opt_<struct type>_gen.go")
29-
g = options.NewGenerator()
46+
outputMode = ModeValue{
47+
value: "write",
48+
validValues: []string{"write", "append"},
49+
}
50+
structTypeName = flag.String("type", "", "Struct type name of the functional options struct.")
51+
output = flag.String("output", "", "Output file name, default: srcDir/opt_<struct type>_gen.go")
52+
g = options.NewGenerator()
3053
)
3154

3255
func usage() {
@@ -36,25 +59,31 @@ func usage() {
3659
fmt.Fprintf(os.Stderr, "Flags:\n")
3760
fmt.Fprintf(os.Stderr, "\t -type <struct name>\n")
3861
fmt.Fprintf(os.Stderr, "\t -output <output path>, default: srcDir/opt_xxx_gen.go\n")
62+
fmt.Fprintf(os.Stderr, "\t -mode <the file writing mode>, default: write\n")
63+
fmt.Fprintf(os.Stderr, "\t there are two available modes:\n")
64+
fmt.Fprintf(os.Stderr, "\t\t - write(Write/Overwrite): Overwrites or creates a new file.\n")
65+
fmt.Fprintf(os.Stderr, "\t\t - append (Append): Adds to the end of the file.\n")
66+
3967
}
4068

4169
func main() {
70+
flag.Var(&outputMode, "mode", "The file writing mode, default: write")
4271
flag.Usage = usage
4372
flag.Parse()
44-
if len(*structTypeNameArg) == 0 {
73+
if len(*structTypeName) == 0 {
4574
flag.Usage()
4675
os.Exit(1)
4776
}
48-
g.StructInfo.StructName = *structTypeNameArg
49-
g.StructInfo.NewStructName = stringx.BigCamelToSmallCamel(*structTypeNameArg)
50-
g.SetOutPath(outputArg)
77+
g.StructInfo.StructName = *structTypeName
78+
g.StructInfo.NewStructName = stringx.BigCamelToSmallCamel(*structTypeName)
79+
g.SetOutPath(output)
80+
g.SetMod(outputMode.value)
5181

5282
g.GeneratingOptions()
5383
if !g.Found {
5484
log.Printf("Target \"[%s]\" is not be found\n", g.StructInfo.StructName)
5585
os.Exit(1)
5686
}
57-
fmt.Println(g.StructInfo.OptionalFields)
5887
g.GenerateCodeByTemplate()
5988
g.OutputToFile()
6089
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Generated by [optioner] command-line tool; 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+
// Copyright 2024 chenmingyong0423
6+
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
19+
package example
20+
21+
import "context"
22+
23+
type _ struct {
24+
_ context.Context
25+
}
26+
27+
type GenericExampleOption[T any, U comparable, V ~int] func(*GenericExample[T, U, V])
28+
29+
func NewGenericExample[T any, U comparable, V ~int](a T, opts ...GenericExampleOption[T, U, V]) *GenericExample[T, U, V] {
30+
genericExample := &GenericExample[T, U, V]{
31+
A: a,
32+
}
33+
34+
for _, opt := range opts {
35+
opt(genericExample)
36+
}
37+
38+
return genericExample
39+
}
40+
41+
func WithB[T any, U comparable, V ~int](b U) GenericExampleOption[T, U, V] {
42+
return func(genericExample *GenericExample[T, U, V]) {
43+
genericExample.B = b
44+
}
45+
}
46+
47+
func WithC[T any, U comparable, V ~int](c V) GenericExampleOption[T, U, V] {
48+
return func(genericExample *GenericExample[T, U, V]) {
49+
genericExample.C = c
50+
}
51+
}
52+
53+
func WithD[T any, U comparable, V ~int](d string) GenericExampleOption[T, U, V] {
54+
return func(genericExample *GenericExample[T, U, V]) {
55+
genericExample.D = d
56+
}
57+
}

example/additional_user_option.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Generated by [optioner] command-line tool; 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+
// Copyright 2024 chenmingyong0423
6+
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
19+
package example
20+
21+
import (
22+
"context"
23+
24+
"github.com/chenmingyong0423/go-optioner/example/third_party"
25+
)
26+
27+
type _ struct {
28+
_ context.Context
29+
}
30+
31+
type UserOption func(*User)
32+
33+
func NewUser(embedded Embedded, embedded2 *Embedded2, e3 Embedded3, e4 *Embedded4, opts ...UserOption) *User {
34+
user := &User{
35+
Embedded: embedded,
36+
Embedded2: embedded2,
37+
E3: e3,
38+
E4: e4,
39+
}
40+
41+
for _, opt := range opts {
42+
opt(user)
43+
}
44+
45+
return user
46+
}
47+
48+
func WithEmbedded5(embedded5 Embedded5) UserOption {
49+
return func(user *User) {
50+
user.Embedded5 = embedded5
51+
}
52+
}
53+
54+
func WithEmbedded6(embedded6 *Embedded6) UserOption {
55+
return func(user *User) {
56+
user.Embedded6 = embedded6
57+
}
58+
}
59+
60+
func WithE7(e7 Embedded7) UserOption {
61+
return func(user *User) {
62+
user.E7 = e7
63+
}
64+
}
65+
66+
func WithE8(e8 *Embedded8) UserOption {
67+
return func(user *User) {
68+
user.E8 = e8
69+
}
70+
}
71+
72+
func WithUsername(username string) UserOption {
73+
return func(user *User) {
74+
user.Username = username
75+
}
76+
}
77+
78+
func WithEmail(email string) UserOption {
79+
return func(user *User) {
80+
user.Email = email
81+
}
82+
}
83+
84+
func WithAddress(address Address) UserOption {
85+
return func(user *User) {
86+
user.Address = address
87+
}
88+
}
89+
90+
func WithArrayField(arrayField [4]int) UserOption {
91+
return func(user *User) {
92+
user.ArrayField = arrayField
93+
}
94+
}
95+
96+
func WithSliceField(sliceField []int) UserOption {
97+
return func(user *User) {
98+
user.SliceField = sliceField
99+
}
100+
}
101+
102+
func WithThirdPartyField(thirdPartyField third_party.ThirdParty) UserOption {
103+
return func(user *User) {
104+
user.ThirdPartyField = thirdPartyField
105+
}
106+
}
107+
108+
func WithMapField(mapField map[string]int) UserOption {
109+
return func(user *User) {
110+
user.MapField = mapField
111+
}
112+
}
113+
114+
func WithPtrField(ptrField *int) UserOption {
115+
return func(user *User) {
116+
user.PtrField = ptrField
117+
}
118+
}
119+
120+
func WithEmptyStructFiled(emptyStructFiled struct{}) UserOption {
121+
return func(user *User) {
122+
user.EmptyStructFiled = emptyStructFiled
123+
}
124+
}
125+
126+
func WithSimpleFuncField(simpleFuncField func()) UserOption {
127+
return func(user *User) {
128+
user.SimpleFuncField = simpleFuncField
129+
}
130+
}
131+
132+
func WithComplexFuncField(complexFuncField func(a int)) UserOption {
133+
return func(user *User) {
134+
user.ComplexFuncField = complexFuncField
135+
}
136+
}
137+
138+
func WithComplexFuncFieldV2(complexFuncFieldV2 func() int) UserOption {
139+
return func(user *User) {
140+
user.ComplexFuncFieldV2 = complexFuncFieldV2
141+
}
142+
}
143+
144+
func WithComplexFuncFieldV3(complexFuncFieldV3 func(a int) int) UserOption {
145+
return func(user *User) {
146+
user.ComplexFuncFieldV3 = complexFuncFieldV3
147+
}
148+
}
149+
150+
func WithComplexFuncFieldV4(complexFuncFieldV4 func(a int) (int, error)) UserOption {
151+
return func(user *User) {
152+
user.ComplexFuncFieldV4 = complexFuncFieldV4
153+
}
154+
}
155+
156+
func WithChanField(chanField chan int) UserOption {
157+
return func(user *User) {
158+
user.ChanField = chanField
159+
}
160+
}
161+
162+
func WithError(error error) UserOption {
163+
return func(user *User) {
164+
user.error = error
165+
}
166+
}

example/examples.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Embedded7 struct{}
3939
type Embedded8 struct{}
4040

4141
//go:generate go run ../cmd/optioner/main.go -type GenericExample
42+
//go:generate go run ../cmd/optioner/main.go -type GenericExample -output ./generic_example_option.go
43+
//go:generate go run ../cmd/optioner/main.go -type GenericExample -mode append -output ./additional_generic_example_option.go
4244
type GenericExample[T any, U comparable, V ~int] struct {
4345
A T `opt:"-"`
4446
B U
@@ -47,6 +49,8 @@ type GenericExample[T any, U comparable, V ~int] struct {
4749
}
4850

4951
//go:generate go run ../cmd/optioner/main.go -type User
52+
//go:generate go run ../cmd/optioner/main.go -type User -output ./user_option.go
53+
//go:generate go run ../cmd/optioner/main.go -type User -mode append -output ./additional_user_option.go
5054
type User struct {
5155
Embedded `opt:"-"`
5256
*Embedded2 `opt:"-"`

example/generic_example_option.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Generated by [optioner] command-line tool; 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 GenericExampleOption[T any, U comparable, V ~int] func(*GenericExample[T, U, V])
8+
9+
func NewGenericExample[T any, U comparable, V ~int](a T, opts ...GenericExampleOption[T, U, V]) *GenericExample[T, U, V] {
10+
genericExample := &GenericExample[T, U, V]{
11+
A: a,
12+
}
13+
14+
for _, opt := range opts {
15+
opt(genericExample)
16+
}
17+
18+
return genericExample
19+
}
20+
21+
func WithB[T any, U comparable, V ~int](b U) GenericExampleOption[T, U, V] {
22+
return func(genericExample *GenericExample[T, U, V]) {
23+
genericExample.B = b
24+
}
25+
}
26+
27+
func WithC[T any, U comparable, V ~int](c V) GenericExampleOption[T, U, V] {
28+
return func(genericExample *GenericExample[T, U, V]) {
29+
genericExample.C = c
30+
}
31+
}
32+
33+
func WithD[T any, U comparable, V ~int](d string) GenericExampleOption[T, U, V] {
34+
return func(genericExample *GenericExample[T, U, V]) {
35+
genericExample.D = d
36+
}
37+
}

example/opt_generic_example_gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by optioner -type GenericExample; DO NOT EDIT
1+
// Generated by [optioner] command-line tool; DO NOT EDIT
22
// If you have any questions, please create issues and submit contributions at:
33
// https://github.com/chenmingyong0423/go-optioner
44

example/opt_user_gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by optioner -type User; DO NOT EDIT
1+
// Generated by [optioner] command-line tool; DO NOT EDIT
22
// If you have any questions, please create issues and submit contributions at:
33
// https://github.com/chenmingyong0423/go-optioner
44

0 commit comments

Comments
 (0)