Skip to content

Commit

Permalink
Refactor the notifications schema definition
Browse files Browse the repository at this point in the history
Notifications are still a work in progress. With this change, each
notification needs to specify a `builder` function.
  • Loading branch information
rohansingh committed May 21, 2024
1 parent 02c7ebc commit fe13dc9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
51 changes: 34 additions & 17 deletions runtime/applet.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,18 @@ func (a *Applet) Run(ctx context.Context) (roots []render.Root, err error) {
return a.RunWithConfig(ctx, nil)
}

// RunWithConfig exceutes the applet's main function, passing it configuration as a
// starlark dict. It returns the render roots that are returned by the applet.
func (a *Applet) RunWithConfig(ctx context.Context, config map[string]string) (roots []render.Root, err error) {
var args starlark.Tuple
if a.mainFun.NumParams() > 0 {
starlarkConfig := AppletConfig(config)
args = starlark.Tuple{starlarkConfig}
}

returnValue, err := a.Call(ctx, a.mainFun, args...)
if err != nil {
return nil, err
}

if returnRoot, ok := returnValue.(render_runtime.Rootable); ok {
// ExtractRoots extracts render roots from a Starlark value. It expects the value
// to be either a single render root or a list of render roots.
//
// It's used internally by RunWithConfig to extract the roots returned by the applet.
func ExtractRoots(val starlark.Value) ([]render.Root, error) {
var roots []render.Root

if val == starlark.None {
// no roots returned
} else if returnRoot, ok := val.(render_runtime.Rootable); ok {
roots = []render.Root{returnRoot.AsRenderRoot()}
} else if returnList, ok := returnValue.(*starlark.List); ok {
} else if returnList, ok := val.(*starlark.List); ok {
roots = make([]render.Root, returnList.Len())
iter := returnList.Iterate()
defer iter.Done()
Expand All @@ -187,7 +182,29 @@ func (a *Applet) RunWithConfig(ctx context.Context, config map[string]string) (r
i++
}
} else {
return nil, fmt.Errorf("expected app implementation to return Root(s) but found: %s", returnValue.Type())
return nil, fmt.Errorf("expected app implementation to return Root(s) but found: %s", val.Type())
}

return roots, nil
}

// RunWithConfig exceutes the applet's main function, passing it configuration as a
// starlark dict. It returns the render roots that are returned by the applet.
func (a *Applet) RunWithConfig(ctx context.Context, config map[string]string) (roots []render.Root, err error) {
var args starlark.Tuple
if a.mainFun.NumParams() > 0 {
starlarkConfig := AppletConfig(config)
args = starlark.Tuple{starlarkConfig}
}

returnValue, err := a.Call(ctx, a.mainFun, args...)
if err != nil {
return nil, err
}

roots, err = ExtractRoots(returnValue)
if err != nil {
return nil, err
}

return roots, nil
Expand Down
2 changes: 1 addition & 1 deletion schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func newSchema(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple
)
}

s.Schema.Notifications = append(s.Schema.Notifications, n.AsSchemaField())
s.Schema.Notifications = append(s.Schema.Notifications, *n)
}
}

Expand Down
19 changes: 13 additions & 6 deletions schema/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Notification struct {
SchemaField
Builder *starlark.Function
starlarkSounds *starlark.List
}

Expand All @@ -19,11 +20,12 @@ func newNotification(
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
id starlark.String
name starlark.String
desc starlark.String
icon starlark.String
sounds *starlark.List
id starlark.String
name starlark.String
desc starlark.String
icon starlark.String
sounds *starlark.List
builder *starlark.Function
)

if err := starlark.UnpackArgs(
Expand All @@ -34,6 +36,7 @@ func newNotification(
"desc", &desc,
"icon", &icon,
"sounds", &sounds,
"builder", &builder,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for Notification: %s", err)
}
Expand All @@ -44,6 +47,7 @@ func newNotification(
s.Name = name.GoString()
s.Description = desc.GoString()
s.Icon = icon.GoString()
s.Builder = builder

var soundVal starlark.Value
soundIter := sounds.Iterate()
Expand Down Expand Up @@ -75,7 +79,7 @@ func (s *Notification) AsSchemaField() SchemaField {

func (s *Notification) AttrNames() []string {
return []string{
"id", "name", "desc", "icon", "sounds",
"id", "name", "desc", "icon", "sounds", "builder",
}
}

Expand All @@ -97,6 +101,9 @@ func (s *Notification) Attr(name string) (starlark.Value, error) {
case "sounds":
return s.starlarkSounds, nil

case "builder":
return s.Builder, nil

default:
return nil, nil
}
Expand Down
10 changes: 6 additions & 4 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const (
// Schema holds a configuration object for an applet. It holds a list of fields
// that are exported from an applet.
type Schema struct {
Version string `json:"version" validate:"required"`
Fields []SchemaField `json:"schema" validate:"dive"`
Notifications []SchemaField `json:"notifications,omitempty" validate:"dive"`
Version string `json:"version" validate:"required"`
Fields []SchemaField `json:"schema" validate:"dive"`
Notifications []Notification `json:"notifications,omitempty" validate:"dive"`

Handlers map[string]SchemaHandler `json:"-"`
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (s Schema) MarshalJSON() ([]byte, error) {
a.Fields = make([]SchemaField, 0)
}
if a.Notifications == nil {
a.Notifications = make([]SchemaField, 0)
a.Notifications = make([]Notification, 0)
}

js, err := json.Marshal(a)
Expand Down Expand Up @@ -165,6 +165,8 @@ func FromStarlark(
if schemaField.StarlarkHandler != nil {
handlerFun = schemaField.StarlarkHandler
} else if schemaField.Handler != "" {
// legacy schema, where the handler was a string instead of
// a function reference
handlerValue, ok := globals[schemaField.Handler]
if !ok {
return nil, fmt.Errorf(
Expand Down

0 comments on commit fe13dc9

Please sign in to comment.