Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix trailing comment parse in properties #2371

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changelog/db2095be68314e47882d4a8881cc72ff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "db2095be-6831-4e47-882d-4a8881cc72ff",
"type": "bugfix",
"description": "Fix recognition of trailing comments in shared config properties. # or ; separators that aren't preceded by whitespace at the end of a property value should be considered part of it.",
"modules": [
"internal/ini"
]
}
2 changes: 1 addition & 1 deletion internal/ini/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (p *parser) handleSubProperty(tok *lineTokenSubProperty) {
// "promote" this to a normal property.
p.handleProperty(&lineTokenProperty{
Key: tok.Key,
Value: strings.TrimSpace(trimComment(tok.Value)),
Value: strings.TrimSpace(trimPropertyComment(tok.Value)),
})
return
}
Expand Down
20 changes: 15 additions & 5 deletions internal/ini/strings.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package ini

import "strings"
import (
"strings"
)

func trimComment(v string) string {
rest, _, _ := strings.Cut(v, "#")
rest, _, _ = strings.Cut(rest, ";")
return rest
func trimProfileComment(s string) string {
r, _, _ := strings.Cut(s, "#")
r, _, _ = strings.Cut(r, ";")
return r
}

func trimPropertyComment(s string) string {
r, _, _ := strings.Cut(s, " #")
r, _, _ = strings.Cut(r, " ;")
r, _, _ = strings.Cut(r, "\t#")
r, _, _ = strings.Cut(r, "\t;")
return r
}

// assumes no surrounding comment
Expand Down
16 changes: 16 additions & 0 deletions internal/ini/testdata/valid/comments
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[profile comments];comment
a = foo#notcomment
b = foo;notcomment
c = foo#
d = foo;
e = foo bar # comment
f = foo bar ; comment
g = foo # comment
h = foo ; comment
i = # comment
j = ; comment
k = foo # ; comment
l = foo ; # comment
m = foo
n = foo #
o = foo ;
19 changes: 19 additions & 0 deletions internal/ini/testdata/valid/comments_expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"profile comments": {
"a": "foo#notcomment",
"b": "foo;notcomment",
"c": "foo#",
"d": "foo;",
"e": "foo bar",
"f": "foo \t bar",
"g": "foo",
"h": "foo",
"i": "",
"j": "",
"k": "foo",
"l": "foo",
"m": "foo",
"n": "foo",
"o": "foo"
}
}
5 changes: 3 additions & 2 deletions internal/ini/tokenize.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func isLineComment(line string) bool {
}

func asProfile(line string) *lineTokenProfile { // " [ type name ] ; comment"
trimmed := strings.TrimSpace(trimComment(line)) // "[ type name ]"
trimmed := strings.TrimSpace(trimProfileComment(line)) // "[ type name ]"
if !isBracketed(trimmed) {
return nil
}
Expand All @@ -48,7 +48,8 @@ func asProperty(line string) *lineTokenProperty {
return nil
}

trimmed := strings.TrimRight(trimComment(line), " \t")
trimmed := trimPropertyComment(line)
trimmed = strings.TrimRight(trimmed, " \t")
k, v, ok := splitProperty(trimmed)
if !ok {
return nil
Expand Down
Loading