{"record":{"id":"6abb053d1d165cad","repo":"inancgumus/learngo","slug":"missing-fields-v","errorCode":null,"errorMessage":"missing fields: %v","messagePattern":"missing fields: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/testing/report/result.go","lineNumber":42,"sourceCode":"\tVisits    int    `json:\"visits\"`\n\tTimeSpent int    `json:\"time_spent\"`\n\t// add more metrics if needed\n}\n\n// add adds the metrics of another Result to itself and returns a new Result\nfunc (r Result) add(other Result) Result {\n\treturn Result{\n\t\tDomain:    r.Domain,\n\t\tVisits:    r.Visits + other.Visits,\n\t\tTimeSpent: r.TimeSpent + other.TimeSpent,\n\t}\n}\n\n// parse parses a single log line\nfunc parse(line string) (r Result, err error) {\n\tfields := strings.Fields(line)\n\tif len(fields) != 3 {\n\t\treturn r, fmt.Errorf(\"missing fields: %v\", fields)\n\t}\n\n\tf := new(field)\n\tr.Domain = fields[0]\n\tr.Visits = f.atoi(\"visits\", fields[1])\n\tr.TimeSpent = f.atoi(\"time spent\", fields[2])\n\treturn r, f.err\n}\n\n// field helps for field parsing\ntype field struct{ err error }\n\nfunc (f *field) atoi(name, val string) int {\n\tn, err := strconv.Atoi(val)\n\tif n < 0 || err != nil {\n\t\tf.err = fmt.Errorf(\"incorrect %s: %q\", name, val)\n\t}\n\treturn n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/testing/report/result.go#L24-L60","documentation":"parse splits a log line with strings.Fields and requires exactly 3 fields: domain, visits, and time spent. Any other count returns this error listing the fields actually found, signaling the line doesn't match the expected 'domain visits timespent' format.","triggerScenarios":"Calling parse (from main, via the report parser) with a line containing 0, 1, 2, or 4+ whitespace-separated tokens, e.g. a blank line or a line missing the time-spent value.","commonSituations":"Hand-edited log files, CSV/TSV lines pasted into a space-separated log, trailing multi-space entries that Fields collapses, or header/footer lines in exported logs.","solutions":["Normalize the input so every line has exactly: domain visits timeSpent","Skip empty/whitespace-only lines before calling parse","Give a clearer message, e.g. expected 3 fields, got N, with the raw line"],"exampleFix":"// before\nif len(fields) != 3 {\n    return r, fmt.Errorf(\"missing fields: %v\", fields)\n}\n// after\nif len(fields) != 3 {\n    return r, fmt.Errorf(\"expected 3 fields (domain visits timeSpent), got %d: %q\", len(fields), line)\n}","handlingStrategy":"validation","validationCode":"func validResultLine(line string) bool {\n    fs := strings.Fields(line)\n    return len(fs) == 3\n}\n// call before parse: if !validResultLine(line) { skip }","typeGuard":"func hasThreeFields(line string) bool {\n    return len(strings.Fields(line)) == 3\n}","tryCatchPattern":"r, err := parse(line)\nif err != nil {\n    if strings.HasPrefix(err.Error(), \"missing fields\") {\n        log.Printf(\"skipping malformed line %q: %v\", line, err)\n        continue\n    }\n    return err\n}","preventionTips":["Skip empty lines before parse","Normalize delimiters (tabs/commas -> spaces) beforehand","State the expected 'domain visits timeSpent' format in the error text","Add unit tests for 0/1/2/4-field lines"],"tags":["go","parsing","validation"],"backgroundTag":"wrong-number-of-fields","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}