Skip to content

Commit

Permalink
MF-519 - Refine Message (#567)
Browse files Browse the repository at this point in the history
* Enable Message JSON mashaling/unmarshaling

Update Protobuf generated files.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update readers and SDK to use Mainflux Message

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix alignment

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
  • Loading branch information
dborovcanin authored and nmarcetic committed Feb 6, 2019
1 parent f3f1fc5 commit bc9e00d
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 203 deletions.
80 changes: 39 additions & 41 deletions internal.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package mainflux

import "encoding/json"

// Type messageType is introduced to prevent cycle when calling Message
// MarshalJSON and UnmarshalJSON methods.
type messageType Message

// Struct message is an internal representation of Mainflux message to be serialized to JSON.
// Field `Value` is added to prevent marshaling of corresponding Message field.
type message struct {
messageType
Value isMessage_Value `json:"Value,omitempty"`
FloatValue *float64 `json:"value,omitempty"`
StringValue *string `json:"stringValue,omitempty"`
BoolValue *bool `json:"boolValue,omitempty"`
DataValue *string `json:"dataValue,omitempty"`
ValueSum *float64 `json:"valueSum,omitempty"`
}

// MarshalJSON method is used by `json` package to serialize Message.
func (m Message) MarshalJSON() ([]byte, error) {
msg := message{messageType: messageType(m)}

switch m.Value.(type) {
case *Message_FloatValue:
floatVal := m.GetFloatValue()
msg.FloatValue = &floatVal
case *Message_StringValue:
strVal := m.GetStringValue()
msg.StringValue = &strVal
case *Message_DataValue:
dataVal := m.GetDataValue()
msg.DataValue = &dataVal
case *Message_BoolValue:
boolVal := m.GetBoolValue()
msg.BoolValue = &boolVal
}

if m.GetValueSum() != nil {
valueSum := m.GetValueSum().GetValue()
msg.ValueSum = &valueSum
}

return json.Marshal(msg)
}

// UnmarshalJSON method is used by `json` package to unmarshal data to Message.
func (m *Message) UnmarshalJSON(data []byte) error {
var msg message
if err := json.Unmarshal(data, &msg); err != nil {
return err
}

*m = Message(msg.messageType)
m.Value = nil

switch {
case msg.FloatValue != nil:
m.Value = &Message_FloatValue{*msg.FloatValue}
case msg.StringValue != nil:
m.Value = &Message_StringValue{*msg.StringValue}
case msg.DataValue != nil:
m.Value = &Message_DataValue{*msg.DataValue}
case msg.BoolValue != nil:
m.Value = &Message_BoolValue{*msg.BoolValue}
}

if msg.ValueSum != nil {
m.ValueSum = &SumValue{Value: *msg.ValueSum}
}

return nil
}
Loading

0 comments on commit bc9e00d

Please sign in to comment.