Skip to content

Commit

Permalink
Merge pull request #129 from nlnwa/refactor/walk-part-2
Browse files Browse the repository at this point in the history
test: add test for `Result` and `Stats`
  • Loading branch information
trym-b authored Apr 8, 2024
2 parents 4aeda99 + 4c8ec4b commit 06d8944
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 187 deletions.
48 changes: 0 additions & 48 deletions internal/filewalker/filewalker.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,51 +327,3 @@ func (walker *fileWalker) walkDir(ctx context.Context, root, dirName string, fn
}
})
}

func (walker *fileWalker) hasSuffix(path string) bool {
if walker.suffixes == nil || len(walker.suffixes) == 0 {
return true
}
for _, suffix := range walker.suffixes {
if strings.HasSuffix(path, suffix) {
return true
}
}
return false
}

func (walker *fileWalker) isLog(log logType) bool {
return walker.logConsoleTypes&log != 0 || walker.logFile != nil && walker.logfileTypes&log != 0
}

func (walker *fileWalker) logSummary(str string) {
if walker.logConsoleTypes&summaryLogType != 0 {
fmt.Println(str)
}
if walker.logFile != nil && walker.logfileTypes&summaryLogType != 0 {
_, _ = fmt.Fprintln(walker.logFile, str)
}
}

func (walker *fileWalker) logInfo(res Result, recNum int) {
logString := res.Log(recNum)
if walker.logConsoleTypes&infoLogType != 0 {
fmt.Println(logString)
}
if walker.logFile != nil && walker.logfileTypes&infoLogType != 0 {
_, _ = fmt.Fprintln(walker.logFile, logString)
}
}

func (walker *fileWalker) logError(res Result, recordNumber int) {
recordNumberLogString := res.Log(recordNumber)
errorString := strings.ReplaceAll(res.Error(), "\n", "\n ")
if walker.logConsoleTypes&errorLogType != 0 {
fmt.Println(recordNumberLogString)
fmt.Println(errorString)
}
if walker.logFile != nil && walker.logfileTypes&errorLogType != 0 {
_, _ = fmt.Fprintln(walker.logFile, recordNumberLogString)
_, _ = fmt.Fprintln(walker.logFile, errorString)
}
}
49 changes: 49 additions & 0 deletions internal/filewalker/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filewalker

import (
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -49,3 +50,51 @@ func printResultsAndProgress(walker *fileWalker, resultChan chan Result, allResu
}
}
}

func (walker *fileWalker) hasSuffix(path string) bool {
if walker.suffixes == nil || len(walker.suffixes) == 0 {
return true
}
for _, suffix := range walker.suffixes {
if strings.HasSuffix(path, suffix) {
return true
}
}
return false
}

func (walker *fileWalker) isLog(log logType) bool {
return walker.logConsoleTypes&log != 0 || walker.logFile != nil && walker.logfileTypes&log != 0
}

func (walker *fileWalker) logSummary(str string) {
if walker.logConsoleTypes&summaryLogType != 0 {
fmt.Println(str)
}
if walker.logFile != nil && walker.logfileTypes&summaryLogType != 0 {
_, _ = fmt.Fprintln(walker.logFile, str)
}
}

func (walker *fileWalker) logInfo(res Result, recNum int) {
logString := res.Log(recNum)
if walker.logConsoleTypes&infoLogType != 0 {
fmt.Println(logString)
}
if walker.logFile != nil && walker.logfileTypes&infoLogType != 0 {
_, _ = fmt.Fprintln(walker.logFile, logString)
}
}

func (walker *fileWalker) logError(res Result, recordNumber int) {
recordNumberLogString := res.Log(recordNumber)
errorString := strings.ReplaceAll(res.Error(), "\n", "\n ")
if walker.logConsoleTypes&errorLogType != 0 {
fmt.Println(recordNumberLogString)
fmt.Println(errorString)
}
if walker.logFile != nil && walker.logfileTypes&errorLogType != 0 {
_, _ = fmt.Fprintln(walker.logFile, recordNumberLogString)
_, _ = fmt.Fprintln(walker.logFile, errorString)
}
}
144 changes: 144 additions & 0 deletions internal/filewalker/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package filewalker

import (
"encoding/binary"
"fmt"
"strings"
)

type Result interface {
fmt.Stringer
Log(fileNum int) string
GetStats() Stats
IncrRecords()
IncrProcessed()
IncrDuplicates()
AddError(err error)
Records() int64
Processed() int64
ErrorCount() int64
Errors() []error
SetFatal(error)
Fatal() error
Duplicates() int64
error
}

type result struct {
fileName string
records int64
processed int64
errorCount int64
errors []error
duplicates int64
fatal error
}

func NewResult(fileName string) Result {
return &result{fileName: fileName}
}

func (r *result) IncrRecords() {
r.records++
}

func (r *result) IncrProcessed() {
r.processed++
}

func (r *result) IncrDuplicates() {
r.duplicates++
}

func (r *result) AddError(err error) {
r.errors = append(r.errors, err)
r.errorCount++
}

func (r *result) Records() int64 {
return r.records
}

func (r *result) Processed() int64 {
return r.processed
}

func (r *result) ErrorCount() int64 {
return r.errorCount
}

func (r *result) Duplicates() int64 {
return r.duplicates
}

func (r *result) Errors() []error {
return r.errors
}

func (r *result) Error() string {
sb := strings.Builder{}
if len(r.errors) > 0 {
for i, e := range r.errors {
if i > 0 {
sb.WriteByte('\n')
}
sb.WriteString(fmt.Sprintf(" %s", e))
}
}
return sb.String()
}

func (r *result) SetFatal(e error) {
r.fatal = e
}

func (r *result) Fatal() error {
return r.fatal
}

func (r *result) String() string {
return fmt.Sprintf("%s: records: %d, processed: %d, errors: %d, duplicates: %d", r.fileName, r.records, r.processed, r.ErrorCount(), r.duplicates)
}

func (r *result) Log(fileNum int) string {
return fmt.Sprintf("%06d %s", fileNum, r.String())
}

func (r *result) GetStats() Stats {
return &stats{
records: r.records,
processed: r.processed,
errors: r.ErrorCount(),
duplicates: r.duplicates,
}
}

func (r *result) UnmarshalBinary(data []byte) error {
var read int
v, n := binary.Varint(data[read:])
r.records = v
read += n
v, n = binary.Varint(data[read:])
r.processed = v
read += n
v, n = binary.Varint(data[read:])
r.duplicates = v
read += n
v, _ = binary.Varint(data[read:])
r.errorCount = v

return nil
}

func (r *result) MarshalBinary() (data []byte, err error) {
buf := make([]byte, binary.MaxVarintLen64*3)
written := binary.PutVarint(buf, r.records)
b := buf[written:]
written += binary.PutVarint(b, r.processed)
b = buf[written:]
written += binary.PutVarint(b, r.duplicates)
b = buf[written:]
written += binary.PutVarint(b, r.errorCount)

return buf[:written], nil
}
Loading

0 comments on commit 06d8944

Please sign in to comment.