Skip to content
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

provide a build in readcsv for users #77

Open
elbraulio opened this issue Apr 5, 2019 · 0 comments
Open

provide a build in readcsv for users #77

elbraulio opened this issue Apr 5, 2019 · 0 comments
Labels
p: medium medium priority s: available ready to work on t: feature New feature or request
Milestone

Comments

@elbraulio
Copy link
Owner

elbraulio commented Apr 5, 2019

Problem
ezload provides a way to read CSV checking its format. But it is too focused on writing to a database when it is also useful to just read CSV. Actually, to read CSV you have to do this

while ((raw = bufferedReader.readLine()) != null) {
    Line parsedLine = this.parser.parse(raw);
    for (Value value : parsedLine.values()) {
        value.accept(new SomeAction(...));
    }
}

In this example, users have to implement an action to take values from csv for each line.

The solution I'd like
provide a ReadCsv to read easily CSV lines by columns. Maybe something like this

ReadCsv read = new ReadImplementation(Parser, Path);
while(read.hasNext()) Object o = read.next().get(0);

The problem of the example above is that a line has multiple columns with different types and we don't want to cast. So, considering that every time we read a line the parser checks its format. When we retrieve a value, we can assume that the line is correct. Otherwise, an exception should have been thrown and in that case, we didn't even have the chance to read a column from a wrong line. Also, we already know the types that ezload allows to read. So we can do this

ReadCsv read = new ReadImplementation(Parser, Path);
read.stream().forEach(
    line -> line.get(0).intValue(); line.get(1).stringValue(); line.get(2).dateValue();
);

When we call dateValue and that column is an int we can throw an exception or try to parse the value into a LocalTime.

All these tries are not elegant enough. Another option is to have a Map<String, ?> for each type. Then the user can get them just calling them by its name

int i = line.intFrom("column name");
String s = line.stringFrom("another column name");
double d = line.doubleFrom("you got the idea right?");

Now if the user asks for an int that has been defined as String this throws an exception because that column name does not exist on int's map. Then the full example is

ReadCsv read = new ReadImplementation(Parser, Path, StandarCharset);
while(read.hasNext()) {
    MapLine line = read.next();
    int i = line.intFrom("column name");
    String s = line.stringFrom("another column name");
    double d = line.doubleFrom("you got the idea right?");
}
@elbraulio elbraulio added the s: triage on status triage process label Apr 5, 2019
@elbraulio elbraulio changed the title wip; provide a build in readcsv for users provide a build in readcsv for users Apr 8, 2019
@elbraulio elbraulio added p: medium medium priority s: available ready to work on t: feature New feature or request and removed s: triage on status triage process labels Apr 8, 2019
@elbraulio elbraulio added this to the 1.0.0 milestone Apr 8, 2019
This was referenced Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: medium medium priority s: available ready to work on t: feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant