Skip to content

Commit

Permalink
Update encoding logic and refactor database methods
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
artpar committed Apr 25, 2024
1 parent f57eb8e commit 0c98d7a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
36 changes: 17 additions & 19 deletions server/resource/dbmethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -123,22 +115,28 @@ 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 {

err = OlricCache.Put(context.Background(), cacheKey, actionRow, olric.EX(1*time.Minute), olric.NX())
//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`
Expand Down
15 changes: 7 additions & 8 deletions server/resource/middleware_eventgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0c98d7a

Please sign in to comment.