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

Setting value to empty string #11

Closed
kenjones-cisco opened this issue Jun 5, 2017 · 3 comments
Closed

Setting value to empty string #11

kenjones-cisco opened this issue Jun 5, 2017 · 3 comments

Comments

@kenjones-cisco
Copy link
Contributor

Version: 1.10

File: (project.yml)

metadata:
  name: example
  version: 0.4.0
  pretag: dev

Command:

    yaml write -i project.yml metadata.pretag ''

Response:

panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x5e3500, 0xc4200100c0)
        /usr/local/Cellar/go/1.7.5/libexec/src/runtime/panic.go:500 +0x1a1
main.parseValue(0x7ffd1417ced1, 0x0, 0x1, 0xc4200ae880)
        /Users/mikefarah/dev/go/src/github.com/mikefarah/yaml/yaml.go:199 +0x83e
main.updateYaml(0xc4200bc2d0, 0x3, 0x5, 0x0, 0xc4200e3c77)
        /Users/mikefarah/dev/go/src/github.com/mikefarah/yaml/yaml.go:188 +0x2e1
main.writeProperty(0xc4201481e0, 0xc4200bc2d0, 0x3, 0x5)
        /Users/mikefarah/dev/go/src/github.com/mikefarah/yaml/yaml.go:162 +0x58
github.com/mikefarah/yaml/Godeps/_workspace/src/github.com/spf13/cobra.(*Command).execute(0xc4201481e0, 0xc4200bc280, 0x5, 0x5, 0xc4201481e0, 0xc4200bc280)
        /Users/mikefarah/dev/go/src/github.com/mikefarah/yaml/Godeps/_workspace/src/github.com/spf13/cobra/command.go:547 +0x411
github.com/mikefarah/yaml/Godeps/_workspace/src/github.com/spf13/cobra.(*Command).Execute(0xc4201485a0, 0xc4200e3f10, 0x3)
        /Users/mikefarah/dev/go/src/github.com/mikefarah/yaml/Godeps/_workspace/src/github.com/spf13/cobra/command.go:630 +0x398
main.main()
        /Users/mikefarah/dev/go/src/github.com/mikefarah/yaml/yaml.go:41 +0x449

Seems like setting a value to empty should be valid, but instead it will panic.

@kenjones-cisco
Copy link
Contributor Author

After reading through the code:

func parseValue(argument string) interface{} {
	var value, err interface{}
	var inQuotes = argument[0] == '"'
	if !inQuotes {
		value, err = strconv.ParseFloat(argument, 64)
		if err == nil {
			return value
		}
		value, err = strconv.ParseBool(argument)
		if err == nil {
			return value
		}
		return argument
	}
	return argument[1 : len(argument)-1]
}

The unquoted empty string was passed in, as a result that is not understood. By explicitly passing in the quoted empty string, the result seems to work.

The workaround seems to be doing the following:

    yaml write -i project.yml metadata.pretag '""'

The result then look like this:

metadata:
  name: example
  version: 0.4.0
  pretag: ""

The desired state would result in the following:

metadata:
  name: example
  version: 0.4.0
  pretag:

@mikefarah
Copy link
Owner

Ah ok cool :)

I've got a fix I should have out soon, need to check the len of arguments in the 'inQuotes' declaration

@mikefarah
Copy link
Owner

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

No branches or pull requests

2 participants