Skip to content

Commit

Permalink
make casting function private
Browse files Browse the repository at this point in the history
  • Loading branch information
jack.jin committed Feb 7, 2023
1 parent bebf9b7 commit a105e80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
20 changes: 10 additions & 10 deletions altsrc/map_input_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -152,15 +152,15 @@ 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)
}
return otherValue, nil
}
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
if exists {
otherValue, isType := CastToInt64(otherGenericValue)
otherValue, isType := castToInt64(otherGenericValue)
if !isType {
return 0, incorrectTypeForFlagError(name, "int64", nestedGenericValue)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -209,15 +209,15 @@ 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)
}
return otherValue, nil
}
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
if exists {
otherValue, isType := CastToUint(nestedGenericValue)
otherValue, isType := castToUint(nestedGenericValue)
if !isType {
return 0, incorrectTypeForFlagError(name, "uint", nestedGenericValue)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -263,15 +263,15 @@ 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)
}
return otherValue, nil
}
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
if exists {
otherValue, isType := CastToUint64(nestedGenericValue)
otherValue, isType := castToUint64(nestedGenericValue)
if !isType {
return 0, incorrectTypeForFlagError(name, "uint64", nestedGenericValue)
}
Expand Down Expand Up @@ -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)
}
Expand Down
46 changes: 23 additions & 23 deletions altsrc/yaml_file_loader_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package altsrc_test
package altsrc

import (
"errors"
Expand All @@ -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() {
Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand All @@ -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"},
Expand Down Expand Up @@ -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...)

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit a105e80

Please sign in to comment.