Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit 4122f16

Browse files
authored
Merge pull request #11 from ulyssessouza/unquoted-values
2 parents 170ae7d + 655269e commit 4122f16

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

fixtures/unquoted.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OPTION_A = "some quoted phrase"
2+
OPTION_B=first one with an unquoted phrase
3+
OPTION_C = then another one with an unquoted phrase
4+
OPTION_D = then another one with an unquoted phrase special è char
5+
OPTION_E = "then another one quoted phrase"

godotenv_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ and so on`,
199199
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
200200
}
201201

202+
func TestLoadUnquotedEnv(t *testing.T) {
203+
envFileName := "fixtures/unquoted.env"
204+
expectedValues := map[string]string{
205+
"OPTION_A": "some quoted phrase",
206+
"OPTION_B": "first one with an unquoted phrase",
207+
"OPTION_C": "then another one with an unquoted phrase",
208+
"OPTION_D": "then another one with an unquoted phrase special è char",
209+
"OPTION_E": "then another one quoted phrase",
210+
}
211+
212+
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
213+
}
214+
202215
func TestSubstitutions(t *testing.T) {
203216
envFileName := "fixtures/substitutions.env"
204217
expectedValues := map[string]string{

parser.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,21 @@ loop:
127127

128128
// extractVarValue extracts variable value and returns rest of slice
129129
func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (value string, rest []byte, err error) {
130-
quote, hasPrefix := hasQuotePrefix(src)
131-
if !hasPrefix {
132-
// unquoted value - read until whitespace
133-
end := bytes.IndexFunc(src, unicode.IsSpace)
134-
if end == -1 {
135-
return expandVariables(string(src), envMap, lookupFn), nil, nil
130+
quote, isQuoted := hasQuotePrefix(src)
131+
if !isQuoted {
132+
// unquoted value - read until new line
133+
end := bytes.IndexFunc(src, isNewLine)
134+
var rest []byte
135+
var value string
136+
if end < 0 {
137+
value := strings.TrimRightFunc(string(src), unicode.IsSpace)
138+
rest = nil
139+
return expandVariables(value, envMap, lookupFn), rest, nil
136140
}
137141

138-
return expandVariables(string(src[0:end]), envMap, lookupFn), src[end:], nil
142+
value = strings.TrimRightFunc(string(src[0:end]), unicode.IsSpace)
143+
rest = src[end:]
144+
return expandVariables(value, envMap, lookupFn), rest, nil
139145
}
140146

141147
// lookup quoted string terminator
@@ -192,7 +198,7 @@ func indexOfNonSpaceChar(src []byte) int {
192198
}
193199

194200
// hasQuotePrefix reports whether charset starts with single or double quote and returns quote character
195-
func hasQuotePrefix(src []byte) (prefix byte, isQuored bool) {
201+
func hasQuotePrefix(src []byte) (quote byte, isQuoted bool) {
196202
if len(src) == 0 {
197203
return 0, false
198204
}
@@ -221,3 +227,9 @@ func isSpace(r rune) bool {
221227
}
222228
return false
223229
}
230+
231+
232+
// isNewLine reports whether the rune is a new line character
233+
func isNewLine(r rune) bool {
234+
return r == '\n'
235+
}

0 commit comments

Comments
 (0)