Added omitempty to storage structs (#401)

This significantly cuts down the size of the marshaled JSON.

I've left it out on fields that, in my experience, never have the
default value.
This commit is contained in:
Jens Alfke
2024-05-19 11:12:57 -07:00
committed by GitHub
parent cc3f827fc1
commit 01f81ebeee
2 changed files with 45 additions and 45 deletions

View File

@@ -46,28 +46,28 @@ type Client struct {
// ClientProperties contains a limited set of the mqtt v5 properties specific to a client connection. // ClientProperties contains a limited set of the mqtt v5 properties specific to a client connection.
type ClientProperties struct { type ClientProperties struct {
AuthenticationData []byte `json:"authenticationData"` AuthenticationData []byte `json:"authenticationData,omitempty"`
User []packets.UserProperty `json:"user"` User []packets.UserProperty `json:"user,omitempty"`
AuthenticationMethod string `json:"authenticationMethod"` AuthenticationMethod string `json:"authenticationMethod,omitempty"`
SessionExpiryInterval uint32 `json:"sessionExpiryInterval"` SessionExpiryInterval uint32 `json:"sessionExpiryInterval,omitempty"`
MaximumPacketSize uint32 `json:"maximumPacketSize"` MaximumPacketSize uint32 `json:"maximumPacketSize,omitempty"`
ReceiveMaximum uint16 `json:"receiveMaximum"` ReceiveMaximum uint16 `json:"receiveMaximum,omitempty"`
TopicAliasMaximum uint16 `json:"topicAliasMaximum"` TopicAliasMaximum uint16 `json:"topicAliasMaximum,omitempty"`
SessionExpiryIntervalFlag bool `json:"sessionExpiryIntervalFlag"` SessionExpiryIntervalFlag bool `json:"sessionExpiryIntervalFlag,omitempty"`
RequestProblemInfo byte `json:"requestProblemInfo"` RequestProblemInfo byte `json:"requestProblemInfo,omitempty"`
RequestProblemInfoFlag bool `json:"requestProblemInfoFlag"` RequestProblemInfoFlag bool `json:"requestProblemInfoFlag,omitempty"`
RequestResponseInfo byte `json:"requestResponseInfo"` RequestResponseInfo byte `json:"requestResponseInfo,omitempty"`
} }
// ClientWill contains a will message for a client, and limited mqtt v5 properties. // ClientWill contains a will message for a client, and limited mqtt v5 properties.
type ClientWill struct { type ClientWill struct {
Payload []byte `json:"payload"` Payload []byte `json:"payload,omitempty"`
User []packets.UserProperty `json:"user"` User []packets.UserProperty `json:"user,omitempty"`
TopicName string `json:"topicName"` TopicName string `json:"topicName,omitempty"`
Flag uint32 `json:"flag"` Flag uint32 `json:"flag,omitempty"`
WillDelayInterval uint32 `json:"willDelayInterval"` WillDelayInterval uint32 `json:"willDelayInterval,omitempty"`
Qos byte `json:"qos"` Qos byte `json:"qos,omitempty"`
Retain bool `json:"retain"` Retain bool `json:"retain,omitempty"`
} }
// MarshalBinary encodes the values into a json string. // MarshalBinary encodes the values into a json string.
@@ -87,27 +87,27 @@ func (d *Client) UnmarshalBinary(data []byte) error {
type Message struct { type Message struct {
Properties MessageProperties `json:"properties"` // - Properties MessageProperties `json:"properties"` // -
Payload []byte `json:"payload"` // the message payload (if retained) Payload []byte `json:"payload"` // the message payload (if retained)
T string `json:"t"` // the data type T string `json:"t,omitempty"` // the data type
ID string `json:"id" storm:"id"` // the storage key ID string `json:"id,omitempty" storm:"id"` // the storage key
Origin string `json:"origin"` // the id of the client who sent the message Origin string `json:"origin,omitempty"` // the id of the client who sent the message
TopicName string `json:"topic_name"` // the topic the message was sent to (if retained) TopicName string `json:"topic_name,omitempty"` // the topic the message was sent to (if retained)
FixedHeader packets.FixedHeader `json:"fixedheader"` // the header properties of the message FixedHeader packets.FixedHeader `json:"fixedheader"` // the header properties of the message
Created int64 `json:"created"` // the time the message was created in unixtime Created int64 `json:"created,omitempty"` // the time the message was created in unixtime
Sent int64 `json:"sent"` // the last time the message was sent (for retries) in unixtime (if inflight) Sent int64 `json:"sent,omitempty"` // the last time the message was sent (for retries) in unixtime (if inflight)
PacketID uint16 `json:"packet_id"` // the unique id of the packet (if inflight) PacketID uint16 `json:"packet_id,omitempty"` // the unique id of the packet (if inflight)
} }
// MessageProperties contains a limited subset of mqtt v5 properties specific to publish messages. // MessageProperties contains a limited subset of mqtt v5 properties specific to publish messages.
type MessageProperties struct { type MessageProperties struct {
CorrelationData []byte `json:"correlationData"` CorrelationData []byte `json:"correlationData,omitempty"`
SubscriptionIdentifier []int `json:"subscriptionIdentifier"` SubscriptionIdentifier []int `json:"subscriptionIdentifier,omitempty"`
User []packets.UserProperty `json:"user"` User []packets.UserProperty `json:"user,omitempty"`
ContentType string `json:"contentType"` ContentType string `json:"contentType,omitempty"`
ResponseTopic string `json:"responseTopic"` ResponseTopic string `json:"responseTopic,omitempty"`
MessageExpiryInterval uint32 `json:"messageExpiry"` MessageExpiryInterval uint32 `json:"messageExpiry,omitempty"`
TopicAlias uint16 `json:"topicAlias"` TopicAlias uint16 `json:"topicAlias,omitempty"`
PayloadFormat byte `json:"payloadFormat"` PayloadFormat byte `json:"payloadFormat,omitempty"`
PayloadFormatFlag bool `json:"payloadFormatFlag"` PayloadFormatFlag bool `json:"payloadFormatFlag,omitempty"`
} }
// MarshalBinary encodes the values into a json string. // MarshalBinary encodes the values into a json string.
@@ -155,15 +155,15 @@ func (d *Message) ToPacket() packets.Packet {
// Subscription is a storable representation of an MQTT subscription. // Subscription is a storable representation of an MQTT subscription.
type Subscription struct { type Subscription struct {
T string `json:"t"` T string `json:"t,omitempty"`
ID string `json:"id" storm:"id"` ID string `json:"id,omitempty" storm:"id"`
Client string `json:"client"` Client string `json:"client,omitempty"`
Filter string `json:"filter"` Filter string `json:"filter"`
Identifier int `json:"identifier"` Identifier int `json:"identifier,omitempty"`
RetainHandling byte `json:"retain_handling"` RetainHandling byte `json:"retain_handling,omitempty"`
Qos byte `json:"qos"` Qos byte `json:"qos"`
RetainAsPublished bool `json:"retain_as_pub"` RetainAsPublished bool `json:"retain_as_pub,omitempty"`
NoLocal bool `json:"no_local"` NoLocal bool `json:"no_local,omitempty"`
} }
// MarshalBinary encodes the values into a json string. // MarshalBinary encodes the values into a json string.

View File

@@ -89,7 +89,7 @@ var (
Filter: "a/b/c", Filter: "a/b/c",
Qos: 1, Qos: 1,
} }
subscriptionJSON = []byte(`{"t":"subscription","id":"id","client":"mochi","filter":"a/b/c","identifier":0,"retain_handling":0,"qos":1,"retain_as_pub":false,"no_local":false}`) subscriptionJSON = []byte(`{"t":"subscription","id":"id","client":"mochi","filter":"a/b/c","qos":1}`)
sysInfoStruct = SystemInfo{ sysInfoStruct = SystemInfo{
T: "info", T: "info",