Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2_bug_fixing_1121 : Now StringSliceFlag, Value(default) value set in Destination #1123

Closed
wants to merge 2 commits into from
Closed

Conversation

atif-konasl
Copy link

@atif-konasl atif-konasl commented Apr 30, 2020

Motivation

Value(default) does not set in Destination field in StringSliceFlag.

Findings

  • No slice of variables like []string, []int, []float can be set in flag.go in flag package in golang.
  • No set.StringVar(f.Destination, name, f.Value, f.Usage) this type of method for slice of string in flag.go in flag package.

Changes

When Destination field set as a StringSlice in StringSliceFlag then we need to set Value(StringSlice) in Destination as default. But flag.go in flag package does not support this.
For that reason we need to set Value in Destination before call set.Var(f.Destination, name, f.Usage)

Before Change :

// Apply populates the flag given the flag set and environment
func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
	if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
		f.Value = &StringSlice{}
		destination := f.Value
		if f.Destination != nil {
			destination = f.Destination
		}

		for _, s := range strings.Split(val, ",") {
			if err := destination.Set(strings.TrimSpace(s)); err != nil {
				return fmt.Errorf("could not parse %q as string value for flag %s: %s", val, f.Name, err)
			}
		}

		// Set this to false so that we reset the slice if we then set values from
		// flags that have already been set by the environment.
		destination.hasBeenSet = false
		f.HasBeenSet = true
	}

	for _, name := range f.Names() {
		if f.Value == nil {
			f.Value = &StringSlice{}
		}

		if f.Destination != nil {
			set.Var(f.Destination, name, f.Usage)
			continue
		}

		set.Var(f.Value, name, f.Usage)
	}
	return nil
}

After Change :

// Apply populates the flag given the flag set and environment
func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
	if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
		f.Value = &StringSlice{}
		destination := f.Value
		if f.Destination != nil {
			destination = f.Destination
		}

		for _, s := range strings.Split(val, ",") {
			if err := destination.Set(strings.TrimSpace(s)); err != nil {
				return fmt.Errorf("could not parse %q as string value for flag %s: %s", val, f.Name, err)
			}
		}

		// Set this to false so that we reset the slice if we then set values from
		// flags that have already been set by the environment.
		destination.hasBeenSet = false
		f.HasBeenSet = true
	}

	for _, name := range f.Names() {
		if f.Value == nil {
			f.Value = &StringSlice{}
		}

		if f.Destination != nil {
			f.Destination.slice = make([]string, len(f.Value.slice))
			copy(f.Destination.slice, f.Value.slice)
			set.Var(f.Destination, name, f.Usage)
			continue
		}

		set.Var(f.Value, name, f.Usage)
	}

	return nil
}

Run go env

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/atif/.cache/go-build"
GOENV="/home/atif/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/atif/WorkStation/Lukso/CodeBase"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build805853841=/tmp/go-build -gno-record-gcc-switches"

@atif-konasl atif-konasl requested a review from a team as a code owner April 30, 2020 12:21
@atif-konasl atif-konasl requested review from saschagrunert and rliebz and removed request for a team April 30, 2020 12:21
saschagrunert
saschagrunert previously approved these changes Apr 30, 2020
Copy link
Member

@saschagrunert saschagrunert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@saschagrunert
Copy link
Member

Can we add a test for that?

@atif-konasl
Copy link
Author

atif-konasl commented Apr 30, 2020

Yes I will add a test.

@atif-konasl
Copy link
Author

@saschagrunert and @rliebz , i added a test in app_test.go. Pls check.

Copy link
Member

@saschagrunert saschagrunert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, @urfave/cli I think the CI is broken here but I'm not sure if it's related to this change 🤔

@coilysiren
Copy link
Member

echoing my comment on #1127, this PR looks like it solves the same issue as that one.

@sgoroshko & @atif-konasl can you both have a conversation about whose solution you think should move forward? We can't merge both

Copy link
Member

@coilysiren coilysiren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to re-request me when the duplication between #1127 and #1123 is resolved!

@coilysiren
Copy link
Member

With respect to CI failing, @atif-konasl try removing the special characters from your branch name. That will require you to create a new PR, most likely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants