-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
128 changed files
with
18,657 additions
and
2,918 deletions.
There are no files selected for viewing
Binary file modified
BIN
+21.4 KB
(180%)
TinyCsvParser/Documentation/build/doctrees/environment.pickle
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+1.08 KB
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/changelog.doctree
Binary file not shown.
Binary file modified
BIN
+842 Bytes
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/contributing.doctree
Binary file not shown.
Binary file modified
BIN
+1.55 KB
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/faq.doctree
Binary file not shown.
Binary file modified
BIN
+48 Bytes
(100%)
TinyCsvParser/Documentation/build/doctrees/sections/overview.doctree
Binary file not shown.
Binary file modified
BIN
+297 Bytes
(100%)
TinyCsvParser/Documentation/build/doctrees/sections/overview/introduction.doctree
Binary file not shown.
Binary file modified
BIN
+1.18 KB
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/quickstart.doctree
Binary file not shown.
Binary file modified
BIN
+1.33 KB
(120%)
TinyCsvParser/Documentation/build/doctrees/sections/tutorials.doctree
Binary file not shown.
Binary file modified
BIN
+878 Bytes
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/tutorials/custom_formats.doctree
Binary file not shown.
Binary file added
BIN
+14.3 KB
TinyCsvParser/Documentation/build/doctrees/sections/tutorials/custom_mapping.doctree
Binary file not shown.
Binary file modified
BIN
+329 Bytes
(100%)
TinyCsvParser/Documentation/build/doctrees/sections/tutorials/mapping_arrays.doctree
Binary file not shown.
Binary file modified
BIN
+730 Bytes
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/tutorials/parsing_enums.doctree
Binary file not shown.
Binary file modified
BIN
+165 Bytes
(100%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide.doctree
Binary file not shown.
Binary file modified
BIN
+2.24 KB
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide/concepts.doctree
Binary file not shown.
Binary file modified
BIN
+266 Bytes
(100%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide/info.doctree
Binary file not shown.
Binary file modified
BIN
+344 Bytes
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide/install.doctree
Binary file not shown.
Binary file modified
BIN
+68 Bytes
(100%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide/parallel.doctree
Binary file not shown.
Binary file modified
BIN
+2.91 KB
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide/parser.doctree
Binary file not shown.
Binary file modified
BIN
+2.21 KB
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide/tokenizer.doctree
Binary file not shown.
Binary file modified
BIN
+1.72 KB
(110%)
TinyCsvParser/Documentation/build/doctrees/sections/userguide/typeconverter.doctree
Binary file not shown.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: e8ec982bd7f52df4525c4ed61d6d6b52 | ||
config: ffc8d2ef8e45a0dd35befa8f465ab5df | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
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
117 changes: 117 additions & 0 deletions
117
...ser/Documentation/build/html/_sources/sections/tutorials/custom_mapping.rst.txt
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,117 @@ | ||
.. _tutorials_custom_mapping: | ||
|
||
Custom Mapping | ||
============== | ||
|
||
In some cases you may need to map CSV columns onto properties of your output entity in a way that is not 1:1, or your | ||
output entity may contain other non-scalar types which you need to populate using multiple columns from the CSV row. | ||
This is where :code:`MapUsing` comes into play. MapUsing accepts a delegate which will be called for each non-empty, | ||
non-comment row in your CSV. The call to the supplied delegate happens *after* all of the :code:`MapProperty` mappings | ||
have executed for that row, so your entity maybe partially populated with data by the time your delegate executes. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
First, like any other mapping, you need to establish an implementation of :code:`CsvMapping<MyEntity>`. | ||
|
||
.. code-block:: csharp | ||
public class Person | ||
{ | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
} | ||
public class MyEntity | ||
{ | ||
public string OrderId { get; set; } | ||
public Person Customer { get; set; } | ||
} | ||
// CSV format: 1234,Doe,John | ||
public class MyMap : CsvMapping<MyEntity> | ||
{ | ||
public MyMap() | ||
{ | ||
MapProperty(0, x => x.OrderId); | ||
// TODO: Map person using MapUsing() | ||
} | ||
} | ||
Adding MapUsing | ||
~~~~~~~~~~~~~~~ | ||
|
||
Now we have our first column mapped to the OrderId, but how can we map the second & third columns to an instance of our :code:`Person` class? | ||
:code:`MapUsing` to the rescue! | ||
|
||
.. code-block:: csharp | ||
// CSV format: 1234,Doe,John | ||
public class MyMap : CsvMapping<MyEntity> | ||
{ | ||
public MyMap() | ||
{ | ||
MapProperty(0, x => x.OrderId); | ||
MapUsing((entity, values) => | ||
{ | ||
// TODO: Invalidate the row if first name is missing. | ||
var customer = new Person(); | ||
// WARNING: IndexOutOfRangeException could happen here!! | ||
customer.LastName = values.Tokens[1]; | ||
customer.FirstName = values.Tokens[2]; | ||
entity.Customer = customer; | ||
return true; | ||
}); | ||
} | ||
} | ||
Getting Defensive | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
Great! Now our :code:`MyEntity` class will get correctly populated with the order ID and a :code:`Person` instance with the correct | ||
first & last name set. But what happens if we encounter a row that is missing the first name, along the lines of "1234,Acme Inc"? | ||
This is bad news, especially if multiple rows could be missing the third column ... each row will raise an exception, which would be | ||
very detrimental to parsing performance. That's why we require your :code:`MapUsing` delegate to return a boolean, indicating | ||
whether the data you mapped resulted in a valid row. | ||
|
||
Note: you should avoid doing things that could raise exceptions within your | ||
delegate, **even** if you use :code:`try...catch`. The very fact that the exception is raised will slow your CSV parsing down | ||
tremendously, even if it is caught and discarded. | ||
|
||
|
||
.. code-block:: csharp | ||
// CSV format: 1234,Doe,John | ||
public class MyMap : CsvMapping<MyEntity> | ||
{ | ||
public MyMap() | ||
{ | ||
MapProperty(0, x => x.OrderId); | ||
MapUsing((entity, values) => | ||
{ | ||
// Checking that we have enough data and that the data is within range | ||
// should happen before we try to access & map it below. | ||
if(values.Tokens.Length < 3) | ||
{ | ||
return false; | ||
} | ||
var customer = new Person(); | ||
customer.LastName = values.Tokens[1]; | ||
customer.FirstName = values.Tokens[2]; | ||
entity.Customer = customer; | ||
return true; | ||
}); | ||
} | ||
} | ||
.. _TinyCsvParser: https://github.com/bytefish/TinyCsvParser |
Oops, something went wrong.