From cf7586a7dc36bcf3746cdcc4ccddaafbe31da6fd Mon Sep 17 00:00:00 2001 From: Alexandre HEIM Date: Wed, 23 Jul 2025 10:56:06 +0200 Subject: [PATCH] add VariableSet, VariableUnset --- model/variable.go | 11 +++++++++++ plugis.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- plugisservice.go | 2 ++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 model/variable.go diff --git a/model/variable.go b/model/variable.go new file mode 100644 index 0000000..2fc5e0b --- /dev/null +++ b/model/variable.go @@ -0,0 +1,11 @@ +package model + +import "time" + +type Variable struct { + Name string `json:"name"` + VarType string `json:"type,omitempty"` + Value any `json:"value,omitempty"` + Created *time.Time `json:"created,omitempty"` + Updated *time.Time `json:"updated,omitempty"` +} diff --git a/plugis.go b/plugis.go index 96df961..409009c 100644 --- a/plugis.go +++ b/plugis.go @@ -4,14 +4,16 @@ import ( "context" "encoding/json" "errors" - "github.com/nats-io/nats.go/micro" - nats_service "github.com/telemac/plugisservice/pkg/nats-service" "iter" "log/slog" "os" "runtime" "time" + "github.com/nats-io/nats.go/micro" + "github.com/telemac/plugisservice/model" + nats_service "github.com/telemac/plugisservice/pkg/nats-service" + "github.com/telemac/goutils/net" "github.com/telemac/goutils/task" @@ -35,6 +37,11 @@ var ( ErrNatsConnectionNil = errors.New("nats connection is nil") ) +type Event[T any] struct { + Type string `json:"type,omitempty"` + Data T `json:"data,omitempty"` +} + // SetLogger sets the logger for the Plugis instance. func (plugis *Plugis) SetLogger(log *slog.Logger) { plugis.logger = log @@ -245,3 +252,39 @@ func (plugis *Plugis) StartService(svc PlugisServiceIntf) (*nats_service.NatsSer }) return service, err } + +// VariableSet sets a variable with the given name, value, and type, then publishes it to a corresponding topic. +func (plugis *Plugis) VariableSet(name string, value any, varType string) error { + variable := model.Variable{ + Name: name, + Value: value, + VarType: varType, + } + event := Event[model.Variable]{ + Type: "variable.set", + Data: variable, + } + topic := "variable.set." + name + payload, err := json.Marshal(event) + if err != nil { + return err + } + return plugis.Publish(topic, payload) +} + +// VariableUnset unsets a variable with the given name, then publishes it to a corresponding topic. +func (plugis *Plugis) VariableUnset(name string) error { + variable := model.Variable{ + Name: name, + } + event := Event[model.Variable]{ + Type: "variable.unset", + Data: variable, + } + topic := "variable.unset." + name + payload, err := json.Marshal(event) + if err != nil { + return err + } + return plugis.Publish(topic, payload) +} diff --git a/plugisservice.go b/plugisservice.go index b1fe329..494ad1c 100644 --- a/plugisservice.go +++ b/plugisservice.go @@ -37,6 +37,8 @@ type PlugisIntf interface { RequestMany(ctx context.Context, subject string, data []byte, opts ...natsext.RequestManyOpt) (iter.Seq2[*nats.Msg, error], error) GetServices(ctx context.Context) ([]ServiceInfo, error) StartService(svc PlugisServiceIntf) (*nats_service.NatsService, error) + VariableSet(name string, value any, varType string) error + VariableUnset(name string) error } // ServiceInfo is the information about a service.