Skip to content

Commit 681f4d1

Browse files
Add support for empty maps or lists
1 parent 881ffb4 commit 681f4d1

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

pkg/markers/parse.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,17 @@ func guessType(scanner *sc.Scanner, raw string, allowSlice bool) *Argument {
310310
// We'll cross that bridge when we get there.
311311

312312
// look ahead till we can figure out if this is a map or a slice
313+
hint = peekNoSpace(subScanner)
313314
firstElemType := guessType(subScanner, subRaw, false)
314315
if firstElemType.Type == StringType {
315316
// might be a map or slice, parse the string and check for colon
316317
// (blech, basically arbitrary look-ahead due to raw strings).
317318
var keyVal string // just ignore this
318319
(&Argument{Type: StringType}).parseString(subScanner, raw, reflect.Indirect(reflect.ValueOf(&keyVal)))
319320

320-
if subScanner.Scan() == ':' {
321+
if token := subScanner.Scan(); token == ':' || hint == '}' {
321322
// it's got a string followed by a colon -- it's a map
323+
// or an empty map in case of {}
322324
return &Argument{
323325
Type: MapType,
324326
ItemType: &Argument{Type: AnyType},

pkg/markers/parse_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ var _ = Describe("Parsing", func() {
190190
It("should support delimitted slices of delimitted slices", argParseTestCase{arg: sliceOSlice, raw: "{{1,1},{2,3},{5,8}}", output: sliceOSliceOut}.Run)
191191

192192
It("should support maps", argParseTestCase{arg: Argument{Type: MapType, ItemType: &Argument{Type: StringType}}, raw: "{formal: hello, `informal`: `hi!`}", output: map[string]string{"formal": "hello", "informal": "hi!"}}.Run)
193+
It("should work with empty maps (which are equal to empty lists in the output)", argParseTestCase{arg: Argument{Type: MapType, ItemType: &Argument{Type: StringType}}, raw: "{}", output: map[string]string{}}.Run)
193194

194195
Context("with any value", func() {
195196
anyArg := Argument{Type: AnyType}

0 commit comments

Comments
 (0)