From 3db9368a99db8572ec5fc046c28b54101edd178c Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Mon, 12 Aug 2019 19:51:37 +0200 Subject: [PATCH] Add ability to pass custom struct as module & record to script runner --- api/compose/spec.json | 4 ++-- api/compose/spec/automation_script.json | 4 ++-- compose/rest/automation_script.go | 25 +++++++++++++++++------ compose/rest/request/automation_script.go | 8 ++++---- docs/compose/README.md | 4 ++-- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/api/compose/spec.json b/api/compose/spec.json index 799e78934..4dcce078c 100644 --- a/api/compose/spec.json +++ b/api/compose/spec.json @@ -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"} ] } } diff --git a/api/compose/spec/automation_script.json b/api/compose/spec/automation_script.json index e804b3292..39cba81e7 100644 --- a/api/compose/spec/automation_script.json +++ b/api/compose/spec/automation_script.json @@ -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" } ] } diff --git a/compose/rest/automation_script.go b/compose/rest/automation_script.go index 336011381..8ffd8c62d 100644 --- a/compose/rest/automation_script.go +++ b/compose/rest/automation_script.go @@ -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 } diff --git a/compose/rest/request/automation_script.go b/compose/rest/request/automation_script.go index 0e1375500..9e5f889bc 100644 --- a/compose/rest/request/automation_script.go +++ b/compose/rest/request/automation_script.go @@ -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")) diff --git a/docs/compose/README.md b/docs/compose/README.md index fe9c19dcb..a481b1e5e 100644 --- a/docs/compose/README.md +++ b/docs/compose/README.md @@ -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 | ---