Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task FormatDataAsync(IAsyncEnumerable<IDataItem> dataItems, Stream
settings.Validate();

await using var textWriter = new StreamWriter(target, leaveOpen: true);
await using var writer = new CsvWriter(textWriter, new CsvConfiguration(CultureInfo.InvariantCulture)
await using var writer = new CsvWriter(textWriter, new CsvConfiguration(settings.GetCultureInfo())
{
Delimiter = settings.Delimiter,
HasHeaderRecord = settings.IncludeHeader,
Expand All @@ -49,7 +49,7 @@ public async Task FormatDataAsync(IAsyncEnumerable<IDataItem> dataItems, Stream

foreach (string field in item.GetFieldNames())
{
writer.WriteField(item.GetValue(field)?.ToString());
writer.WriteField(item.GetValue(field));
}

firstRecord = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
using Cosmos.DataTransfer.Interfaces;
using System.Globalization;
using Cosmos.DataTransfer.Interfaces;

namespace Cosmos.DataTransfer.CsvExtension.Settings;

public class CsvWriterSettings : IDataExtensionSettings
{
public bool IncludeHeader { get; set; } = true;
public string Delimiter { get; set; } = ",";
public string Culture { get; set; } = "InvariantCulture";
public CultureInfo GetCultureInfo() {
switch (this.Culture.ToLower())
{
case "invariantculture": return CultureInfo.InvariantCulture;
case "current": return CultureInfo.CurrentCulture;
default: return CultureInfo.GetCultureInfo(this.Culture);
}
}
}
11 changes: 10 additions & 1 deletion Extensions/Csv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ See storage extension documentation for any storage specific settings needed ([e

Source supports an optional `Delimiter` parameter (`,` by default) and an optional `HasHeader` parameter (`true` by default). For files without a header, column names will be generated based on the `ColumnNameFormat` setting, which uses a default value of `column_{0}` to produce columns `column_0`, `column_1`, etc.


```json
{
"Delimiter": ",",
Expand All @@ -35,9 +36,17 @@ Source supports an optional `Delimiter` parameter (`,` by default) and an option

Sink supports an optional `Delimiter` parameter (`,` by default) and an optional `IncludeHeader` parameter (`true` by default) to add a leading row of column names.

Formatting options, or locale, can be set with an optional `Culture` setting (`"Invariant"` by default).
This specifies how e.g., numbers and dates are formatted according to a specific culture.
Set to `"Current"` to use the system's or process' current locale setting
(see [CultureInfo.CurrentCulture](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentculture)),
or e.g., `"en"`, `"en-GB"`, or `"en-US"` for English standards (period, `.`, as decimal separator and other regional standards),
"da-DK" for Danish (comma, `,`, as decimal separator), etc.

```json
{
"Delimiter": ",",
"IncludeHeader": true
"IncludeHeader": true,
"Culture": "Invariant"
}
```