Skip to content

Commit

Permalink
Added support for hiding params in specific actions
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Nov 26, 2024
1 parent a256618 commit 8736ebd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
11 changes: 9 additions & 2 deletions internal/app/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ type Action struct {
LightTheme string
DarkTheme string
containerProxyUrl string
hidden map[string]bool // params which are not shown in the UI
}

// NewAction creates a new action
func NewAction(logger *types.Logger, sourceFS *appfs.SourceFs, isDev bool, name, description, apath string, run, suggest starlark.Callable,
params []apptype.AppParam, paramValuesStr map[string]string, paramDict starlark.StringDict,
appPath string, styleType types.StyleType, containerProxyUrl string) (*Action, error) {
appPath string, styleType types.StyleType, containerProxyUrl string, hidden []string) (*Action, error) {

funcMap := system.GetFuncMap()

Expand Down Expand Up @@ -97,6 +98,11 @@ func NewAction(logger *types.Logger, sourceFS *appfs.SourceFs, isDev bool, name,
pagePath = ""
}

hiddenParams := make(map[string]bool)
for _, h := range hidden {
hiddenParams[h] = true
}

return &Action{
Logger: &appLogger,
isDev: isDev,
Expand All @@ -112,6 +118,7 @@ func NewAction(logger *types.Logger, sourceFS *appfs.SourceFs, isDev bool, name,
actionTemplate: tmpl,
StyleType: styleType,
containerProxyUrl: containerProxyUrl,
hidden: hiddenParams,
// AppTemplate and Theme names are initialized later
}, nil
}
Expand Down Expand Up @@ -551,7 +558,7 @@ func (a *Action) getForm(w http.ResponseWriter, r *http.Request) {
}

for _, p := range a.params {
if strings.HasPrefix(p.Name, OPTIONS_PREFIX) {
if strings.HasPrefix(p.Name, OPTIONS_PREFIX) || a.hidden[p.Name] {
continue
}

Expand Down
8 changes: 7 additions & 1 deletion internal/app/apptype/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,22 @@ func createLibraryBuiltin(_ *starlark.Thread, _ *starlark.Builtin, args starlark
func createActionBuiltin(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name, desc, path starlark.String
var suggest, executor starlark.Callable
var hidden *starlark.List
if err := starlark.UnpackArgs(ACTION, args, kwargs, "name", &name, "path", &path,
"run", &executor, "suggest?", &suggest, "description?", &desc); err != nil {
"run", &executor, "suggest?", &suggest, "description?", &desc, "hidden?", &hidden); err != nil {
return nil, fmt.Errorf("error unpacking action args: %w", err)
}

if hidden == nil {
hidden = starlark.NewList([]starlark.Value{})
}

fields := starlark.StringDict{
"name": name,
"description": desc,
"path": path,
"run": executor,
"hidden": hidden,
}

if suggest != nil {
Expand Down
7 changes: 5 additions & 2 deletions internal/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ func (a *App) addAction(count int, val starlark.Value, router *chi.Mux) error {

var name, path, description string
var run, suggest starlark.Callable
var hidden []string
var err error
if name, err = apptype.GetStringAttr(actionDef, "name"); err != nil {
return err
Expand All @@ -522,7 +523,9 @@ func (a *App) addAction(count int, val starlark.Value, router *chi.Mux) error {
if run, err = apptype.GetCallableAttr(actionDef, "run"); err != nil {
return err
}

if hidden, err = apptype.GetListStringAttr(actionDef, "hidden", true); err != nil {
return err
}
sa, _ := actionDef.Attr("suggest")
if sa != nil {
if suggest, err = apptype.GetCallableAttr(actionDef, "suggest"); err != nil {
Expand All @@ -539,7 +542,7 @@ func (a *App) addAction(count int, val starlark.Value, router *chi.Mux) error {
}
action, err := action.NewAction(a.Logger, a.sourceFS, a.IsDev, name, description, path, run, suggest,
slices.Collect(maps.Values(a.paramInfo)), a.paramValuesStr, a.paramDict, a.Path, a.appStyle.GetStyleType(),
containerProxyUrl)
containerProxyUrl, hidden)
if err != nil {
return fmt.Errorf("error creating action %s: %w", name, err)
}
Expand Down
8 changes: 6 additions & 2 deletions internal/app/tests/appaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,12 @@ def handler(dry_run, args):
return ace.result(status="done", values=["a", "b"], param_errors={"param1": "param1error", "param3": "param3error"})
app = ace.app("testApp",
actions=[ace.action("testAction", "/", handler)])
actions=[ace.action("testAction", "/", handler, hidden=["param2"])])
`,
"params.star": `param("param1", description="param1 description", type=STRING, default="myvalue")
param("options-param1", description="param1 options", type=LIST, default=["a", "b", "c"])`,
param("options-param1", description="param1 options", type=LIST, default=["a", "b", "c"])
param("param2", description="param2 description", type=STRING, default="myvalue2")`,
}
a, _, err := CreateTestApp(logger, fileData)
if err != nil {
Expand All @@ -610,6 +611,9 @@ param("options-param1", description="param1 options", type=LIST, default=["a", "
if strings.Contains(bodyStripped, `options-param1`) {
t.Errorf("options-param1 should not be in the body")
}
if strings.Contains(bodyStripped, `param2`) {
t.Errorf("hidden param2 should not be in the body")
}
}

func TestActionError(t *testing.T) {
Expand Down

0 comments on commit 8736ebd

Please sign in to comment.