~xenrox/go-scfg

3e1672da946ae869d171f2d1642313d4854fd90e — Thorben Günther 7 months ago 76adf4a master
Allow to set empty strings

Something like this in the config file

    test ""

would currently fail with the following error:

   directive "test": want 1 params, got 0

By allowing go-scfg to read empty strings, it would be possible to set
default values, but still allow users to unset those.
2 files changed, 5 insertions(+), 2 deletions(-)

M reader.go
M reader_test.go
M reader.go => reader.go +3 -0
@@ 159,6 159,9 @@ func splitWords(l string) ([]string, error) {
		case quote != 0 && ch == quote:
			quote = 0
			wantWSP = true
			if sb.Len() == 0 {
				words = append(words, "")
			}
		case quote == 0 && len(words) == 0 && sb.Len() == 0 && ch == '#':
			return nil, nil
		case quote == 0 && (ch == '\'' || ch == '"'):

M reader_test.go => reader_test.go +2 -2
@@ 15,14 15,14 @@ var readTests = []struct {
}{
	{
		name: "flat",
		src: `dir1 param1 param2 param3
		src: `dir1 param1 param2 "" param3
dir2
dir3 param1

# comment
dir4 "param 1" 'param 2'`,
		want: Block{
			{Name: "dir1", Params: []string{"param1", "param2", "param3"}},
			{Name: "dir1", Params: []string{"param1", "param2", "", "param3"}},
			{Name: "dir2", Params: []string{}},
			{Name: "dir3", Params: []string{"param1"}},
			{Name: "dir4", Params: []string{"param 1", "param 2"}},