Skip to content

Commit

Permalink
refactor: bytes testing & remove unused parameter & invalid recursive…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
hui.wang committed Mar 10, 2023
1 parent 12d0f61 commit 92c1ff2
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 54 deletions.
2 changes: 2 additions & 0 deletions tests/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ http_address: :3002
read_timeout: 100s
default_empty_map:
test1: 1
bytes: [1,2,3,4]
map1:
test1: 1000000
map_not_leaf:
Expand Down Expand Up @@ -77,6 +78,7 @@ func TestOverideDefaultValue(t *testing.T) {
So(cc.Int64Slice, ShouldResemble, []int64{101, 202, 303})
So(x.Parse(cc), ShouldBeNil)

So(cc.Bytes, ShouldResemble, []byte{1, 2, 3, 4})
So(cc.HttpAddress, ShouldEqual, ":3002")
// map1作为叶子节点存在
So(cc.Map1, ShouldResemble, map[string]int{"test1": 1000000})
Expand Down
2 changes: 2 additions & 0 deletions tests/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type RedisTimeout = redis.Timeout
var optionUsage = `在这里描述一些应用级别的配置规则`

// ConfigOptionDeclareWithDefault go-lint
//
//go:generate optiongen --option_with_struct_name=false --new_func=NewTestConfig --xconf=true --empty_composite_nil=true --usage_tag_name=usage
func ConfigOptionDeclareWithDefault() interface{} {
return map[string]interface{}{
Expand All @@ -47,6 +48,7 @@ func ConfigOptionDeclareWithDefault() interface{} {
"ProcessCount": int8(1),
"MaxUint64": uint64(0),
"MaxInt": int(0),
"Bytes": []byte(nil),
"Int8": int8(1),
"TimeDurations": []time.Duration([]time.Duration{time.Second, time.Second}), // @MethodComment(延迟队列)
"DefaultEmptyMap": map[string]int{},
Expand Down
13 changes: 13 additions & 0 deletions tests/gen_config_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 40 additions & 38 deletions tests/replit/config/gen_config_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions xcmd/gen_config_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion xcmd/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Executer 命令执行方法
type Executer = func(ctx context.Context, cmd *Command) error
type Executer func(ctx context.Context, cmd *Command) error

var usageExecuter = func(ctx context.Context, cmd *Command) error {
cmd.Usage()
Expand Down
12 changes: 4 additions & 8 deletions xconf_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,19 @@ func (x *XConf) usageToWriter(w io.Writer, args ...string) (err error) {

// usageLinesToWriter 打印usage信息到io.Writer
func (x *XConf) usageLinesToWriter(w io.Writer) error {
using := x.zeroValPtrForLayout
optionUsageStr := x.optionUsage
if using == nil {
return errors.New("usageToWriter: should parse first")
}
lines, magic, err := x.usageLines(using)
lines, magic, err := x.usageLines()
if err != nil {
return fmt.Errorf("Usage err: " + err.Error())
}
fmt.Fprintln(w, xutil.TableFormat(lines, magic, true, optionUsageStr))
return nil
}

func (x *XConf) usageLines(valPtr interface{}) ([]string, string, error) {
func (x *XConf) usageLines() ([]string, string, error) {
magic := "\x00"
var lineAll []string
lineAll = append(lineAll, "FLAG"+"\x00"+"ENV"+"\x00"+"TYPE"+"\x00"+"USAGE")
lineAll = append(lineAll, "FLAG"+magic+"ENV"+magic+"TYPE"+magic+"USAGE")
allFlag := xflag.GetFlagInfo(x.cc.FlagSet)
for _, v := range allFlag.List {
line := fmt.Sprintf("--%s", v.Name)
Expand Down Expand Up @@ -152,7 +148,7 @@ func (x *XConf) DumpInfo() {
}
lines = append(lines, fmt.Sprintf("# Hash Center : %s", hashCenter))

usageLines, magic, err := x.usageLines(x.zeroValPtrForLayout)
usageLines, magic, err := x.usageLines()
if err != nil {
x.cc.LogWarning("got error:" + err.Error())
return
Expand Down
11 changes: 7 additions & 4 deletions xflag/gen_options_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92c1ff2

Please sign in to comment.