Merge pull request #2703 from rbwsam/ref/simplify_error

ref(pkg/chartutil): replace use of 'fmt.Efforf'
pull/2724/head
Steven E. Harris 7 years ago committed by GitHub
commit e8d80729ac

@ -17,6 +17,7 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -387,10 +388,16 @@ func istable(v interface{}) bool {
return ok return ok
} }
// PathValue takes a yaml path with . notation and returns the value if exists // PathValue takes a path that traverses a YAML structure and returns the value at the end of that path.
// The path starts at the root of the YAML structure and is comprised of YAML keys separated by periods.
// Given the following YAML data the value at path "chapter.one.title" is "Loomings".
//
// chapter:
// one:
// title: "Loomings"
func (v Values) PathValue(ypath string) (interface{}, error) { func (v Values) PathValue(ypath string) (interface{}, error) {
if len(ypath) == 0 { if len(ypath) == 0 {
return nil, fmt.Errorf("yaml path string cannot be zero length") return nil, errors.New("YAML path string cannot be zero length")
} }
yps := strings.Split(ypath, ".") yps := strings.Split(ypath, ".")
if len(yps) == 1 { if len(yps) == 1 {

@ -448,10 +448,12 @@ chapter:
if _, err := d.PathValue("chapter.doesntexist.one"); err == nil { if _, err := d.PathValue("chapter.doesntexist.one"); err == nil {
t.Errorf("Non-existent key in middle of path should return error: %s\n%v", err, d) t.Errorf("Non-existent key in middle of path should return error: %s\n%v", err, d)
} }
if _, err := d.PathValue(""); err == nil {
t.Error("Asking for the value from an empty path should yield an error")
}
if v, err := d.PathValue("title"); err == nil { if v, err := d.PathValue("title"); err == nil {
if v != "Moby Dick" { if v != "Moby Dick" {
t.Errorf("Failed to return values for root key title") t.Errorf("Failed to return values for root key title")
} }
} }
} }

Loading…
Cancel
Save