Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit empty fields in requests. #30

Merged
merged 2 commits into from
May 17, 2024
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
64 changes: 26 additions & 38 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,32 @@ type Request struct {

// MarshalCBOR implements the CBOR marshaler interface.
func (r *Request) MarshalCBOR() ([]byte, error) {
return cbor.Marshal(requestRaw{
Type: r.Type,
Sender: r.Sender.Raw,
Nonce: r.Nonce,
IngressExpiry: r.IngressExpiry,
CanisterID: r.CanisterID.Raw,
MethodName: r.MethodName,
Arguments: r.Arguments,
Paths: r.Paths,
})
}

// UnmarshalCBOR implements the CBOR unmarshaler interface.
func (r *Request) UnmarshalCBOR(data []byte) error {
var raw requestRaw
if err := cbor.Unmarshal(data, &raw); err != nil {
return err
}
r.Type = raw.Type
r.Sender = principal.Principal{Raw: raw.Sender}
r.Nonce = raw.Nonce
r.IngressExpiry = raw.IngressExpiry
r.CanisterID = principal.Principal{Raw: raw.CanisterID}
r.MethodName = raw.MethodName
r.Arguments = raw.Arguments
r.Paths = raw.Paths
return nil
m := make(map[string]any)
if len(r.Type) != 0 {
m["request_type"] = r.Type
}
if r.CanisterID.Raw != nil {
m["canister_id"] = r.CanisterID.Raw
}
if len(r.MethodName) != 0 {
m["method_name"] = r.MethodName
}
if len(r.Arguments) != 0 {
m["arg"] = r.Arguments
}
if len(r.Sender.Raw) != 0 {
m["sender"] = r.Sender.Raw
}
if r.IngressExpiry != 0 {
m["ingress_expiry"] = r.IngressExpiry
}
if len(r.Nonce) != 0 {
m["nonce"] = r.Nonce
}
if r.Paths != nil {
m["paths"] = r.Paths
}
return cbor.Marshal(m)
}

// RequestID is the request ID.
Expand Down Expand Up @@ -167,14 +166,3 @@ const (
// RequestTypeReadState is a read state request.
RequestTypeReadState RequestType = "read_state"
)

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"`
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))
}
}
Loading