Skip to content

Commit

Permalink
Add ID field for sounds (#1073)
Browse files Browse the repository at this point in the history
Both the path and the contents of a notification sound could conceivably
change without the sound being semantically different.

For example, `ding.mp3` could get renamed to `ding1.mp3`. Or a developer
may re-encode `ding.mp3` to make it sound better.

In both cases, we need some sort of ID to keep a handle on the sound and
know what to play.
  • Loading branch information
rohansingh committed May 14, 2024
1 parent a703555 commit 86c4032
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions schema/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ load("sound.mp3", "file")
sounds = [
schema.Sound(
id = "ding",
title = "Ding!",
file = file,
),
Expand All @@ -35,6 +36,7 @@ assert.eq(s.name, "New message")
assert.eq(s.desc, "A new message has arrived")
assert.eq(s.icon, "message")
assert.eq(s.sounds[0].id, "ding")
assert.eq(s.sounds[0].title, "Ding!")
assert.eq(s.sounds[0].file, file)
Expand Down
1 change: 1 addition & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type SchemaOption struct {

// SchemaSound represents a sound that can be played by the applet.
type SchemaSound struct {
ID string `json:"id" validate:"required"`
Title string `json:"title" validate:"required"`
Path string `json:"path" validate:"required"`
}
Expand Down
3 changes: 3 additions & 0 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def get_schema():
icon = "notification",
sounds = [
schema.Sound(
id = "ding",
title = "Ding!",
file = ding,
),
Expand Down Expand Up @@ -162,6 +163,7 @@ def main():
Icon: "notification",
Sounds: []schema.SchemaSound{
{
ID: "ding",
Title: "Ding!",
Path: "ding.mp3",
},
Expand Down Expand Up @@ -307,6 +309,7 @@ def get_schema():
icon = "notification",
sounds = [
schema.Sound(
id = "ding",
title = "Ding!",
file = ding,
),
Expand Down
7 changes: 6 additions & 1 deletion schema/sound.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ func newSound(
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
id starlark.String
title starlark.String
file *file.File
)

if err := starlark.UnpackArgs(
"Sound",
args, kwargs,
"id", &id,
"title", &title,
"file", &file,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for Sound: %s", err)
}

s := &Sound{file: file}
s.ID = id.GoString()
s.Title = title.GoString()
s.Path = file.Path

Expand All @@ -46,11 +49,13 @@ func (s *Sound) AsSchemaSound() SchemaSound {
}

func (s *Sound) AttrNames() []string {
return []string{"title", "file"}
return []string{"id", "title", "file"}
}

func (s *Sound) Attr(name string) (starlark.Value, error) {
switch name {
case "id":
return starlark.String(s.ID), nil

case "title":
return starlark.String(s.Title), nil
Expand Down
2 changes: 2 additions & 0 deletions schema/sound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ def assert(success, message=None):
fail(message or "assertion failed")
s = schema.Sound(
id = "sound1",
title = "Sneezing Elephant",
file = file,
)
assert(s.id == "sound1")
assert(s.title == "Sneezing Elephant")
assert(s.file == file)
assert(s.file.readall() == "sound data")
Expand Down

0 comments on commit 86c4032

Please sign in to comment.