From a105e805814b57aa53b8f31c20b3f390d2d108ee Mon Sep 17 00:00:00 2001 From: "jack.jin" Date: Tue, 7 Feb 2023 16:23:45 +0900 Subject: [PATCH] make casting function private --- altsrc/map_input_source.go | 20 +++++++------- altsrc/yaml_file_loader_test.go | 46 ++++++++++++++++----------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/altsrc/map_input_source.go b/altsrc/map_input_source.go index 857e4febea..abd989f0d4 100644 --- a/altsrc/map_input_source.go +++ b/altsrc/map_input_source.go @@ -128,7 +128,7 @@ func (fsm *MapInputSource) Float64(name string) (float64, error) { return 0, nil } -func CastToInt64(v interface{}) (int64, bool) { +func castToInt64(v interface{}) (int64, bool) { int64Value := int64(0) isType := false if v == nil { @@ -152,7 +152,7 @@ func CastToInt64(v interface{}) (int64, bool) { func (fsm *MapInputSource) Int64(name string) (int64, error) { otherGenericValue, exists := fsm.valueMap[name] if exists { - otherValue, isType := CastToInt64(otherGenericValue) + otherValue, isType := castToInt64(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "int64", otherGenericValue) } @@ -160,7 +160,7 @@ func (fsm *MapInputSource) Int64(name string) (int64, error) { } nestedGenericValue, exists := nestedVal(name, fsm.valueMap) if exists { - otherValue, isType := CastToInt64(otherGenericValue) + otherValue, isType := castToInt64(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "int64", nestedGenericValue) } @@ -170,7 +170,7 @@ func (fsm *MapInputSource) Int64(name string) (int64, error) { return 0, nil } -func CastToUint(v interface{}) (uint, bool) { +func castToUint(v interface{}) (uint, bool) { uintValue := uint(0) isType := false if v == nil { @@ -209,7 +209,7 @@ func CastToUint(v interface{}) (uint, bool) { func (fsm *MapInputSource) Uint(name string) (uint, error) { otherGenericValue, exists := fsm.valueMap[name] if exists { - otherValue, isType := CastToUint(otherGenericValue) + otherValue, isType := castToUint(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint", otherGenericValue) } @@ -217,7 +217,7 @@ func (fsm *MapInputSource) Uint(name string) (uint, error) { } nestedGenericValue, exists := nestedVal(name, fsm.valueMap) if exists { - otherValue, isType := CastToUint(nestedGenericValue) + otherValue, isType := castToUint(nestedGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint", nestedGenericValue) } @@ -227,7 +227,7 @@ func (fsm *MapInputSource) Uint(name string) (uint, error) { return 0, nil } -func CastToUint64(v interface{}) (uint64, bool) { +func castToUint64(v interface{}) (uint64, bool) { uint64Value := uint64(0) isType := false if v == nil { @@ -263,7 +263,7 @@ func CastToUint64(v interface{}) (uint64, bool) { func (fsm *MapInputSource) Uint64(name string) (uint64, error) { otherGenericValue, exists := fsm.valueMap[name] if exists { - otherValue, isType := CastToUint64(otherGenericValue) + otherValue, isType := castToUint64(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint64", otherGenericValue) } @@ -271,7 +271,7 @@ func (fsm *MapInputSource) Uint64(name string) (uint64, error) { } nestedGenericValue, exists := nestedVal(name, fsm.valueMap) if exists { - otherValue, isType := CastToUint64(nestedGenericValue) + otherValue, isType := castToUint64(nestedGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint64", nestedGenericValue) } @@ -378,7 +378,7 @@ func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error) { var int64Slice = make([]int64, 0, len(otherValue)) for i, v := range otherValue { - int64Value, isType := CastToInt64(v) + int64Value, isType := castToInt64(v) if !isType { return nil, incorrectTypeForFlagError(fmt.Sprintf("%s[%d]", name, i), "int64", v) } diff --git a/altsrc/yaml_file_loader_test.go b/altsrc/yaml_file_loader_test.go index 23811795ea..dc8d521651 100644 --- a/altsrc/yaml_file_loader_test.go +++ b/altsrc/yaml_file_loader_test.go @@ -1,4 +1,4 @@ -package altsrc_test +package altsrc import ( "errors" @@ -11,7 +11,7 @@ import ( "time" "github.com/urfave/cli/v2" - "github.com/urfave/cli/v2/altsrc" + // "github.com/urfave/cli/v2/altsrc" ) func ExampleApp_Run_yamlFileLoaderDuration() { @@ -26,7 +26,7 @@ func ExampleApp_Run_yamlFileLoaderDuration() { return stat != nil } - // initConfigFileInputSource is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks + // initConfigFileInputSource is like InitInputSourceWithContext and NewYamlSourceFromFlagFunc, but checks // if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails. initConfigFileInputSource := func(configFlag string, flags []cli.Flag) cli.BeforeFunc { return func(context *cli.Context) error { @@ -36,11 +36,11 @@ func ExampleApp_Run_yamlFileLoaderDuration() { } else if !context.IsSet(configFlag) && !fileExists(configFile) { return nil } - inputSource, err := altsrc.NewYamlSourceFromFile(configFile) + inputSource, err := NewYamlSourceFromFile(configFile) if err != nil { return err } - return altsrc.ApplyInputSourceValues(context, inputSource, flags) + return ApplyInputSourceValues(context, inputSource, flags) } } @@ -53,7 +53,7 @@ func ExampleApp_Run_yamlFileLoaderDuration() { DefaultText: "../testdata/empty.yml", Usage: "config file", }, - altsrc.NewDurationFlag( + NewDurationFlag( &cli.DurationFlag{ Name: "keepalive-interval", Aliases: []string{"k"}, @@ -96,11 +96,11 @@ func TestYamlFileInt64Slice(t *testing.T) { defer os.Remove("current.yaml") testFlag := []cli.Flag{ - &altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, - &altsrc.Int64SliceFlag{Int64SliceFlag: &cli.Int64SliceFlag{Name: "top.test"}}, + &StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, + &Int64SliceFlag{Int64SliceFlag: &cli.Int64SliceFlag{Name: "top.test"}}, } app := &cli.App{} - app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf")) + app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf")) app.Action = func(c *cli.Context) error { return nil } app.Flags = append(app.Flags, testFlag...) @@ -116,11 +116,11 @@ func TestYamlFileStringSlice(t *testing.T) { defer os.Remove("current.yaml") testFlag := []cli.Flag{ - &altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, - &altsrc.StringSliceFlag{StringSliceFlag: &cli.StringSliceFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}}, + &StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, + &StringSliceFlag{StringSliceFlag: &cli.StringSliceFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}}, } app := &cli.App{} - app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf")) + app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf")) app.Action = func(c *cli.Context) error { if c.IsSet("top.test") { return nil @@ -185,12 +185,12 @@ func TestYamlFileUint64(t *testing.T) { defer os.Remove("current.yaml") testFlag := []cli.Flag{ - &altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, - &altsrc.Uint64Flag{Uint64Flag: &cli.Uint64Flag{Name: test.name}}, + &StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, + &Uint64Flag{Uint64Flag: &cli.Uint64Flag{Name: test.name}}, } app := &cli.App{} app.Flags = append(app.Flags, testFlag...) - app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf")) + app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf")) appCmd := []string{"testApp", "--conf", "current.yaml"} err := app.Run(appCmd) @@ -249,12 +249,12 @@ func TestYamlFileUint(t *testing.T) { defer os.Remove("current.yaml") testFlag := []cli.Flag{ - &altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, - &altsrc.UintFlag{UintFlag: &cli.UintFlag{Name: test.name}}, + &StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, + &UintFlag{UintFlag: &cli.UintFlag{Name: test.name}}, } app := &cli.App{} app.Flags = append(app.Flags, testFlag...) - app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf")) + app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf")) appCmd := []string{"testApp", "--conf", "current.yaml"} err := app.Run(appCmd) @@ -313,12 +313,12 @@ func TestYamlFileInt64(t *testing.T) { defer os.Remove("current.yaml") testFlag := []cli.Flag{ - &altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, - &altsrc.Int64Flag{Int64Flag: &cli.Int64Flag{Name: test.name}}, + &StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, + &Int64Flag{Int64Flag: &cli.Int64Flag{Name: test.name}}, } app := &cli.App{} app.Flags = append(app.Flags, testFlag...) - app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf")) + app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf")) appCmd := []string{"testApp", "--conf", "current.yaml"} err := app.Run(appCmd) @@ -338,7 +338,7 @@ func TestCastToUint64(t *testing.T) { } for _, test := range tests { - v, isType := altsrc.CastToUint64(test.value) + v, isType := castToUint64(test.value) if isType != test.expect && reflect.TypeOf(v).Kind() != reflect.Uint64 { t.Fatalf("expect %v, but %v", test.expect, isType) } @@ -354,7 +354,7 @@ func TestCastToUint(t *testing.T) { } for _, test := range tests { - v, isType := altsrc.CastToUint(test.value) + v, isType := castToUint(test.value) if isType != test.expect && reflect.TypeOf(v).Kind() != reflect.Uint64 { t.Fatalf("expect %v, but %v", test.expect, isType) }