Skip to content

Commit 3c3a5de

Browse files
author
Evan Lin
authored
Merge pull request #7 from kkdai/fix_json_string
refactor: refactor callbackHandler function to remove first and last …
2 parents 36a5704 + e8dd4b4 commit 3c3a5de

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

bot.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
8282

8383
log.Println("Got GeminiImage ret:", ret)
8484

85-
jsonData := extractJSONFromString(ret)
85+
// Remove first and last line, which are the backticks.
86+
jsonData := removeFirstAndLastLine(ret)
8687
log.Println("Got jsonData:", jsonData)
87-
// Parse json and insert NotionDB
8888

89+
// Parse json and insert NotionDB
8990
var person Person
9091
err = json.Unmarshal([]byte(jsonData), &person)
9192
if err != nil {
@@ -186,3 +187,17 @@ func extractJSONFromString(s string) string {
186187

187188
return s
188189
}
190+
191+
// removeFirstAndLastLine takes a string and removes the first and last lines.
192+
func removeFirstAndLastLine(s string) string {
193+
// Split the string into lines.
194+
lines := strings.Split(s, "\n")
195+
196+
// If there are less than 3 lines, return an empty string because removing the first and last would leave nothing.
197+
if len(lines) < 3 {
198+
return ""
199+
}
200+
201+
// Join the lines back together, skipping the first and last lines.
202+
return strings.Join(lines[1:len(lines)-1], "\n")
203+
}

0 commit comments

Comments
 (0)