Skip to content

Commit

Permalink
Omit empty fields in requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 17, 2024
1 parent 6602f73 commit ad8d9cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
14 changes: 7 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ const (
)

type requestRaw struct {
Type RequestType `cbor:"request_type"`
Sender []byte `cbor:"sender"`
Nonce []byte `cbor:"nonce"`
IngressExpiry uint64 `cbor:"ingress_expiry"`
CanisterID []byte `cbor:"canister_id"`
MethodName string `cbor:"method_name"`
Arguments []byte `cbor:"arg"`
Type RequestType `cbor:"request_type,omitempty"`
Sender []byte `cbor:"sender,omitempty"`
Nonce []byte `cbor:"nonce,omitempty"`
IngressExpiry uint64 `cbor:"ingress_expiry,omitempty"`
CanisterID []byte `cbor:"canister_id,omitempty"`
MethodName string `cbor:"method_name,omitempty"`
Arguments []byte `cbor:"arg,omitempty"`
Paths [][]hashtree.Label `cbor:"paths,omitempty"`
}
22 changes: 22 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package agent_test

import (
"fmt"
"github.com/fxamacker/cbor/v2"
"testing"
"time"

"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/certification/hashtree"
Expand Down Expand Up @@ -82,3 +84,23 @@ func TestRequestID_Sign(t *testing.T) {
t.Error(h)
}
}

func TestRequest_MarshalCBOR(t *testing.T) {
request := agent.Request{
Type: agent.RequestTypeReadState,
Sender: principal.AnonymousID,
Paths: [][]hashtree.Label{{hashtree.Label("subnet")}},
IngressExpiry: uint64(time.Now().Add(time.Minute).UnixNano()),
}
encoded, err := cbor.Marshal(request)
if err != nil {
t.Fatal(err)
}
var r map[string]any
if err := cbor.Unmarshal(encoded, &r); err != nil {
t.Fatal(err)
}
if len(r) != 4 {
t.Error(len(r))
}
}

0 comments on commit ad8d9cd

Please sign in to comment.