Skip to content

Commit 01f81eb

Browse files
authored
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.
1 parent cc3f827 commit 01f81eb

File tree

2 files changed

+45
-45
lines changed

2 files changed

+45
-45
lines changed

hooks/storage/storage.go

+44-44
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@ type Client struct {
4646

4747
// ClientProperties contains a limited set of the mqtt v5 properties specific to a client connection.
4848
type ClientProperties struct {
49-
AuthenticationData []byte `json:"authenticationData"`
50-
User []packets.UserProperty `json:"user"`
51-
AuthenticationMethod string `json:"authenticationMethod"`
52-
SessionExpiryInterval uint32 `json:"sessionExpiryInterval"`
53-
MaximumPacketSize uint32 `json:"maximumPacketSize"`
54-
ReceiveMaximum uint16 `json:"receiveMaximum"`
55-
TopicAliasMaximum uint16 `json:"topicAliasMaximum"`
56-
SessionExpiryIntervalFlag bool `json:"sessionExpiryIntervalFlag"`
57-
RequestProblemInfo byte `json:"requestProblemInfo"`
58-
RequestProblemInfoFlag bool `json:"requestProblemInfoFlag"`
59-
RequestResponseInfo byte `json:"requestResponseInfo"`
49+
AuthenticationData []byte `json:"authenticationData,omitempty"`
50+
User []packets.UserProperty `json:"user,omitempty"`
51+
AuthenticationMethod string `json:"authenticationMethod,omitempty"`
52+
SessionExpiryInterval uint32 `json:"sessionExpiryInterval,omitempty"`
53+
MaximumPacketSize uint32 `json:"maximumPacketSize,omitempty"`
54+
ReceiveMaximum uint16 `json:"receiveMaximum,omitempty"`
55+
TopicAliasMaximum uint16 `json:"topicAliasMaximum,omitempty"`
56+
SessionExpiryIntervalFlag bool `json:"sessionExpiryIntervalFlag,omitempty"`
57+
RequestProblemInfo byte `json:"requestProblemInfo,omitempty"`
58+
RequestProblemInfoFlag bool `json:"requestProblemInfoFlag,omitempty"`
59+
RequestResponseInfo byte `json:"requestResponseInfo,omitempty"`
6060
}
6161

6262
// ClientWill contains a will message for a client, and limited mqtt v5 properties.
6363
type ClientWill struct {
64-
Payload []byte `json:"payload"`
65-
User []packets.UserProperty `json:"user"`
66-
TopicName string `json:"topicName"`
67-
Flag uint32 `json:"flag"`
68-
WillDelayInterval uint32 `json:"willDelayInterval"`
69-
Qos byte `json:"qos"`
70-
Retain bool `json:"retain"`
64+
Payload []byte `json:"payload,omitempty"`
65+
User []packets.UserProperty `json:"user,omitempty"`
66+
TopicName string `json:"topicName,omitempty"`
67+
Flag uint32 `json:"flag,omitempty"`
68+
WillDelayInterval uint32 `json:"willDelayInterval,omitempty"`
69+
Qos byte `json:"qos,omitempty"`
70+
Retain bool `json:"retain,omitempty"`
7171
}
7272

7373
// MarshalBinary encodes the values into a json string.
@@ -85,29 +85,29 @@ func (d *Client) UnmarshalBinary(data []byte) error {
8585

8686
// Message is a storable representation of an MQTT message (specifically publish).
8787
type Message struct {
88-
Properties MessageProperties `json:"properties"` // -
89-
Payload []byte `json:"payload"` // the message payload (if retained)
90-
T string `json:"t"` // the data type
91-
ID string `json:"id" storm:"id"` // the storage key
92-
Origin string `json:"origin"` // the id of the client who sent the message
93-
TopicName string `json:"topic_name"` // the topic the message was sent to (if retained)
94-
FixedHeader packets.FixedHeader `json:"fixedheader"` // the header properties of the message
95-
Created int64 `json:"created"` // the time the message was created in unixtime
96-
Sent int64 `json:"sent"` // the last time the message was sent (for retries) in unixtime (if inflight)
97-
PacketID uint16 `json:"packet_id"` // the unique id of the packet (if inflight)
88+
Properties MessageProperties `json:"properties"` // -
89+
Payload []byte `json:"payload"` // the message payload (if retained)
90+
T string `json:"t,omitempty"` // the data type
91+
ID string `json:"id,omitempty" storm:"id"` // the storage key
92+
Origin string `json:"origin,omitempty"` // the id of the client who sent the message
93+
TopicName string `json:"topic_name,omitempty"` // the topic the message was sent to (if retained)
94+
FixedHeader packets.FixedHeader `json:"fixedheader"` // the header properties of the message
95+
Created int64 `json:"created,omitempty"` // the time the message was created in unixtime
96+
Sent int64 `json:"sent,omitempty"` // the last time the message was sent (for retries) in unixtime (if inflight)
97+
PacketID uint16 `json:"packet_id,omitempty"` // the unique id of the packet (if inflight)
9898
}
9999

100100
// MessageProperties contains a limited subset of mqtt v5 properties specific to publish messages.
101101
type MessageProperties struct {
102-
CorrelationData []byte `json:"correlationData"`
103-
SubscriptionIdentifier []int `json:"subscriptionIdentifier"`
104-
User []packets.UserProperty `json:"user"`
105-
ContentType string `json:"contentType"`
106-
ResponseTopic string `json:"responseTopic"`
107-
MessageExpiryInterval uint32 `json:"messageExpiry"`
108-
TopicAlias uint16 `json:"topicAlias"`
109-
PayloadFormat byte `json:"payloadFormat"`
110-
PayloadFormatFlag bool `json:"payloadFormatFlag"`
102+
CorrelationData []byte `json:"correlationData,omitempty"`
103+
SubscriptionIdentifier []int `json:"subscriptionIdentifier,omitempty"`
104+
User []packets.UserProperty `json:"user,omitempty"`
105+
ContentType string `json:"contentType,omitempty"`
106+
ResponseTopic string `json:"responseTopic,omitempty"`
107+
MessageExpiryInterval uint32 `json:"messageExpiry,omitempty"`
108+
TopicAlias uint16 `json:"topicAlias,omitempty"`
109+
PayloadFormat byte `json:"payloadFormat,omitempty"`
110+
PayloadFormatFlag bool `json:"payloadFormatFlag,omitempty"`
111111
}
112112

113113
// MarshalBinary encodes the values into a json string.
@@ -155,15 +155,15 @@ func (d *Message) ToPacket() packets.Packet {
155155

156156
// Subscription is a storable representation of an MQTT subscription.
157157
type Subscription struct {
158-
T string `json:"t"`
159-
ID string `json:"id" storm:"id"`
160-
Client string `json:"client"`
158+
T string `json:"t,omitempty"`
159+
ID string `json:"id,omitempty" storm:"id"`
160+
Client string `json:"client,omitempty"`
161161
Filter string `json:"filter"`
162-
Identifier int `json:"identifier"`
163-
RetainHandling byte `json:"retain_handling"`
162+
Identifier int `json:"identifier,omitempty"`
163+
RetainHandling byte `json:"retain_handling,omitempty"`
164164
Qos byte `json:"qos"`
165-
RetainAsPublished bool `json:"retain_as_pub"`
166-
NoLocal bool `json:"no_local"`
165+
RetainAsPublished bool `json:"retain_as_pub,omitempty"`
166+
NoLocal bool `json:"no_local,omitempty"`
167167
}
168168

169169
// MarshalBinary encodes the values into a json string.

hooks/storage/storage_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ var (
8989
Filter: "a/b/c",
9090
Qos: 1,
9191
}
92-
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}`)
92+
subscriptionJSON = []byte(`{"t":"subscription","id":"id","client":"mochi","filter":"a/b/c","qos":1}`)
9393

9494
sysInfoStruct = SystemInfo{
9595
T: "info",

0 commit comments

Comments
 (0)