3
0

Add ability to pass custom struct as module & record to script runner

This commit is contained in:
Denis Arh
2019-08-12 19:51:37 +02:00
parent ffdeef1da2
commit 3db9368a99
5 changed files with 29 additions and 16 deletions
+2 -2
View File
@@ -1508,8 +1508,8 @@
{"name": "source", "type": "string", "title": "Script's source code (overrides scriptID parameter)"},
{"name": "moduleID", "type": "uint64", "title": "Preload module and pass it to the automation script"},
{"name": "recordID", "type": "uint64", "title": "Preload record and pass it to the automation script"},
{"name": "module", "type": "interface{}", "title": "Module to pass to the automation script"},
{"name": "record", "type": "interface{}", "title": "Record to pass to the automation script"}
{"name": "module", "type": "json.RawMessage", "title": "Module to pass to the automation script"},
{"name": "record", "type": "json.RawMessage", "title": "Record to pass to the automation script"}
]
}
}
+2 -2
View File
@@ -271,12 +271,12 @@
{
"name": "module",
"title": "Module to pass to the automation script",
"type": "interface{}"
"type": "json.RawMessage"
},
{
"name": "record",
"title": "Record to pass to the automation script",
"type": "interface{}"
"type": "json.RawMessage"
}
]
}
+19 -6
View File
@@ -2,6 +2,7 @@ package rest
import (
"context"
"encoding/json"
"github.com/pkg/errors"
"github.com/titpetric/factory/resputil"
@@ -218,21 +219,33 @@ func (ctrl AutomationScript) Runnable(ctx context.Context, r *request.Automation
func (ctrl AutomationScript) Run(ctx context.Context, r *request.AutomationScriptRun) (interface{}, error) {
var (
err error
ns *types.Namespace
module *types.Module
record *types.Record
err error
ns *types.Namespace
module = &types.Module{}
record = &types.Record{}
)
// Load requested namespace
if ns, err = ctrl.namespace.FindByID(r.NamespaceID); err != nil {
return nil, err
}
if module, err = ctrl.module.FindByID(ns.ID, r.ModuleID); err != nil {
// Unmarshal given module or find existing one from ID
if r.Module != nil {
if err = json.Unmarshal(r.Module, &module); err != nil {
return nil, err
}
} else if module, err = ctrl.module.FindByID(ns.ID, r.ModuleID); err != nil {
return nil, err
}
if record, err = ctrl.record.FindByID(ns.ID, r.RecordID); err != nil {
// Unmarshal given record or find existing one from ID
if r.Record != nil {
if err = json.Unmarshal(r.Record, &record); err != nil {
return nil, err
}
} else if record, err = ctrl.record.FindByID(ns.ID, r.RecordID); err != nil {
return nil, err
}
+4 -4
View File
@@ -482,8 +482,8 @@ type AutomationScriptRun struct {
Source string
ModuleID uint64 `json:",string"`
RecordID uint64 `json:",string"`
Module interface{}
Record interface{}
Module json.RawMessage
Record json.RawMessage
NamespaceID uint64 `json:",string"`
}
@@ -545,10 +545,10 @@ func (r *AutomationScriptRun) Fill(req *http.Request) (err error) {
r.RecordID = parseUInt64(val)
}
if val, ok := post["module"]; ok {
r.Module = interface{}(val)
r.Module = json.RawMessage(val)
}
if val, ok := post["record"]; ok {
r.Record = interface{}(val)
r.Record = json.RawMessage(val)
}
r.NamespaceID = parseUInt64(chi.URLParam(req, "namespaceID"))
+2 -2
View File
@@ -253,8 +253,8 @@
| source | string | POST | Script's source code (overrides scriptID parameter) | N/A | NO |
| moduleID | uint64 | POST | Preload module and pass it to the automation script | N/A | NO |
| recordID | uint64 | POST | Preload record and pass it to the automation script | N/A | NO |
| module | interface{} | POST | Module to pass to the automation script | N/A | NO |
| record | interface{} | POST | Record to pass to the automation script | N/A | NO |
| module | json.RawMessage | POST | Module to pass to the automation script | N/A | NO |
| record | json.RawMessage | POST | Record to pass to the automation script | N/A | NO |
| namespaceID | uint64 | PATH | Namespace ID | N/A | YES |
---