refactor: log parser parseLine to parseFields

This commit is contained in:
Inanc Gumus
2019-08-06 01:45:41 +03:00
parent 9ad46b14ce
commit eadf0c85e4
2 changed files with 4 additions and 5 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"strings"
)
// result stores the parsed result for a domain
@ -13,9 +12,8 @@ type result struct {
uniques int
}
// parseLine parses a log line and returns the parsed result with an error
func parseLine(line string) (r result, err error) {
fields := strings.Fields(line)
// parseFields parses and returns the parsing result
func parseFields(fields []string) (r result, err error) {
if len(fields) != 4 {
return r, fmt.Errorf("wrong number of fields -> %v", fields)
}

View File

@ -11,6 +11,7 @@ import (
"bufio"
"fmt"
"io"
"strings"
)
func textReader(r io.Reader) ([]result, error) {
@ -27,7 +28,7 @@ func parseText(in *bufio.Scanner) ([]result, error) {
for in.Scan() {
lines++
result, err := parseLine(in.Text())
result, err := parseFields(strings.Fields(in.Text()))
if err != nil {
// TODO: custom error type for line information
return nil, fmt.Errorf("line %d: %v", lines, err)