Skip to content

Commit

Permalink
Issue error when array max_count is not an int. (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
scudette authored Jan 23, 2024
1 parent 7856327 commit 069d4a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion array.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ func (self *ArrayParser) New(profile *Profile, options *ordereddict.Dict) (Parse

// Default to 0 length
result.options.Count, _ = options.GetInt64("count")
result.options.MaxCount, _ = options.GetInt64("max_count")
max_count_any, pres := options.Get("max_count")
if pres {
result.options.MaxCount, pres = to_int64(max_count_any)
if !pres {
return nil, fmt.Errorf("Array max_count must be an int not %T", max_count_any)
}
}

if result.options.MaxCount == 0 {
result.options.MaxCount = 1000
Expand Down
23 changes: 23 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ func TestStructParser(t *testing.T) {
goldie.Assert(t, "TestStructParser", serialized)
}

func TestArrayParserError(t *testing.T) {
profile := NewProfile()
AddModel(profile)

scope := MakeScope()
scope.SetLogger(log.New(os.Stderr, " ", 0))

definition := `
[
["TestStruct", 0, [
["Field1", 2, "Array", {
"max_count": "x=>x.Length",
"type": "uint8"
}],
]
]]
`
err := profile.ParseStructDefinitions(definition)
assert.Error(t, err)

assert.Contains(t, err.Error(), "Array max_count must be an int not string")
}

func TestArrayParser(t *testing.T) {
profile := NewProfile()
AddModel(profile)
Expand Down

0 comments on commit 069d4a7

Please sign in to comment.