Skip to content

Commit

Permalink
Changed isRead to be enum
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Jan 21, 2024
1 parent b807d0f commit f8c47a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
16 changes: 12 additions & 4 deletions internal/app/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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 {
Expand All @@ -92,7 +100,7 @@ func CreatePluginApiName(

return utils.PluginFunc{
Name: name,
IsRead: isRead,
IsRead: opType == READ,
FunctionName: funcName,
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/app/store/store_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}

Expand Down
14 changes: 7 additions & 7 deletions plugins/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit f8c47a1

Please sign in to comment.