diff --git a/pkg/cmd/template/cmd_data_values_test.go b/pkg/cmd/template/cmd_data_values_test.go index 5d763815..9a5fd2bd 100644 --- a/pkg/cmd/template/cmd_data_values_test.go +++ b/pkg/cmd/template/cmd_data_values_test.go @@ -250,9 +250,9 @@ from_nested_lib: opts := cmdtpl.NewOptions() opts.DataValuesFlags = cmdtpl.DataValuesFlags{ - KVsFromYAML: []string{"@~inst1:val0=0", "@~inst1@nested-lib:val1=1"}, + KVsFromYAML: []string{"@~inst1:val0=0", "@~inst1@nested-lib:val1=1"}, FromFiles: []string{"c:\\User\\user\\dvs2.yml", "@~inst1:dvs3.yml", "@lib:D:\\User\\user\\dvs4.yml"}, - EnvFromStrings: []string{"@lib::DVS"}, + EnvFromStrings: []string{"@lib:DVS"}, EnvironFunc: func() []string { return []string{"DVS_val5=5"} }, KVsFromFiles: []string{"@lib:val6=c:\\User\\user\\dvs6.yml"}, ReadFileFunc: func(path string) ([]byte, error) { @@ -262,7 +262,7 @@ from_nested_lib: case "dvs3.yml": return dvs3, nil case "D:\\User\\user\\dvs4.yml": - return dvs4, nil + return dvs4, nil case "c:\\User\\user\\dvs6.yml": return dvs6, nil default: diff --git a/pkg/cmd/template/data_values_flags.go b/pkg/cmd/template/data_values_flags.go index 8f0b5061..d92afb2c 100644 --- a/pkg/cmd/template/data_values_flags.go +++ b/pkg/cmd/template/data_values_flags.go @@ -20,8 +20,9 @@ import ( ) const ( - dvsKVSep = "=" - dvsMapKeySep = "." + dvsKVSep = "=" + dvsMapKeySep = "." + libraryKeySep = ":" ) type DataValuesFlags struct { @@ -188,7 +189,7 @@ func (s *DataValuesFlags) env(prefix string, src dataValuesFlagsSource) ([]*data envVars = s.EnvironFunc() } - libRef, keyPrefix, err := s.libraryRefAndKey(prefix) + libRef, keyPrefix, err := s.libraryRefAndKeyStrict(prefix) if err != nil { return nil, err } @@ -235,7 +236,7 @@ func (s *DataValuesFlags) kv(kv string, src dataValuesFlagsSource) (*datavalues. return nil, fmt.Errorf("Deserializing value for key '%s': %s", pieces[0], err) } - libRef, key, err := s.libraryRefAndKey(pieces[0]) + libRef, key, err := s.libraryRefAndKeyStrict(pieces[0]) if err != nil { return nil, err } @@ -273,16 +274,23 @@ func (s *DataValuesFlags) kvFile(kv string) (*datavalues.Envelope, error) { return datavalues.NewEnvelopeWithLibRef(overlay, libRef) } +func (DataValuesFlags) libraryRefAndKeyStrict(key string) (string, string, error) { + libRef, key, err := DataValuesFlags{}.libraryRefAndKey(key) + if err != nil { + return "", "", err + } + if len(strings.Split(key, libraryKeySep)) > 1 { + // error on a common syntax mistake + return "", "", fmt.Errorf("Expected at most one library-key separator '%s' in '%s'", libraryKeySep, key) + } + return libRef, key, nil +} // libraryRefAndKey separates a library reference and a key. -// A library reference starts with ref.LibrarySep, and ends with the first occurrence of libraryPathSep. +// A library reference starts with ref.LibrarySep, and ends with the first occurrence of libraryKeySep. func (DataValuesFlags) libraryRefAndKey(key string) (string, string, error) { - const ( - libraryPathSep = ":" - ) - if strings.HasPrefix(key, ref.LibrarySep) { - keyPieces := strings.SplitN(key, libraryPathSep, 2) + keyPieces := strings.SplitN(key, libraryKeySep, 2) switch len(keyPieces) { case 1: return "", key, nil @@ -293,9 +301,7 @@ func (DataValuesFlags) libraryRefAndKey(key string) (string, string, error) { return keyPieces[0], keyPieces[1], nil } } - return "", key, nil - } func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, desc string, line string) *yamlmeta.Document {