From d103ed6ca97ca5a2669f6cf5fe4b3d2a9c945f26 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Wed, 17 May 2017 16:51:25 -0400 Subject: Upgrading server dependancies (#6431) --- .../nicksnyder/go-i18n/i18n/bundle/bundle.go | 39 +++++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'vendor/github.com/nicksnyder/go-i18n/i18n/bundle') diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go index 2ad1d7cc3..a2291362f 100644 --- a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go +++ b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go @@ -9,6 +9,7 @@ import ( "path/filepath" "reflect" "sync" + "unicode" "github.com/nicksnyder/go-i18n/i18n/language" "github.com/nicksnyder/go-i18n/i18n/translation" @@ -86,8 +87,8 @@ func parseTranslations(filename string, buf []byte) ([]translation.Translation, ext := filepath.Ext(filename) - // `github.com/pelletier/go-toml` has an Unmarshal function, - // that can't unmarshal to maps, so we should parse TOML format separately. + // `github.com/pelletier/go-toml` lacks an Unmarshal function, + // so we should parse TOML separately. if ext == ".toml" { tree, err := toml.LoadReader(bytes.NewReader(buf)) if err != nil { @@ -119,11 +120,39 @@ func parseTranslations(filename string, buf []byte) ([]translation.Translation, } func isStandardFormat(ext string, buf []byte) bool { + buf = deleteLeadingComments(ext, buf) firstRune := rune(buf[0]) - if (ext == ".json" && firstRune == '[') || (ext == ".yaml" && firstRune == '-') { - return true + return (ext == ".json" && firstRune == '[') || (ext == ".yaml" && firstRune == '-') +} + +// deleteLeadingComments deletes leading newlines and comments in buf. +// It only works for ext == ".yaml". +func deleteLeadingComments(ext string, buf []byte) []byte { + if ext != ".yaml" { + return buf } - return false + + for { + buf = bytes.TrimLeftFunc(buf, unicode.IsSpace) + if buf[0] == '#' { + buf = deleteLine(buf) + } else { + break + } + } + + return buf +} + +func deleteLine(buf []byte) []byte { + index := bytes.IndexRune(buf, '\n') + if index == -1 { // If there is only one line without newline ... + return nil // ... delete it and return nothing. + } + if index == len(buf)-1 { // If there is only one line with newline ... + return nil // ... do the same as above. + } + return buf[index+1:] } // unmarshal finds an appropriate unmarshal function for ext -- cgit v1.2.3-1-g7c22