fix multiline handler thing for when we're don't have any stdin.

This commit is contained in:
fiatjaf
2025-02-05 20:42:55 -03:00
parent d00976a669
commit 1f2492c9b1

View File

@@ -49,7 +49,7 @@ func getJsonsOrBlank() iter.Seq[string] {
var curr strings.Builder var curr strings.Builder
return func(yield func(string) bool) { return func(yield func(string) bool) {
for stdinLine := range getStdinLinesOrBlank() { hasStdin := writeStdinLinesOrNothing(func(stdinLine string) bool {
// we're look for an event, but it may be in multiple lines, so if json parsing fails // we're look for an event, but it may be in multiple lines, so if json parsing fails
// we'll try the next line until we're successful // we'll try the next line until we're successful
curr.WriteString(stdinLine) curr.WriteString(stdinLine)
@@ -57,24 +57,34 @@ func getJsonsOrBlank() iter.Seq[string] {
var dummy any var dummy any
if err := json.Unmarshal([]byte(stdinEvent), &dummy); err != nil { if err := json.Unmarshal([]byte(stdinEvent), &dummy); err != nil {
continue return true
} }
if !yield(stdinEvent) { if !yield(stdinEvent) {
return return false
} }
curr.Reset() curr.Reset()
return true
})
if !hasStdin {
yield("{}")
} }
} }
} }
func getStdinLinesOrBlank() iter.Seq[string] { func getStdinLinesOrBlank() iter.Seq[string] {
return func(yield func(string) bool) { return func(yield func(string) bool) {
if hasStdinLines := writeStdinLinesOrNothing(yield); !hasStdinLines { hasStdin := writeStdinLinesOrNothing(func(stdinLine string) bool {
return if !yield(stdinLine) {
} else { return false
return }
return true
})
if !hasStdin {
yield("")
} }
} }
} }
@@ -107,10 +117,7 @@ func writeStdinLinesOrNothing(yield func(string) bool) (hasStdinLines bool) {
} }
hasEmittedAtLeastOne = true hasEmittedAtLeastOne = true
} }
if !hasEmittedAtLeastOne { return hasEmittedAtLeastOne
yield("")
}
return true
} else { } else {
// not piped // not piped
return false return false