Skip to content
Snippets Groups Projects
Commit 90b302b9 authored by thijsheijden's avatar thijsheijden
Browse files

Added MessageStruct to describe outgoing messages

Replaced all maps in the handleMessage method with the MessageStruct,
except for the final result, as this one uses the key 'values', where
the rest use the key 'value'. Once this has been changed in the frontend
it will be updated in the backend as well.
parent 241c671a
No related branches found
No related tags found
No related merge requests found
package entity
// MessageStruct describes outgoing messages from the query service
type MessageStruct struct {
Type string `json:"type"`
Value interface{} `json:"value"`
}
...@@ -2,6 +2,7 @@ package consume ...@@ -2,6 +2,7 @@ package consume
import ( import (
"encoding/json" "encoding/json"
"query-service/internal/entity"
"query-service/pkg/errorhandler" "query-service/pkg/errorhandler"
"strings" "strings"
...@@ -21,12 +22,26 @@ func (s *Service) HandleMessage(msg *broker.Message) { ...@@ -21,12 +22,26 @@ func (s *Service) HandleMessage(msg *broker.Message) {
return return
} }
// Marshall the incoming message into an IncomingJSONQuery object
var JSONQuery entity.IncomingQueryJSON
err := json.Unmarshal(msg.Body, &JSONQuery)
if err != nil {
errorMsg := entity.MessageStruct{
Type: "query_translation_error",
Value: "failed to unmarshall the incoming JSON query",
}
errorMsgBytes, _ := json.Marshal(errorMsg)
s.producer.PublishMessage(&errorMsgBytes, &sessionID)
return
}
// Convert the json byte msg to a query string // Convert the json byte msg to a query string
query, databaseName, err := s.queryConverter.ConvertQuery(&msg.Body) query, databaseName, err := s.queryConverter.ConvertQuery(&msg.Body)
if err != nil { if err != nil {
errorMsg := make(map[string]string) errorMsg := entity.MessageStruct{
errorMsg["type"] = "query_translation_error" Type: "query_translation_error",
errorMsg["value"] = err.Error() Value: err.Error(),
}
errorMsgBytes, _ := json.Marshal(errorMsg) errorMsgBytes, _ := json.Marshal(errorMsg)
s.producer.PublishMessage(&errorMsgBytes, &sessionID) s.producer.PublishMessage(&errorMsgBytes, &sessionID)
return return
...@@ -34,35 +49,39 @@ func (s *Service) HandleMessage(msg *broker.Message) { ...@@ -34,35 +49,39 @@ func (s *Service) HandleMessage(msg *broker.Message) {
// Check if there was a database name // Check if there was a database name
if *databaseName == "" { if *databaseName == "" {
errorMsg := make(map[string]string) errorMsg := entity.MessageStruct{
errorMsg["type"] = "query_malformed_request_error" Type: "query_malformed_request_error",
errorMsg["value"] = "no database name supplied" Value: "no database name supplied",
}
errorMsgBytes, _ := json.Marshal(errorMsg) errorMsgBytes, _ := json.Marshal(errorMsg)
s.producer.PublishMessage(&errorMsgBytes, &sessionID) s.producer.PublishMessage(&errorMsgBytes, &sessionID)
return return
} }
// TODO: Figure out a way to do this in the integration tests instead
// Remove newlines and tabs from string // Remove newlines and tabs from string
queryNoNewlines := strings.ReplaceAll(*query, "\n", "") queryNoNewlines := strings.ReplaceAll(*query, "\n", "")
queryNoTabs := strings.ReplaceAll(queryNoNewlines, "\t", "") queryNoTabs := strings.ReplaceAll(queryNoNewlines, "\t", "")
// Send the resulting query back to the client // Send the resulting query back to the client
msgmap := make(map[string]interface{}) translationMsg := entity.MessageStruct{
msgmap["type"] = "query_translation_result" Type: "query_translation_result",
msgmap["values"] = queryNoTabs Value: queryNoTabs,
msgbyte, err := json.Marshal(msgmap) }
translationMsgBytes, err := json.Marshal(translationMsg)
if err != nil { if err != nil {
errorhandler.LogError(err, "Marshalling query_translation_result went wrong!") // TODO: send error message to client instead errorhandler.LogError(err, "Marshalling query_translation_result went wrong!") // TODO: send error message to client instead
return return
} }
s.producer.PublishMessage(&msgbyte, &sessionID) // TODO: should this be a go routine? s.producer.PublishMessage(&translationMsgBytes, &sessionID) // TODO: should this be a go routine?
// Fetch database credentials from the user service // Fetch database credentials from the user service
databaseInfo, err := s.databaseInfoService.GetDatabaseInfo(&clientID, databaseName) databaseInfo, err := s.databaseInfoService.GetDatabaseInfo(&clientID, databaseName)
if err != nil { if err != nil {
errorMsg := make(map[string]string) errorMsg := entity.MessageStruct{
errorMsg["type"] = "query_database_not_found" Type: "query_database_not_found",
errorMsg["value"] = err.Error() Value: err.Error(),
}
errorMsgBytes, _ := json.Marshal(errorMsg) errorMsgBytes, _ := json.Marshal(errorMsg)
s.producer.PublishMessage(&errorMsgBytes, &sessionID) s.producer.PublishMessage(&errorMsgBytes, &sessionID)
return return
...@@ -72,15 +91,17 @@ func (s *Service) HandleMessage(msg *broker.Message) { ...@@ -72,15 +91,17 @@ func (s *Service) HandleMessage(msg *broker.Message) {
// convert result to general (node-link) format // convert result to general (node-link) format
result, err := s.requestSender.SendAQLQuery(*query, databaseInfo.Username, databaseInfo.Password, databaseInfo.URL, databaseInfo.Port, databaseInfo.InternalDatabaseName) result, err := s.requestSender.SendAQLQuery(*query, databaseInfo.Username, databaseInfo.Password, databaseInfo.URL, databaseInfo.Port, databaseInfo.InternalDatabaseName)
if err != nil { if err != nil {
errorMsg := make(map[string]string) errorMsg := entity.MessageStruct{
errorMsg["type"] = "query_database_error" Type: "query_database_error",
errorMsg["value"] = err.Error() Value: err.Error(),
}
errorMsgBytes, _ := json.Marshal(errorMsg) errorMsgBytes, _ := json.Marshal(errorMsg)
s.producer.PublishMessage(&errorMsgBytes, &sessionID) s.producer.PublishMessage(&errorMsgBytes, &sessionID)
return return
} }
// Add type indicator to result from database // Add type indicator to result from database
// TODO: Change key 'values' to key 'value'
var res interface{} var res interface{}
json.Unmarshal(*result, &res) json.Unmarshal(*result, &res)
resultMsgMap := make(map[string]interface{}) resultMsgMap := make(map[string]interface{})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment