Skip to content

Commit

Permalink
fix: skip null field on tsv export
Browse files Browse the repository at this point in the history
  • Loading branch information
phuchptty committed Jan 14, 2024
1 parent f39e6d4 commit 168a0bc
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions modules/exporter/tsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func TsvExport(subjectScoreMap map[string]SubjectStudentCore, output string) {

for _, studentScore := range value.StudentScores {

// Skip if student code is empty
if studentScore.StudentCode == "" {
continue
}

// Append to student data if not exist
_, studentExistYet := studentsDataMap[studentScore.StudentCode]
if !studentExistYet {
Expand All @@ -92,33 +97,31 @@ func TsvExport(subjectScoreMap map[string]SubjectStudentCore, output string) {
studentsData := maps.Values(studentsDataMap)
subjectsData := maps.Values(subjectsDataMap)

outputPath := generateOutputPath(output)

studentsTsvContent, err := gocsv.MarshalString(&studentsData)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal TSV students data")
}
saveToFile(output, "students.tsv", studentsTsvContent)
saveToFile(outputPath, "students.tsv", studentsTsvContent)
log.Info().Msg("Exported students data to TSV")

subjectsTsvContent, err := gocsv.MarshalString(&subjectsData)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal TSV subjects data")
}
saveToFile(output, "subjects.tsv", subjectsTsvContent)
saveToFile(outputPath, "subjects.tsv", subjectsTsvContent)
log.Info().Msg("Exported students data to TSV")

studentScoresTsvContent, err := gocsv.MarshalString(&studentScores)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal TSV student scores data")
}
saveToFile(output, "scores.tsv", studentScoresTsvContent)
saveToFile(outputPath, "scores.tsv", studentScoresTsvContent)
log.Info().Msg("Exported students data to TSV")
}

func saveToFile(output string, fileName string, content string) {
// Create file
timestamp := time.Now().Unix()
outputPath := path.Join(output, strconv.FormatInt(timestamp, 10))

func saveToFile(outputPath string, fileName string, content string) {
err := os.MkdirAll(outputPath, os.ModePerm)
if err != nil {
log.Warn().Err(err).Msgf("Failed to create directory %s", outputPath)
Expand All @@ -130,3 +133,11 @@ func saveToFile(output string, fileName string, content string) {
log.Error().Err(err).Msgf("Failed to create file %s", fileName)
}
}

func generateOutputPath(output string) string {
// Create file
timestamp := time.Now().Unix()
outputPath := path.Join(output, strconv.FormatInt(timestamp, 10))

return outputPath
}

0 comments on commit 168a0bc

Please sign in to comment.