take reference to module for future process calls

This commit is contained in:
Joel Wetzell
2026-05-14 22:35:31 -05:00
parent d16e247a3a
commit ac2930b91b
3 changed files with 58 additions and 50 deletions
+5 -2
View File
@@ -17,9 +17,11 @@ type DbQuery struct {
ModuleId string
Query *template.Template
logger *slog.Logger
module common.DatabaseModule
}
func (dq *DbQuery) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
if dq.module == nil {
if wrappedPayload.Modules == nil {
wrappedPayload.End = true
return wrappedPayload, errors.New("db.query wrapped payload has no modules")
@@ -36,9 +38,10 @@ func (dq *DbQuery) Process(ctx context.Context, wrappedPayload common.WrappedPay
wrappedPayload.End = true
return wrappedPayload, fmt.Errorf("db.query module with id %s is not a DatabaseModule", dq.ModuleId)
}
// TODO(jwetzell): cache the module reference after the first run
dq.module = dbModule
}
db := dbModule.Database()
db := dq.module.Database()
if db == nil {
wrappedPayload.End = true
return wrappedPayload, fmt.Errorf("db.query module with id %s returned nil database", dq.ModuleId)
+5 -2
View File
@@ -16,9 +16,11 @@ type KVGet struct {
ModuleId string
Key string
logger *slog.Logger
module common.KeyValueModule
}
func (kvg *KVGet) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
if kvg.module == nil {
if wrappedPayload.Modules == nil {
wrappedPayload.End = true
return wrappedPayload, errors.New("kv.get wrapped payload has no modules")
@@ -35,9 +37,10 @@ func (kvg *KVGet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
wrappedPayload.End = true
return wrappedPayload, fmt.Errorf("kv.get module with id %s is not a KeyValueModule", kvg.ModuleId)
}
// TODO(jwetzell): cache the module reference after the first run
kvg.module = kvModule
}
value, err := kvModule.Get(kvg.Key)
value, err := kvg.module.Get(kvg.Key)
if err != nil {
wrappedPayload.End = true
return wrappedPayload, fmt.Errorf("kv.get error getting key: %w", err)
+5 -3
View File
@@ -19,10 +19,11 @@ type KVSet struct {
Key string
Value *template.Template
logger *slog.Logger
module common.KeyValueModule
}
func (kvs *KVSet) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
if kvs.module == nil {
if wrappedPayload.Modules == nil {
wrappedPayload.End = true
return wrappedPayload, errors.New("kv.set wrapped payload has no modules")
@@ -39,7 +40,8 @@ func (kvs *KVSet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
wrappedPayload.End = true
return wrappedPayload, fmt.Errorf("kv.set module with id %s is not a KeyValueModule", kvs.ModuleId)
}
// TODO(jwetzell): cache the module reference after the first run
kvs.module = kvModule
}
var valueBuffer bytes.Buffer
err := kvs.Value.Execute(&valueBuffer, wrappedPayload)
@@ -49,7 +51,7 @@ func (kvs *KVSet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
return wrappedPayload, err
}
err = kvModule.Set(kvs.Key, valueBuffer.String())
err = kvs.module.Set(kvs.Key, valueBuffer.String())
if err != nil {
wrappedPayload.End = true
return wrappedPayload, fmt.Errorf("kv.set error setting key: %w", err)