Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion caveats.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package macaroon

import (
"bytes"
"encoding/json"
"fmt"
"time"
Expand Down Expand Up @@ -126,8 +127,17 @@ func (c UnregisteredCaveat) MarshalMsgpack() ([]byte, error) {
}

func (c *UnregisteredCaveat) UnmarshalMsgpack(data []byte) error {
dec := msgpack.GetDecoder()
defer msgpack.PutDecoder(dec)

dec.Reset(bytes.NewReader(data))
dec.SetMapDecoder(func(d *msgpack.Decoder) (interface{}, error) {
return d.DecodeUntypedMap()
})

c.RawMsgpack = data
return msgpack.Unmarshal(data, &c.Body)

return dec.Decode(&c.Body)
}

func (c UnregisteredCaveat) MarshalJSON() ([]byte, error) {
Expand Down
14 changes: 7 additions & 7 deletions caveats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func TestSimple(t *testing.T) {
}

type myUnregistered struct {
Bar map[string]string `json:"bar"`
Foo int `json:"foo"`
Bar map[int]string `json:"bar"`
Foo int `json:"foo"`
}

func (c *myUnregistered) CaveatType() CaveatType { return cavMyUnregistered }
Expand All @@ -156,7 +156,7 @@ func (c *myUnregistered) Prohibits(f Access) error { return nil }

func TestUnregisteredCaveatJSON(t *testing.T) {
RegisterCaveatType(&myUnregistered{})
c := &myUnregistered{Foo: 1, Bar: map[string]string{"a": "b"}}
c := &myUnregistered{Foo: 1, Bar: map[int]string{1: "b"}}
cs := NewCaveatSet(c)
b, err := json.Marshal(cs)
assert.NoError(t, err)
Expand All @@ -174,7 +174,7 @@ func TestUnregisteredCaveatJSON(t *testing.T) {
assert.Equal(t,
any(map[string]any{
"bar": map[string]any{
"a": "b",
"1": "b",
},
"foo": float64(1),
}),
Expand All @@ -199,7 +199,7 @@ func TestUnregisteredCaveatJSON(t *testing.T) {

func TestUnregisteredCaveatMsgpack(t *testing.T) {
RegisterCaveatType(&myUnregistered{})
c := &myUnregistered{Foo: 1, Bar: map[string]string{"a": "b"}}
c := &myUnregistered{Foo: 1, Bar: map[int]string{1: "b"}}
cs := NewCaveatSet(c)
b, err := cs.MarshalMsgpack()
assert.NoError(t, err)
Expand All @@ -215,8 +215,8 @@ func TestUnregisteredCaveatMsgpack(t *testing.T) {

assert.Equal(t,
any([]any{
map[string]any{
"a": "b",
map[any]any{
int8(1): "b",
},
int8(1),
}),
Expand Down