From 0c98d7ad3b6aed709bb436a875e0a87d99f6c95c Mon Sep 17 00:00:00 2001 From: artpar Date: Thu, 25 Apr 2024 15:19:39 +0530 Subject: [PATCH] Update encoding logic and refactor database methods Update encoding and decoding logic for EventData in `middleware_eventgenerator.go` to use JSON instead of length encoding. Refactored `dbmethods.go` to introduce a new function, `ActionFromActionRow`, to reduce redundancy and improve code readability. Minor debugging related change made in `server.go`. --- server/resource/dbmethods.go | 36 +++++++++----------- server/resource/middleware_eventgenerator.go | 15 ++++---- server/server.go | 1 + 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/server/resource/dbmethods.go b/server/resource/dbmethods.go index 3dade54e..fd867999 100644 --- a/server/resource/dbmethods.go +++ b/server/resource/dbmethods.go @@ -71,16 +71,8 @@ func (dbResource *DbResource) GetActionByName(typeName string, actionName string var cachedActionRow ActionRow err = value.Scan(&cachedActionRow) - - CheckErr(err, "failed to unmarshal ActionSchema 76") - - if err == nil { - action.Name = cachedActionRow.Name - action.Label = cachedActionRow.Name - action.ReferenceId = cachedActionRow.ReferenceId - action.OnType = cachedActionRow.OnType - return action, err - } + action, err = ActionFromActionRow(cachedActionRow) + return action, err } } @@ -123,14 +115,7 @@ func (dbResource *DbResource) GetActionByName(typeName string, actionName string return action, err } - err = json.Unmarshal([]byte(actionRow.ActionSchema), &action) - CheckErr(err, "failed to unmarshal ActionSchema 127") - - action.Name = actionRow.Name - action.Label = actionRow.Name - action.ReferenceId = actionRow.ReferenceId - action.OnType = actionRow.OnType - action.InstanceOptional = actionRow.InstanceOptional + action, err = ActionFromActionRow(actionRow) if OlricCache != nil { @@ -138,7 +123,20 @@ func (dbResource *DbResource) GetActionByName(typeName string, actionName string //CheckErr(err, "Failed to set action in olric cache") } - return action, nil + return action, err +} + +func ActionFromActionRow(actionRow ActionRow) (Action, error) { + var action Action + err := json.Unmarshal([]byte(actionRow.ActionSchema), &action) + CheckErr(err, "failed to unmarshal ActionSchema 127") + + action.Name = actionRow.Name + action.Label = actionRow.Name + action.ReferenceId = actionRow.ReferenceId + action.OnType = actionRow.OnType + action.InstanceOptional = actionRow.InstanceOptional + return action, err } // GetActionsByType Gets the list of all actions defined on type `typeName` diff --git a/server/resource/middleware_eventgenerator.go b/server/resource/middleware_eventgenerator.go index 74086dab..2319bf38 100644 --- a/server/resource/middleware_eventgenerator.go +++ b/server/resource/middleware_eventgenerator.go @@ -47,7 +47,8 @@ func (e EventMessage) MarshalBinary() (data []byte, err error) { } // Simplified handling for EventData: encoding just the length (this should be replaced with actual data encoding logic) - if err := binary.Write(buffer, binary.BigEndian, int32(len(e.EventData))); err != nil { + jsonStr, err := json.MarshalToString(e.EventData) + if err := encodeString(buffer, jsonStr); err != nil { return nil, err } @@ -79,15 +80,13 @@ func (e *EventMessage) UnmarshalBinary(data []byte) error { e.ObjectType = objectType } - // Simplified handling for EventData (assuming only length was encoded) - var length int32 - if err := binary.Read(buffer, binary.BigEndian, &length); err != nil { + // Assume EventData is just the count of items (real logic needed to parse actual data) + if eventDataJson, err := decodeString(buffer); err != nil { + return err + } else { + err = json.Unmarshal([]byte(eventDataJson), &e.EventData) return err } - // Assume EventData is just the count of items (real logic needed to parse actual data) - e.EventData = make(map[string]interface{}, length) - - return nil } // Helper functions to encode and decode strings diff --git a/server/server.go b/server/server.go index 7f1061b7..a2da8202 100644 --- a/server/server.go +++ b/server/server.go @@ -806,6 +806,7 @@ func Main(boxRoot http.FileSystem, db database.DatabaseConnection, localStorageP for { msg := <-channel var eventMessage resource.EventMessage + //log.Infof("Message received: %s", msg.Payload) err = eventMessage.UnmarshalBinary([]byte(msg.Payload)) if err != nil { resource.CheckErr(err, "Failed to read message on channel "+typename)