Skip to content

Commit 36fe2a9

Browse files
go-tui: Replace maps with idiomatic types (#211)
* go-tui: Fix SDK URL * go-tui: Fix <Space> as key to toggle a task * go-tui: Use ditto.QueryArguments * go-sdk: Use native deserialization for Tasks
1 parent 72197ba commit 36fe2a9

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

go-tui/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ else
3535
$(error Unsupported platform: $(PLATFORM))
3636
endif
3737

38-
DITTO_SDK_URL = "https://software.ditto.live/go/Ditto/$(DITTO_SDK_VERSION)/libs/libdittoffi-$(DITTO_PLATFORM).tar.gz"
38+
DITTO_SDK_URL = "https://software.ditto.live/go/Ditto/$(DITTO_SDK_VERSION)/dist/libdittoffi-$(DITTO_PLATFORM).tar.gz"
3939

4040
# Build the application
4141
.PHONY: build

go-tui/main.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func (a *App) handleNormalMode(e ui.Event) {
271271
}
272272
a.render()
273273

274-
case "<Enter>", " ":
274+
case "<Enter>", "<Space>":
275275
if task, ok := a.getSelectedTask(); ok {
276276
go a.toggleTask(task.ID, !task.Done)
277277
}
@@ -436,7 +436,7 @@ func (a *App) createTask(title string) {
436436

437437
result, err := a.ditto.Store().Execute(
438438
"INSERT INTO tasks VALUES (:task)",
439-
map[string]interface{}{"task": task},
439+
ditto.QueryArguments{"task": task},
440440
)
441441
if err != nil {
442442
a.setError(err.Error())
@@ -448,7 +448,7 @@ func (a *App) createTask(title string) {
448448
func (a *App) updateTask(id, title string) {
449449
result, err := a.ditto.Store().Execute(
450450
"UPDATE tasks SET title = :title WHERE _id = :id",
451-
map[string]interface{}{
451+
ditto.QueryArguments{
452452
"title": title,
453453
"id": id,
454454
},
@@ -463,7 +463,7 @@ func (a *App) updateTask(id, title string) {
463463
func (a *App) toggleTask(id string, done bool) {
464464
result, err := a.ditto.Store().Execute(
465465
"UPDATE tasks SET done = :done WHERE _id = :id",
466-
map[string]interface{}{
466+
ditto.QueryArguments{
467467
"done": done,
468468
"id": id,
469469
},
@@ -478,7 +478,7 @@ func (a *App) toggleTask(id string, done bool) {
478478
func (a *App) deleteTask(id string) {
479479
result, err := a.ditto.Store().Execute(
480480
"UPDATE tasks SET deleted = true WHERE _id = :id",
481-
map[string]interface{}{"id": id},
481+
ditto.QueryArguments{"id": id},
482482
)
483483
if err != nil {
484484
a.setError(err.Error())
@@ -514,23 +514,13 @@ func parseTasks(result *ditto.QueryResult) []Task {
514514
return []Task{}
515515
}
516516

517-
// Don't pre-allocate when we're filtering
518-
var tasks []Task
519-
items := result.Items()
520-
for _, queryItem := range items {
521-
// Get the value as a map
522-
item := queryItem.Value()
523-
524-
// Parse the task from the document
525-
task := Task{
526-
ID: getStringValue(item, "_id"),
527-
Title: getStringValue(item, "title"),
528-
Done: getBoolValue(item, "done"),
529-
Deleted: getBoolValue(item, "deleted"),
530-
}
531-
if !task.Deleted {
532-
tasks = append(tasks, task)
517+
tasks := make([]Task, 0, result.ItemCount())
518+
for _, queryItem := range result.Items() {
519+
var task Task
520+
if err := queryItem.UnmarshalTo(&task); err != nil {
521+
panic(err)
533522
}
523+
tasks = append(tasks, task)
534524
}
535525
return tasks
536526
}

0 commit comments

Comments
 (0)