Skip to content

Commit e126d36

Browse files
committed
relax whitespace rules when parsing parameters
1 parent afecfbf commit e126d36

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

internal/param/param.go

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package param
22

33
import (
44
"bufio"
5-
"errors"
65
"fmt"
76
"io"
87
"strings"
@@ -40,6 +39,10 @@ func Parse(s string) ([]Param, error) {
4039
var pp []Param
4140
br := bufio.NewReader(strings.NewReader(s))
4241
for i := 0; true; i++ {
42+
// skip whitespace
43+
if err := skipWhite(br); err != nil {
44+
return nil, err
45+
}
4346
// see if there's more to read
4447
if _, err := br.Peek(1); err == io.EOF {
4548
break
@@ -75,16 +78,26 @@ func parseIdent(br *bufio.Reader) (string, error) {
7578
return string(ident), nil
7679
}
7780

81+
func parseByte(br *bufio.Reader, expect byte) error {
82+
b, err := br.ReadByte()
83+
if err != nil {
84+
if err == io.EOF {
85+
return fmt.Errorf("expected '%c', got EOF", expect)
86+
}
87+
return err
88+
}
89+
if b != expect {
90+
return fmt.Errorf("expected '%c', got '%c'", expect, b)
91+
}
92+
return nil
93+
}
94+
7895
func parseString(br *bufio.Reader) (string, error) {
7996
var s []rune
8097
// read the open quote
81-
b, err := br.ReadByte()
82-
if err != nil {
98+
if err := parseByte(br, '"'); err != nil {
8399
return "", err
84100
}
85-
if b != '"' {
86-
return "", errors.New("missing open quote")
87-
}
88101
// read the string
89102
var escaped bool
90103
for {
@@ -110,21 +123,33 @@ func parseString(br *bufio.Reader) (string, error) {
110123
return string(s), nil
111124
}
112125

113-
func skipComma(br *bufio.Reader) error {
126+
func skipWhite(br *bufio.Reader) error {
114127
for {
115128
b, err := br.ReadByte()
116129
if err != nil {
130+
if err == io.EOF {
131+
return nil
132+
}
117133
return err
118134
}
119-
if b != ' ' && b != ',' {
135+
if b != ' ' {
120136
return br.UnreadByte()
121137
}
122138
}
123139
}
124140

125141
func parseParam(br *bufio.Reader, first bool) (Param, error) {
142+
// skip whitespace
143+
if err := skipWhite(br); err != nil {
144+
return Param{}, err
145+
}
126146
if !first {
127-
if err := skipComma(br); err != nil {
147+
// read the comma separator
148+
if err := parseByte(br, ','); err != nil {
149+
return Param{}, err
150+
}
151+
// skip whitespace
152+
if err := skipWhite(br); err != nil {
128153
return Param{}, err
129154
}
130155
}
@@ -133,13 +158,17 @@ func parseParam(br *bufio.Reader, first bool) (Param, error) {
133158
if err != nil {
134159
return Param{}, err
135160
}
161+
// skip whitespace
162+
if err := skipWhite(br); err != nil {
163+
return Param{}, err
164+
}
136165
// read the equals sign
137-
eq, err := br.ReadByte()
138-
if err != nil {
166+
if err := parseByte(br, '='); err != nil {
139167
return Param{}, err
140168
}
141-
if eq != '=' {
142-
return Param{}, fmt.Errorf("expected '=', got %c", eq)
169+
// skip whitespace
170+
if err := skipWhite(br); err != nil {
171+
return Param{}, err
143172
}
144173
// read the value
145174
var value string

internal/param/param_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
func TestParam(t *testing.T) {
1111
tests := []struct {
1212
input string
13+
output string
1314
err string
1415
params []Param
1516
}{
@@ -84,6 +85,14 @@ func TestParam(t *testing.T) {
8485
{Key: "key", Value: "Hello, 世界", Quote: true},
8586
},
8687
},
88+
{
89+
input: " key = value , key2 = value2 ",
90+
output: "key=value, key2=value2",
91+
params: []Param{
92+
{Key: "key", Value: "value"},
93+
{Key: "key2", Value: "value2"},
94+
},
95+
},
8796
}
8897
for i, tt := range tests {
8998
tt := tt
@@ -101,7 +110,11 @@ func TestParam(t *testing.T) {
101110
if tt.err != "" {
102111
return
103112
}
104-
assert.DeepEqual(t, Format(tt.params...), tt.input)
113+
output := tt.output
114+
if output == "" {
115+
output = tt.input
116+
}
117+
assert.DeepEqual(t, Format(tt.params...), output)
105118
})
106119
})
107120
}

0 commit comments

Comments
 (0)