Skip to content

Commit

Permalink
Remove JSON opinion from babashka package (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds authored Dec 30, 2020
1 parent a506f69 commit ab490dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
15 changes: 3 additions & 12 deletions babashka/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package babashka

import (
"bufio"
"encoding/json"
"os"

"github.com/jackpal/bencode-go"
Expand Down Expand Up @@ -57,18 +56,10 @@ func WriteDescribeResponse(describeResponse *DescribeResponse) {
writeResponse(*describeResponse)
}

func WriteInvokeResponse(inputMessage *Message, value interface{}) error {
if value == nil {
return nil
}
resultValue, err := json.Marshal(value)
if err != nil {
return err
}
response := InvokeResponse{Id: inputMessage.Id, Status: []string{"done"}, Value: string(resultValue)}
writeResponse(response)
func WriteInvokeResponse(inputMessage *Message, value string) error {
response := InvokeResponse{Id: inputMessage.Id, Status: []string{"done"}, Value: value}

return nil
return writeResponse(response)
}

func WriteErrorResponse(inputMessage *Message, err error) {
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"encoding/json"

"github.com/babashka/pod-babashka-sqlite3/babashka"
"github.com/babashka/pod-babashka-sqlite3/pod"
)
Expand All @@ -24,6 +26,11 @@ func main() {
babashka.WriteDescribeResponse(describeRes)
continue
}
babashka.WriteInvokeResponse(message, res)

if json, err := json.Marshal(res); err != nil {
babashka.WriteErrorResponse(message, err)
} else {
babashka.WriteInvokeResponse(message, string(json))
}
}
}

0 comments on commit ab490dd

Please sign in to comment.