Skip to content

Commit e693ecd

Browse files
committed
💚 test: fix the internal ParseEnvLines in some case error
1 parent 2b5a873 commit e693ecd

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

envutil/envutil.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ func ParseEnvValue(val string) string { return varexpr.SafeParse(val) }
5151

5252
// SplitText2map parse ENV text to map. Can use to parse .env file contents.
5353
func SplitText2map(text string) map[string]string {
54-
envMp, _ := comfunc.ParseEnvLines(text, true)
54+
envMp, _ := comfunc.ParseEnvLines(text, comfunc.ParseEnvLineOption{
55+
SkipOnErrorLine: true,
56+
})
5557
return envMp
5658
}
5759

@@ -60,5 +62,5 @@ func SplitLineToKv(line string) (string, string) {
6062
if line = strings.TrimSpace(line); line == "" {
6163
return "", ""
6264
}
63-
return comfunc.SplitLineToKv(line, "")
65+
return comfunc.SplitLineToKv(line, "=")
6466
}

internal/comfunc/str_func.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ package comfunc
33
import (
44
"fmt"
55
"strings"
6-
76
)
87

98
var commentsPrefixes = []string{"#", ";", "//"}
109

10+
// ParseEnvLineOption parse env line options
11+
type ParseEnvLineOption struct {
12+
// NotInlineComments dont parse inline comments.
13+
// - default: false. will parse inline comments
14+
NotInlineComments bool
15+
// SkipOnErrorLine skip error line, continue parse next line
16+
// - False: return error, clear parsed map
17+
SkipOnErrorLine bool
18+
}
19+
1120
// ParseEnvLines parse simple multiline k-v string to a string-map.
1221
// Can use to parse simple INI or DOTENV file contents.
1322
//
@@ -17,7 +26,7 @@ var commentsPrefixes = []string{"#", ";", "//"}
1726
// - Support comments line starts with: "#", ";", "//"
1827
// - Support inline comments split with: " #" eg: name=tom # a comments
1928
// - DON'T support submap parse.
20-
func ParseEnvLines(text string, parseInlineComments bool) (mp map[string]string, err error) {
29+
func ParseEnvLines(text string, opt ParseEnvLineOption) (mp map[string]string, err error) {
2130
lines := strings.Split(text, "\n")
2231
ln := len(lines)
2332
if ln == 0 {
@@ -32,11 +41,16 @@ func ParseEnvLines(text string, parseInlineComments bool) (mp map[string]string,
3241
}
3342

3443
// skip comments line
35-
if strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") || strings.HasPrefix(line, "//") {
44+
if line[0] == '#' || line[0] == ';' || strings.HasPrefix(line, "//") {
3645
continue
3746
}
3847

39-
if !strings.ContainsRune(line, '=') {
48+
// invalid line
49+
if strings.IndexByte(line, '=') < 1 {
50+
if opt.SkipOnErrorLine {
51+
continue
52+
}
53+
4054
strMap = nil
4155
err = fmt.Errorf("invalid line contents: must match `KEY=VAL`(line: %s)", line)
4256
return
@@ -45,7 +59,7 @@ func ParseEnvLines(text string, parseInlineComments bool) (mp map[string]string,
4559
key, value := SplitLineToKv(line, "=")
4660

4761
// check and remove inline comments
48-
if parseInlineComments {
62+
if !opt.NotInlineComments {
4963
if pos := strings.Index(value, " #"); pos > 0 {
5064
value = strings.TrimRight(value[0:pos], " \t")
5165
}
@@ -58,7 +72,8 @@ func ParseEnvLines(text string, parseInlineComments bool) (mp map[string]string,
5872
}
5973

6074
// SplitLineToKv parse string line to k-v. eg:
61-
// 'DEBUG=true' => ['DEBUG', 'true']
75+
//
76+
// 'DEBUG=true' => ['DEBUG', 'true']
6277
//
6378
// NOTE: line must contain '=', allow: 'ENV_KEY='
6479
func SplitLineToKv(line, sep string) (string, string) {
@@ -77,4 +92,4 @@ func SplitLineToKv(line, sep string) (string, string) {
7792
return "", ""
7893
}
7994
return envKey, strings.TrimSpace(nodes[1])
80-
}
95+
}

strutil/textutil/textutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func ParseInlineINI(tagVal string, keys ...string) (mp maputil.SMap, err error)
7272
// - it's like INI format contents.
7373
// - support comments line with: "#", ";", "//"
7474
// - support inline comments with: " #" eg: name=tom # a comments
75-
// - don't support submap parse.
75+
// - DON'T support submap parse.
7676
func ParseSimpleINI(text string) (mp maputil.SMap, err error) {
77-
return comfunc.ParseEnvLines(text, true)
77+
return comfunc.ParseEnvLines(text, comfunc.ParseEnvLineOption{})
7878
}

strutil/textutil/textutil_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func TestParseSimpleINI(t *testing.T) {
171171
input := "invalid line"
172172
mp, err := textutil.ParseSimpleINI(input)
173173
assert.Err(t, err)
174-
assert.Contains(t, err.Error(), "invalid config line")
174+
assert.Contains(t, err.Error(), "invalid line contents")
175175
assert.Nil(t, mp)
176176
})
177177

@@ -202,7 +202,7 @@ func TestParseSimpleINI(t *testing.T) {
202202
input := "# comment\nkey1=value1\n\nkey2=value2 # inline comment\ninvalidline"
203203
mp, err := textutil.ParseSimpleINI(input)
204204
assert.Err(t, err)
205-
assert.ErrMsgContains(t, err, "invalid config line")
205+
assert.ErrMsgContains(t, err, "invalid line contents")
206206
assert.Empty(t, mp)
207207
})
208208
}

0 commit comments

Comments
 (0)