-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to one content file provider
- Loading branch information
Mike Savage
committed
Jul 3, 2022
1 parent
39855a2
commit 866198b
Showing
15 changed files
with
455 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package contentfile | ||
|
||
import ( | ||
"encoding/csv" | ||
"io" | ||
"log" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/ezeoleaf/larry/domain" | ||
) | ||
|
||
type CsvFileReader struct { | ||
skipHeader bool | ||
} | ||
|
||
func NewCsvFileReader(skipHeader bool) ContentFileReader { | ||
return CsvFileReader{skipHeader: skipHeader} | ||
} | ||
|
||
func (r CsvFileReader) getContentFromReader(handle io.Reader, skip func(string) bool) (*domain.Content, error) { | ||
size := 1 | ||
var reservoir []string | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
count := 0 | ||
skipHeader := r.skipHeader | ||
csvReader := csv.NewReader(handle) | ||
for { | ||
rec, err := csvReader.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// skip header line | ||
if skipHeader { | ||
skipHeader = false | ||
continue | ||
} | ||
|
||
if rec[0] == "" { | ||
log.Println("content missing title, skipping record") | ||
continue | ||
} | ||
|
||
if skip(rec[0]) { | ||
continue | ||
} | ||
|
||
// reservoir sampling technique | ||
if count < size { | ||
reservoir = rec | ||
} else { | ||
j := rand.Intn(count + 1) | ||
if j < size { | ||
reservoir = rec | ||
} | ||
} | ||
|
||
count++ | ||
} | ||
|
||
if count > 0 { | ||
if content, err := convertCsvToContent(reservoir); err != nil { | ||
return nil, err | ||
} else { | ||
return content, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func convertCsvToContent(rec []string) (*domain.Content, error) { | ||
content := domain.Content{ExtraData: []string{}} | ||
if len(rec) > 0 { | ||
content.Title = StringToPointer(rec[0]) | ||
} | ||
if len(rec) > 1 { | ||
content.Subtitle = StringToPointer(rec[1]) | ||
} | ||
if len(rec) > 2 { | ||
content.URL = StringToPointer(rec[2]) | ||
} | ||
if len(rec) > 3 { | ||
// number of extra data fields is variable for CSV | ||
content.ExtraData = make([]string, len(rec)-3) | ||
for i := 3; i < len(rec); i++ { | ||
content.ExtraData[i-3] = rec[i] | ||
} | ||
} | ||
return &content, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package contentfile | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/ezeoleaf/larry/domain" | ||
) | ||
|
||
type JsonFileReader struct { | ||
} | ||
|
||
func NewJsonFileReader() ContentFileReader { | ||
return JsonFileReader{} | ||
} | ||
|
||
func (r JsonFileReader) getContentFromReader(handle io.Reader, skip func(string) bool) (*domain.Content, error) { | ||
size := 1 | ||
reservoir := domain.Content{} | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
decoder := json.NewDecoder(handle) | ||
if _, err := decoder.Token(); err != nil { | ||
if err.Error() == "EOF" { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
count := 0 | ||
for decoder.More() { | ||
data := new(domain.Content) | ||
if err := decoder.Decode(data); err != nil { | ||
return nil, err | ||
} | ||
|
||
if data.Title == nil || *data.Title == "" { | ||
log.Println("content missing title, skipping record") | ||
continue | ||
} | ||
|
||
if skip(*data.Title) { | ||
continue | ||
} | ||
|
||
// reservoir sampling technique | ||
if count < size { | ||
reservoir = *data | ||
} else { | ||
j := rand.Intn(count + 1) | ||
if j < size { | ||
reservoir = *data | ||
} | ||
} | ||
count++ | ||
} | ||
|
||
if count > 0 { | ||
return &reservoir, nil | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.