Skip to content

Commit

Permalink
refactor: update textLineHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
iret-kawashima committed Nov 10, 2023
1 parent 2a8bc63 commit 5ac81e5
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/nekrassov01/access-log-parser"
Expand Down Expand Up @@ -35,14 +36,28 @@ func prettyJSON(s string) (string, error) {

func textLineHandler(matches []string, fields []string, index int) (string, error) {
var builder strings.Builder
builder.WriteString(fmt.Sprintf("index=%d", index))
builder.WriteString("index=")
builder.WriteString(strconv.Itoa(index))
for i, match := range matches {
if i < len(fields) {
builder.WriteRune(' ')
builder.WriteString(fmt.Sprintf("%s=\"%s\"", fields[i], strings.ReplaceAll(match, `"`, `\"`)))
builder.WriteString(" ")
builder.WriteString(fields[i])
builder.WriteString(`="`)
if match == "\"-\"" {
builder.WriteString("-")
} else {
for _, m := range match {
if m == '"' {
builder.WriteString(`\"`)
} else {
builder.WriteRune(m)
}
}
}
builder.WriteString(`"`)
}
}
return strings.ReplaceAll(builder.String(), `"\"-\""`, `"-"`), nil
return builder.String(), nil
}

func textMetadataHandler(m *parser.Metadata) (string, error) {
Expand Down

0 comments on commit 5ac81e5

Please sign in to comment.