Skip to content

Commit

Permalink
feat: less LineHandler signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrassov01 committed Mar 17, 2024
1 parent a5b52b6 commit 610d892
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 366 deletions.
32 changes: 5 additions & 27 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ import (
"github.com/mattn/go-isatty"
)

const lineNumberLabel = "no"

// JSONLineHandler serializes log lines into JSON (NDJSON) format. It keywords the line number if specified.
// Labels and values are combined into key-value pairs, and the result is a single JSON object.
func JSONLineHandler(labels []string, values []string, lineNumber int, hasLineNumber, _ bool) (string, error) {
if hasLineNumber {
labels, values = addLineNumber(labels, values, lineNumber)
}
func JSONLineHandler(labels []string, values []string, _ bool) (string, error) {
b := &strings.Builder{}
b.WriteString("{")
for i, value := range values {
Expand All @@ -34,10 +29,7 @@ func JSONLineHandler(labels []string, values []string, lineNumber int, hasLineNu
}

// PrettyJSONLineHandler enhances JSONLineHandler by formatting the output for readability. It uses indentation and new lines.
func PrettyJSONLineHandler(labels []string, values []string, lineNumber int, hasLineNumber, _ bool) (string, error) {
if hasLineNumber {
labels, values = addLineNumber(labels, values, lineNumber)
}
func PrettyJSONLineHandler(labels []string, values []string, _ bool) (string, error) {
b := &strings.Builder{}
b.WriteString("{\n")
for i, value := range values {
Expand All @@ -56,10 +48,7 @@ func PrettyJSONLineHandler(labels []string, values []string, lineNumber int, has
}

// KeyValuePairLineHandler converts log lines into a space-separated string of key-value pairs.
func KeyValuePairLineHandler(labels []string, values []string, lineNumber int, hasLineNumber, _ bool) (string, error) {
if hasLineNumber {
labels, values = addLineNumber(labels, values, lineNumber)
}
func KeyValuePairLineHandler(labels []string, values []string, _ bool) (string, error) {
b := &strings.Builder{}
for i, value := range values {
if i < len(labels) {
Expand All @@ -75,10 +64,7 @@ func KeyValuePairLineHandler(labels []string, values []string, lineNumber int, h
}

// LTSVLineHandler formats log lines as LTSV (Labeled Tab-separated Values).
func LTSVLineHandler(labels []string, values []string, lineNumber int, hasLineNumber, _ bool) (string, error) {
if hasLineNumber {
labels, values = addLineNumber(labels, values, lineNumber)
}
func LTSVLineHandler(labels []string, values []string, _ bool) (string, error) {
b := &strings.Builder{}
for i, value := range values {
if i < len(labels) {
Expand All @@ -98,10 +84,7 @@ func LTSVLineHandler(labels []string, values []string, lineNumber int, hasLineNu
}

// TSVLineHandler formats log lines as TSV (Tab-separated Values).
func TSVLineHandler(labels []string, values []string, lineNumber int, hasLineNumber, isFirst bool) (string, error) {
if hasLineNumber {
labels, values = addLineNumber(labels, values, lineNumber)
}
func TSVLineHandler(labels []string, values []string, isFirst bool) (string, error) {
b := &strings.Builder{}
if isFirst {
header := strings.Join(labels, "\t")
Expand All @@ -125,8 +108,3 @@ func TSVLineHandler(labels []string, values []string, lineNumber int, hasLineNum
}
return b.String(), nil
}

// addLineNumber prepends the line number to labels and values.
func addLineNumber(labels []string, values []string, lineNumber int) ([]string, []string) {
return append([]string{lineNumberLabel}, labels...), append([]string{strconv.Itoa(lineNumber)}, values...)
}
Loading

0 comments on commit 610d892

Please sign in to comment.