Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
- 新增 mode 参数,指定文件的写入方式:write 和 append
- 根据 mode 参数,决定代码的生成位置(支持创建指定文件并写入和追加到指定文件)
  • Loading branch information
chenmingyong0423 committed Mar 12, 2024
1 parent 33a11e8 commit 44cee50
Show file tree
Hide file tree
Showing 11 changed files with 554 additions and 30 deletions.
45 changes: 37 additions & 8 deletions cmd/optioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,33 @@ import (
"os"
)

type ModeValue struct {
value string
validValues []string
}

func (m *ModeValue) String() string {
return m.value
}

func (m *ModeValue) Set(s string) error {
for _, v := range m.validValues {
if s == v {
m.value = s
return nil
}
}
return fmt.Errorf("invalid value %q for mode, valid values are: %v", s, m.validValues)
}

var (
structTypeNameArg = flag.String("type", "", "Struct type name of the functional options struct.")
outputArg = flag.String("output", "", "Output file name, default: srcDir/opt_<struct type>_gen.go")
g = options.NewGenerator()
outputMode = ModeValue{
value: "write",
validValues: []string{"write", "append"},
}
structTypeName = flag.String("type", "", "Struct type name of the functional options struct.")
output = flag.String("output", "", "Output file name, default: srcDir/opt_<struct type>_gen.go")
g = options.NewGenerator()
)

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

}

func main() {
flag.Var(&outputMode, "mode", "The file writing mode, default: write")
flag.Usage = usage
flag.Parse()
if len(*structTypeNameArg) == 0 {
if len(*structTypeName) == 0 {
flag.Usage()
os.Exit(1)
}
g.StructInfo.StructName = *structTypeNameArg
g.StructInfo.NewStructName = stringx.BigCamelToSmallCamel(*structTypeNameArg)
g.SetOutPath(outputArg)
g.StructInfo.StructName = *structTypeName
g.StructInfo.NewStructName = stringx.BigCamelToSmallCamel(*structTypeName)
g.SetOutPath(output)
g.SetMod(outputMode.value)

g.GeneratingOptions()
if !g.Found {
log.Printf("Target \"[%s]\" is not be found\n", g.StructInfo.StructName)
os.Exit(1)
}
fmt.Println(g.StructInfo.OptionalFields)
g.GenerateCodeByTemplate()
g.OutputToFile()
}
57 changes: 57 additions & 0 deletions example/additional_generic_example_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Generated by [optioner] command-line tool; DO NOT EDIT
// If you have any questions, please create issues and submit contributions at:
// https://github.com/chenmingyong0423/go-optioner

// Copyright 2024 chenmingyong0423

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package example

import "context"

type _ struct {
_ context.Context
}

type GenericExampleOption[T any, U comparable, V ~int] func(*GenericExample[T, U, V])

func NewGenericExample[T any, U comparable, V ~int](a T, opts ...GenericExampleOption[T, U, V]) *GenericExample[T, U, V] {
genericExample := &GenericExample[T, U, V]{
A: a,
}

for _, opt := range opts {
opt(genericExample)
}

return genericExample
}

func WithB[T any, U comparable, V ~int](b U) GenericExampleOption[T, U, V] {
return func(genericExample *GenericExample[T, U, V]) {
genericExample.B = b
}
}

func WithC[T any, U comparable, V ~int](c V) GenericExampleOption[T, U, V] {
return func(genericExample *GenericExample[T, U, V]) {
genericExample.C = c
}
}

func WithD[T any, U comparable, V ~int](d string) GenericExampleOption[T, U, V] {
return func(genericExample *GenericExample[T, U, V]) {
genericExample.D = d
}
}
166 changes: 166 additions & 0 deletions example/additional_user_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Generated by [optioner] command-line tool; DO NOT EDIT
// If you have any questions, please create issues and submit contributions at:
// https://github.com/chenmingyong0423/go-optioner

// Copyright 2024 chenmingyong0423

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package example

import (
"context"

"github.com/chenmingyong0423/go-optioner/example/third_party"
)

type _ struct {
_ context.Context
}

type UserOption func(*User)

func NewUser(embedded Embedded, embedded2 *Embedded2, e3 Embedded3, e4 *Embedded4, opts ...UserOption) *User {
user := &User{
Embedded: embedded,
Embedded2: embedded2,
E3: e3,
E4: e4,
}

for _, opt := range opts {
opt(user)
}

return user
}

func WithEmbedded5(embedded5 Embedded5) UserOption {
return func(user *User) {
user.Embedded5 = embedded5
}
}

func WithEmbedded6(embedded6 *Embedded6) UserOption {
return func(user *User) {
user.Embedded6 = embedded6
}
}

func WithE7(e7 Embedded7) UserOption {
return func(user *User) {
user.E7 = e7
}
}

func WithE8(e8 *Embedded8) UserOption {
return func(user *User) {
user.E8 = e8
}
}

func WithUsername(username string) UserOption {
return func(user *User) {
user.Username = username
}
}

func WithEmail(email string) UserOption {
return func(user *User) {
user.Email = email
}
}

func WithAddress(address Address) UserOption {
return func(user *User) {
user.Address = address
}
}

func WithArrayField(arrayField [4]int) UserOption {
return func(user *User) {
user.ArrayField = arrayField
}
}

func WithSliceField(sliceField []int) UserOption {
return func(user *User) {
user.SliceField = sliceField
}
}

func WithThirdPartyField(thirdPartyField third_party.ThirdParty) UserOption {
return func(user *User) {
user.ThirdPartyField = thirdPartyField
}
}

func WithMapField(mapField map[string]int) UserOption {
return func(user *User) {
user.MapField = mapField
}
}

func WithPtrField(ptrField *int) UserOption {
return func(user *User) {
user.PtrField = ptrField
}
}

func WithEmptyStructFiled(emptyStructFiled struct{}) UserOption {
return func(user *User) {
user.EmptyStructFiled = emptyStructFiled
}
}

func WithSimpleFuncField(simpleFuncField func()) UserOption {
return func(user *User) {
user.SimpleFuncField = simpleFuncField
}
}

func WithComplexFuncField(complexFuncField func(a int)) UserOption {
return func(user *User) {
user.ComplexFuncField = complexFuncField
}
}

func WithComplexFuncFieldV2(complexFuncFieldV2 func() int) UserOption {
return func(user *User) {
user.ComplexFuncFieldV2 = complexFuncFieldV2
}
}

func WithComplexFuncFieldV3(complexFuncFieldV3 func(a int) int) UserOption {
return func(user *User) {
user.ComplexFuncFieldV3 = complexFuncFieldV3
}
}

func WithComplexFuncFieldV4(complexFuncFieldV4 func(a int) (int, error)) UserOption {
return func(user *User) {
user.ComplexFuncFieldV4 = complexFuncFieldV4
}
}

func WithChanField(chanField chan int) UserOption {
return func(user *User) {
user.ChanField = chanField
}
}

func WithError(error error) UserOption {
return func(user *User) {
user.error = error
}
}
4 changes: 4 additions & 0 deletions example/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Embedded7 struct{}
type Embedded8 struct{}

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

//go:generate go run ../cmd/optioner/main.go -type User
//go:generate go run ../cmd/optioner/main.go -type User -output ./user_option.go
//go:generate go run ../cmd/optioner/main.go -type User -mode append -output ./additional_user_option.go
type User struct {
Embedded `opt:"-"`
*Embedded2 `opt:"-"`
Expand Down
37 changes: 37 additions & 0 deletions example/generic_example_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Generated by [optioner] command-line tool; DO NOT EDIT
// If you have any questions, please create issues and submit contributions at:
// https://github.com/chenmingyong0423/go-optioner

package example

type GenericExampleOption[T any, U comparable, V ~int] func(*GenericExample[T, U, V])

func NewGenericExample[T any, U comparable, V ~int](a T, opts ...GenericExampleOption[T, U, V]) *GenericExample[T, U, V] {
genericExample := &GenericExample[T, U, V]{
A: a,
}

for _, opt := range opts {
opt(genericExample)
}

return genericExample
}

func WithB[T any, U comparable, V ~int](b U) GenericExampleOption[T, U, V] {
return func(genericExample *GenericExample[T, U, V]) {
genericExample.B = b
}
}

func WithC[T any, U comparable, V ~int](c V) GenericExampleOption[T, U, V] {
return func(genericExample *GenericExample[T, U, V]) {
genericExample.C = c
}
}

func WithD[T any, U comparable, V ~int](d string) GenericExampleOption[T, U, V] {
return func(genericExample *GenericExample[T, U, V]) {
genericExample.D = d
}
}
2 changes: 1 addition & 1 deletion example/opt_generic_example_gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by optioner -type GenericExample; DO NOT EDIT
// Generated by [optioner] command-line tool; DO NOT EDIT
// If you have any questions, please create issues and submit contributions at:
// https://github.com/chenmingyong0423/go-optioner

Expand Down
2 changes: 1 addition & 1 deletion example/opt_user_gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by optioner -type User; DO NOT EDIT
// Generated by [optioner] command-line tool; DO NOT EDIT
// If you have any questions, please create issues and submit contributions at:
// https://github.com/chenmingyong0423/go-optioner

Expand Down
Loading

0 comments on commit 44cee50

Please sign in to comment.