refactor: log parser parseLine to parseFields
This commit is contained in:
		| @@ -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) | ||||
| 	} | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user