natsio/utils.go

54 lines
1.3 KiB
Go
Raw Normal View History

2021-10-08 05:00:03 +00:00
package natsio
2021-10-08 07:41:58 +00:00
import (
2022-12-04 16:11:51 +00:00
"encoding/json"
2021-10-08 07:41:58 +00:00
"fmt"
"strings"
2021-10-08 07:41:58 +00:00
"github.com/thoas/go-funk"
)
2021-10-08 05:00:03 +00:00
// mergeAndUniqueArrayStrings ...
func mergeAndUniqueArrayStrings(arr1, arr2 []string) []string {
var result = make([]string, 0)
result = append(result, arr1...)
result = append(result, arr2...)
result = funk.UniqString(result)
return result
}
2021-10-08 07:41:58 +00:00
func combineStreamAndSubjectName(stream, subject string) string {
return fmt.Sprintf("%s.%s", stream, subject)
}
2022-12-04 15:21:30 +00:00
// GenerateJetStreamSubject ...
2022-12-16 03:47:20 +00:00
func GenerateJetStreamSubject(stream, server, subject string) string {
return fmt.Sprintf("%s.jetstream.%s.%s", stream, server, subject)
}
// GenerateReqrepSubject ...
2022-12-16 03:47:20 +00:00
func GenerateReqrepSubject(stream, server, subject string) string {
return fmt.Sprintf("%s.reqrep.%s.%s", stream, server, subject)
2022-12-04 15:21:30 +00:00
}
2022-12-04 16:11:51 +00:00
func GenerateQueueNameFromSubject(subject string) string {
return strings.ReplaceAll(subject, ".", "_")
}
// InterfaceToBytes ...
func InterfaceToBytes(data interface{}) []byte {
2022-12-04 16:11:51 +00:00
b, err := json.Marshal(data)
if err != nil {
fmt.Printf("[natsio.InterfaceToBytes] error: %v with data: %v\n", err, data)
2022-12-04 16:11:51 +00:00
}
return b
}
func BytesToInterface(b []byte, dest interface{}) error {
err := json.Unmarshal(b, dest)
if err != nil {
fmt.Printf("[natsio.BytesToInterface] error: %v with data: %s\n", err, string(b))
}
return err
}