Some improvements to compose volume spec parsing

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-06-03 22:59:09 -04:00
parent 9f00ec085f
commit db6ff357a7
2 changed files with 13 additions and 12 deletions

View File

@ -10,6 +10,8 @@ import (
"github.com/pkg/errors"
)
const endOfSpec = rune(0)
func parseVolume(spec string) (types.ServiceVolumeConfig, error) {
volume := types.ServiceVolumeConfig{}
@ -23,11 +25,11 @@ func parseVolume(spec string) (types.ServiceVolumeConfig, error) {
}
buffer := []rune{}
for _, char := range spec {
for _, char := range spec + string(endOfSpec) {
switch {
case isWindowsDrive(char, buffer, volume):
case isWindowsDrive(buffer, char):
buffer = append(buffer, char)
case char == ':':
case char == ':' || char == endOfSpec:
if err := populateFieldFromBuffer(char, buffer, &volume); err != nil {
return volume, errors.Wrapf(err, "invalid spec: %s", spec)
}
@ -36,15 +38,11 @@ func parseVolume(spec string) (types.ServiceVolumeConfig, error) {
buffer = append(buffer, char)
}
}
if err := populateFieldFromBuffer(rune(0), buffer, &volume); err != nil {
return volume, errors.Wrapf(err, "invalid spec: %s", spec)
}
populateType(&volume)
return volume, nil
}
func isWindowsDrive(char rune, buffer []rune, volume types.ServiceVolumeConfig) bool {
func isWindowsDrive(buffer []rune, char rune) bool {
return char == ':' && len(buffer) == 1 && unicode.IsLetter(buffer[0])
}
@ -54,7 +52,7 @@ func populateFieldFromBuffer(char rune, buffer []rune, volume *types.ServiceVolu
case len(buffer) == 0:
return errors.New("empty section between colons")
// Anonymous volume
case volume.Source == "" && char == rune(0):
case volume.Source == "" && char == endOfSpec:
volume.Target = strBuffer
return nil
case volume.Source == "":
@ -112,7 +110,6 @@ func isFilePath(source string) bool {
return true
}
// Windows absolute path
first, next := utf8.DecodeRuneInString(source)
return unicode.IsLetter(first) && source[next] == ':'
first, nextIndex := utf8.DecodeRuneInString(source)
return isWindowsDrive([]rune{first}, rune(source[nextIndex]))
}

View File

@ -147,3 +147,7 @@ func TestParseVolumeWithRW(t *testing.T) {
assert.Equal(t, expected, volume)
}
}
func TestIsFilePath(t *testing.T) {
assert.False(t, isFilePath("a界"))
}