Skip to content

Commit

Permalink
Merge pull request #4 from AllanCapistrano/refactor/serialize
Browse files Browse the repository at this point in the history
Refactor/serialize
  • Loading branch information
AllanCapistrano authored Sep 29, 2023
2 parents a603a93 + a1148b5 commit 3ff7a59
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"log"
"strings"

infoNode "github.com/allancapistrano/tangle-client-go/info"
"github.com/allancapistrano/tangle-client-go/messages"
Expand All @@ -24,13 +23,12 @@ func main() {
// messages.SubmitMessage(nodeURL, "LB_REPLY", "{asdfghjkl}", 15)

// Reading some messages by an index.
messagesByIndex, err := messages.GetAllMessagesByIndex(nodeURL, "LB_ENTRY_REPLY")
messagesByIndex, err := messages.GetAllMessagesByIndex(nodeURL, "REP_EVALUATION")
if err != nil {
log.Fatal(err)
}

for _, v := range messagesByIndex {
fmt.Println([]byte(strings.Trim(v.Content, "\t")))
fmt.Printf("Index: %s | Content: %s\n", v.Index, v.Content)
}

Expand Down
18 changes: 6 additions & 12 deletions messages/getAllMessagesByIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,15 @@ func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) {
Content: err.Error(),
}
} else {
message, err = formatMessagePayload(*messageReturned, index)
indexationPayload := messageReturned.Payload.(*iotago.Indexation)

if err != nil {
log.Println(err)

message = Message{
Index: "Error",
Content: err.Error(),
}
} else {
sanitizeMessage(&message)

messages = append(messages, message)
message = Message{
Index: string(indexationPayload.Index),
Content: string(indexationPayload.Data),
}
}

messages = append(messages, message)
}
} else {
log.Println("No messages with this index were found.")
Expand Down
15 changes: 5 additions & 10 deletions messages/getMessageByMessageId.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,12 @@ func GetMessageFormattedByMessageID(nodeUrl string, messageIdHex string) (Messag
Content: err.Error(),
}
} else {
message, err = formatMessagePayloadWithoutIndex(messageReturned)
if err != nil {
log.Println(err)

message = Message{
Index: "Error",
Content: err.Error(),
}
}
indexationPayload := messageReturned.Payload.(*iotago.Indexation)

sanitizeMessage(&message)
message = Message{
Index: string(indexationPayload.Index),
Content: string(indexationPayload.Data),
}
}

return message, nil
Expand Down
16 changes: 16 additions & 0 deletions messages/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ type Message struct {
}

// Sanitizes a message.
//
// Deprecated: sanitizeMessage exists for historical compatibility
// and should not be used. You no longer need to sanitize a message.
func sanitizeMessage(message *Message) {
message.Content = utils.SanitizeString(message.Content)
message.Index = utils.SanitizeString(message.Index)
}

// Formats the message payload into a custom message type.
//
// Deprecated: formatMessagePayload exists for historical compatibility
// and should not be used. You no longer need to format a message payload
// because now Indexation payload is used.
func formatMessagePayload(message iotago.Message, messageIndex string) (Message, error) {
payloadInString, err := utils.SerializeMessagePayload(message.Payload, true)
if err != nil {
Expand All @@ -33,6 +40,11 @@ func formatMessagePayload(message iotago.Message, messageIndex string) (Message,
if strings.Contains(payloadInString, "/") {
payloadTemp := strings.Split(payloadInString, "/")

index = payloadTemp[0]
content = payloadTemp[1]
} else if strings.Contains(payloadInString, "|") {
payloadTemp := strings.Split(payloadInString, "|")

index = payloadTemp[0]
content = payloadTemp[1]
} else if strings.Contains(payloadInString, "\v") {
Expand Down Expand Up @@ -70,6 +82,10 @@ func formatMessagePayload(message iotago.Message, messageIndex string) (Message,
}

// Formats the message payload into a custom message type.
//
// Deprecated: formatMessagePayloadWithoutIndex exists for historical
// compatibility and should not be used. You no longer need to format a message
// payload because now Indexation payload is used.
func formatMessagePayloadWithoutIndex(message *iotago.Message) (Message, error) {
payloadInString, err := utils.SerializeMessagePayload(message.Payload, true)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions utils/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (

// Serializes a given message payload using iota.go package. You can turn
// on/off the debug messages.
//
// Deprecated: SerializeMessagePayload exists for historical compatibility
// and should not be used. You no longer need to serialize a message payload
// because now Indexation payload is used.
func SerializeMessagePayload(messagePayload serializer.Serializable, debugMode bool) (string, error) {
messagePayloadSerialized, err := messagePayload.Serialize(serializer.DeSeriModePerformLexicalOrdering)
if err != nil {
Expand All @@ -28,6 +32,10 @@ func SerializeMessagePayload(messagePayload serializer.Serializable, debugMode b

// Serializes a given message using iota.go package. You can turn on/off the
// debug messages.
//
// Deprecated: SerializeMessage exists for historical compatibility
// and should not be used. You no longer need to serialize a message payload
// because now Indexation payload is used.
func SerializeMessage(message iotago.Message, debugMode bool) (string, error) {
messageSerialized, err := message.Serialize(serializer.DeSeriModePerformLexicalOrdering)
if err != nil {
Expand Down

0 comments on commit 3ff7a59

Please sign in to comment.