refactor: log parser parseLine to parseFields
This commit is contained in:
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// result stores the parsed result for a domain
|
// result stores the parsed result for a domain
|
||||||
@ -13,9 +12,8 @@ type result struct {
|
|||||||
uniques int
|
uniques int
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseLine parses a log line and returns the parsed result with an error
|
// parseFields parses and returns the parsing result
|
||||||
func parseLine(line string) (r result, err error) {
|
func parseFields(fields []string) (r result, err error) {
|
||||||
fields := strings.Fields(line)
|
|
||||||
if len(fields) != 4 {
|
if len(fields) != 4 {
|
||||||
return r, fmt.Errorf("wrong number of fields -> %v", fields)
|
return r, fmt.Errorf("wrong number of fields -> %v", fields)
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func textReader(r io.Reader) ([]result, error) {
|
func textReader(r io.Reader) ([]result, error) {
|
||||||
@ -27,7 +28,7 @@ func parseText(in *bufio.Scanner) ([]result, error) {
|
|||||||
for in.Scan() {
|
for in.Scan() {
|
||||||
lines++
|
lines++
|
||||||
|
|
||||||
result, err := parseLine(in.Text())
|
result, err := parseFields(strings.Fields(in.Text()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: custom error type for line information
|
// TODO: custom error type for line information
|
||||||
return nil, fmt.Errorf("line %d: %v", lines, err)
|
return nil, fmt.Errorf("line %d: %v", lines, err)
|
||||||
|
Reference in New Issue
Block a user