diff --git a/internal/app/plugin.go b/internal/app/plugin.go index b3acf8f..337184c 100644 --- a/internal/app/plugin.go +++ b/internal/app/plugin.go @@ -19,6 +19,14 @@ import ( "go.starlark.net/starlarkstruct" ) +type PluginFunctionType int + +const ( + READ PluginFunctionType = iota + WRITE + READ_WRITE +) + var ( loaderInitMutex sync.Mutex builtInPlugins map[string]utils.PluginMap @@ -53,7 +61,7 @@ func RegisterPlugin(name string, builder utils.NewPluginFunc, funcs []utils.Plug func CreatePluginApi( f func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error), - isRead bool, + opType PluginFunctionType, ) utils.PluginFunc { funcVal := runtime.FuncForPC(reflect.ValueOf(f).Pointer()) @@ -65,13 +73,13 @@ func CreatePluginApi( nameParts := strings.Split(parts[len(parts)-1], ".") funcName := strings.TrimSuffix(nameParts[len(nameParts)-1], "-fm") // -fm denotes function value - return CreatePluginApiName(f, isRead, strings.ToLower(funcName)) + return CreatePluginApiName(f, opType, strings.ToLower(funcName)) } // CreatePluginApiName creates a Clace plugin function func CreatePluginApiName( f func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error), - isRead bool, + opType PluginFunctionType, name string) utils.PluginFunc { funcVal := runtime.FuncForPC(reflect.ValueOf(f).Pointer()) if funcVal == nil { @@ -92,7 +100,7 @@ func CreatePluginApiName( return utils.PluginFunc{ Name: name, - IsRead: isRead, + IsRead: opType == READ, FunctionName: funcName, } } diff --git a/internal/app/store/store_plugin.go b/internal/app/store/store_plugin.go index db1d6ac..270278f 100644 --- a/internal/app/store/store_plugin.go +++ b/internal/app/store/store_plugin.go @@ -14,10 +14,10 @@ import ( func init() { h := &storePlugin{} pluginFuncs := []utils.PluginFunc{ - app.CreatePluginApi(h.Insert, false), - app.CreatePluginApiName(h.SelectById, true, "select_by_id"), - app.CreatePluginApi(h.Update, false), - app.CreatePluginApiName(h.DeleteById, false, "delete_by_id"), + app.CreatePluginApi(h.Insert, app.WRITE), + app.CreatePluginApiName(h.SelectById, app.READ, "select_by_id"), + app.CreatePluginApi(h.Update, app.WRITE), + app.CreatePluginApiName(h.DeleteById, app.WRITE, "delete_by_id"), } app.RegisterPlugin("store", NewStorePlugin, pluginFuncs) } diff --git a/plugins/exec.go b/plugins/exec.go index c132411..4490469 100644 --- a/plugins/exec.go +++ b/plugins/exec.go @@ -21,7 +21,7 @@ const MAX_BYTES_STDOUT = 100 * 1024 * 1024 // 100MB func init() { e := &ExecPlugin{} app.RegisterPlugin("exec", NewExecPlugin, []utils.PluginFunc{ - app.CreatePluginApi(e.Run, false), + app.CreatePluginApi(e.Run, app.READ_WRITE), }) } diff --git a/plugins/http.go b/plugins/http.go index d2c56ca..d9dff8b 100644 --- a/plugins/http.go +++ b/plugins/http.go @@ -38,13 +38,13 @@ const ( func init() { h := &httpPlugin{} pluginFuncs := []utils.PluginFunc{ - app.CreatePluginApi(h.Get, true), - app.CreatePluginApi(h.Head, true), - app.CreatePluginApi(h.Options, true), - app.CreatePluginApi(h.Post, false), - app.CreatePluginApi(h.Put, false), - app.CreatePluginApi(h.Delete, false), - app.CreatePluginApi(h.Patch, false), + app.CreatePluginApi(h.Get, app.READ), + app.CreatePluginApi(h.Head, app.READ), + app.CreatePluginApi(h.Options, app.READ), + app.CreatePluginApi(h.Post, app.WRITE), + app.CreatePluginApi(h.Put, app.WRITE), + app.CreatePluginApi(h.Delete, app.WRITE), + app.CreatePluginApi(h.Patch, app.WRITE), } app.RegisterPlugin("http", NewHttpPlugin, pluginFuncs) }