CSI-ES-2324/Projs/PD1/internal/client/datastore.go

48 lines
1 KiB
Go
Raw Normal View History

package client
2024-04-19 23:59:26 +01:00
import (
2024-04-28 22:02:13 +01:00
"encoding/json"
2024-04-19 23:59:26 +01:00
"time"
)
2024-04-19 23:59:26 +01:00
type ClientMessage struct {
FromUID string
ToUID string
Subject string
Body string
Timestamp time.Time
}
2024-04-19 23:59:26 +01:00
type ClientMessageInfo struct {
2024-04-28 22:02:13 +01:00
Num int
FromUID string
Timestamp time.Time
Subject string
decryptError error
2024-04-19 23:59:26 +01:00
}
func newClientMessage(fromUID string, toUID string, subject string, body string, timestamp time.Time) ClientMessage {
return ClientMessage{FromUID: fromUID, ToUID: toUID, Subject: subject, Body: body, Timestamp: timestamp}
}
2024-04-28 22:02:13 +01:00
func newClientMessageInfo(num int, fromUID string, subject string, timestamp time.Time, err error) ClientMessageInfo {
return ClientMessageInfo{Num: num, FromUID: fromUID, Subject: subject, Timestamp: timestamp, decryptError: err}
2024-04-19 23:59:26 +01:00
}
2024-04-28 22:02:13 +01:00
func Marshal(data any) ([]byte, error) {
2024-04-19 23:59:26 +01:00
subject, err := json.Marshal(data)
if err != nil {
2024-04-28 22:02:13 +01:00
return nil, err
2024-04-19 23:59:26 +01:00
}
2024-04-28 22:02:13 +01:00
return subject, nil
2024-04-19 23:59:26 +01:00
}
2024-04-28 22:02:13 +01:00
func Unmarshal(data []byte) (string, error) {
2024-04-19 23:59:26 +01:00
var c string
err := json.Unmarshal(data, &c)
if err != nil {
2024-04-28 22:02:13 +01:00
return "", err
2024-04-19 23:59:26 +01:00
}
2024-04-28 22:02:13 +01:00
return c, nil
}