A question refers to the comment ! And if not Ooopsy
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
// NATS Method Enum defines the different methods that can be used to interact with the NATS server
|
||||
type NATSMethod int
|
||||
|
||||
const (
|
||||
@@ -14,6 +15,7 @@ const (
|
||||
CREATE
|
||||
)
|
||||
|
||||
// NameToMethod returns the NATSMethod enum value from a string
|
||||
func NameToMethod(name string) NATSMethod {
|
||||
for _, v := range [...]NATSMethod{REMOVE, CREATE} {
|
||||
if strings.Contains(strings.ToLower(v.String()), strings.ToLower(name)) {
|
||||
@@ -23,20 +25,24 @@ func NameToMethod(name string) NATSMethod {
|
||||
return -1
|
||||
}
|
||||
|
||||
// GenerateKey generates a key for the NATSMethod usefull for standard key based on data name & method
|
||||
func (d NATSMethod) GenerateKey(name string) string {
|
||||
return name + "_" + d.String()
|
||||
}
|
||||
|
||||
// String returns the string of the enum
|
||||
func (d NATSMethod) String() string {
|
||||
return [...]string{"remove", "create", "discovery"}[d]
|
||||
}
|
||||
|
||||
type natsCaller struct{}
|
||||
|
||||
// NewNATSCaller creates a new instance of the NATS Caller
|
||||
func NewNATSCaller() *natsCaller {
|
||||
return &natsCaller{}
|
||||
}
|
||||
|
||||
// SetNATSPub sets a message to the NATS server
|
||||
func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interface{}) string {
|
||||
if GetConfig().NATSUrl == "" {
|
||||
return " -> NATS_SERVER is not set"
|
||||
@@ -50,9 +56,9 @@ func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interfa
|
||||
if err != nil {
|
||||
return " -> " + err.Error()
|
||||
}
|
||||
err = nc.Publish(method.GenerateKey(dataName), js)
|
||||
err = nc.Publish(method.GenerateKey(dataName), js) // Publish the message on the NATS server with a channel name based on the data name (or whatever start) and the method
|
||||
if err != nil {
|
||||
return " -> " + err.Error()
|
||||
return " -> " + err.Error() // Return an error if the message could not be published
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user