Compare commits

...

1 commit
0.0.3 ... main

Author SHA1 Message Date
Alexandre HEIM
cf7586a7dc add VariableSet, VariableUnset 2025-07-23 10:56:06 +02:00
3 changed files with 58 additions and 2 deletions

11
model/variable.go Normal file
View file

@ -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"`
}

View file

@ -4,14 +4,16 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/nats-io/nats.go/micro"
nats_service "github.com/telemac/plugisservice/pkg/nats-service"
"iter" "iter"
"log/slog" "log/slog"
"os" "os"
"runtime" "runtime"
"time" "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/net"
"github.com/telemac/goutils/task" "github.com/telemac/goutils/task"
@ -35,6 +37,11 @@ var (
ErrNatsConnectionNil = errors.New("nats connection is nil") 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. // SetLogger sets the logger for the Plugis instance.
func (plugis *Plugis) SetLogger(log *slog.Logger) { func (plugis *Plugis) SetLogger(log *slog.Logger) {
plugis.logger = log plugis.logger = log
@ -245,3 +252,39 @@ func (plugis *Plugis) StartService(svc PlugisServiceIntf) (*nats_service.NatsSer
}) })
return service, err 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)
}

View file

@ -37,6 +37,8 @@ type PlugisIntf interface {
RequestMany(ctx context.Context, subject string, data []byte, opts ...natsext.RequestManyOpt) (iter.Seq2[*nats.Msg, error], error) RequestMany(ctx context.Context, subject string, data []byte, opts ...natsext.RequestManyOpt) (iter.Seq2[*nats.Msg, error], error)
GetServices(ctx context.Context) ([]ServiceInfo, error) GetServices(ctx context.Context) ([]ServiceInfo, error)
StartService(svc PlugisServiceIntf) (*nats_service.NatsService, 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. // ServiceInfo is the information about a service.