Skip to content

ToString for CSV Parser results #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions csvparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/csv"
"fmt"
"io"
"reflect"
"strings"
)

Expand Down Expand Up @@ -271,3 +272,56 @@ func (c *CsvParser[ReadTo]) runOnFinish() {
c.onFinish()
}
}

/*
Given array of structs from the parsed csv, prints out the contents of the file parsing in a nice string format.
The input of the function can take in any type of structs with any number of fields within the struct.
*/
func ParseToString(arr []interface{}) string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing!
I like the idea of having a ParseToString helper. Maybe we could just put this function in a new .go file? Maybe a file with the name helpers.go.
Also, could you add a unit test or two? It would be nice to have everything tested.

result := ""

// For each struct in the given array
for j, val := range arr {
line := "|-> ("

// Use reflect package to obtain number of fields in struct
v := reflect.ValueOf(val)
values := make([]interface{}, v.NumField())

// Store array of each field in the struct
for i := 0; i < v.NumField(); i++ {
values[i] = v.Field(i).Interface()
}

if j == 0 {
// Add top outline of the printed result
for k := 0; k < 20*(v.NumField())+10; k++ {
result += "_"
}
result += "\n"
}

for i, printVal := range values {
// Translate each field to a string. Each field started as an interface.
str := fmt.Sprintf("%v", printVal)
line += str

if i < len(values)-1 {
// Add buffer of spaces between each field on every row. The buffer size is set to 25 chars.
for i := 0; i < 25-len(str); i++ {
line += " "
}
} else {
line += ")"
}

}
if j != 0 {
result += "\n"
}
result += line
}

// Return string of the struct contents in a more readable format
return result
}
6 changes: 6 additions & 0 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ parser.AddColumnParser("age", func (value string, into *Person) error {

// res is []Person type
res, err := parser.Parse()
translate := make([]interface{}, len(res))
for i, s := range res {
translate[i] = s
}
resultString := parseToString(translate)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be csvparser.ParseToString(translate)

fmt.Println(resultString)
```

Note: as long as there is a parser for the header that you want, the order of the .csv columns will not matter
Expand Down