Skip to content

Commit

Permalink
first and probably the last version of the package
Browse files Browse the repository at this point in the history
  • Loading branch information
onokonem committed Dec 14, 2023
1 parent d380fb5 commit ba43c4d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func (r Raw) AsNumber() float64 {
return v
}

// AsBool returns a boolean contained in the Raw or false in case there is not a boolean.
func (r Raw) AsBool() bool {
v, _ := r.payload.(bool)

return v
}

// AsList returns an array (actually slice) contained in the Raw or nil in case there is not an array.
func (r Raw) AsList() List {
l, ok := r.payload.([]interface{})
Expand Down
3 changes: 3 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ func TestRaw(t *testing.T) {
require.Nil(t, raw.AsMap().Get("mess").AsMap().Get("nonexistent").AsMap())
require.Equal(t, freeform.Raw{}, raw.AsMap().Get("mess").AsMap().Get("nonexistent").AsMap().Get(""))
require.Equal(t, "", raw.AsString())
require.False(t, raw.AsBool())
require.True(t, raw.AsMap().Get("bool true").AsBool())
require.False(t, raw.AsMap().Get("bool false").AsBool())
}
2 changes: 2 additions & 0 deletions testdata/good.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
200,
400
],
"bool false": false,
"bool true": true,
"map": {
"0": 100,
"1": 500,
Expand Down

0 comments on commit ba43c4d

Please sign in to comment.