Skip to content

Commit a2fc23b

Browse files
committed
Add HasHeader property and makes plugin able to read and write csv with/without header
#15
1 parent bc9bed0 commit a2fc23b

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/CsvPlugin.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ internal CsvPlugin(IGuidProvider guidProvider, IReflectionGuard reflectionGuard)
2626
_reflectionGuard = reflectionGuard ?? throw new ArgumentNullException(nameof(reflectionGuard));
2727
}
2828

29-
public PluginMetadata Metadata => new PluginMetadata
29+
public PluginMetadata Metadata => new()
3030
{
3131
Id = Guid.Parse("81c99765-9581-4f13-ba77-86c32ae21d97"),
3232
Name = "Csv",
3333
CompanyName = "FlowSynx",
3434
Description = Resources.PluginDescription,
35-
Version = new Version(1, 2, 2),
35+
Version = new Version(1, 2, 3),
3636
Category = PluginCategory.Data,
3737
Authors = new List<string> { "FlowSynx" },
3838
Copyright = "© FlowSynx. All rights reserved.",
@@ -93,7 +93,7 @@ public Task Initialize(IPluginLogger logger)
9393
{
9494
Delimiter = inputParameter.Delimiter ?? ",",
9595
IgnoreBlankLines = inputParameter.IgnoreBlankLines ?? true,
96-
HasHeaderRecord = true,
96+
HasHeaderRecord = inputParameter.HasHeader ?? true,
9797
TrimOptions = TrimOptions.Trim,
9898
DetectColumnCountChanges = true,
9999
BadDataFound = null
@@ -145,12 +145,12 @@ private string ReadDataFromPluginContext(PluginContext pluginContext, InputParam
145145
if (pluginContext.Content is not null)
146146
return pluginContext.Content;
147147
else if (pluginContext.StructuredData is not null)
148-
return StructuredDataToCsv(pluginContext.StructuredData, inputParameter.Delimiter);
148+
return StructuredDataToCsv(pluginContext.StructuredData, inputParameter.Delimiter, inputParameter.HasHeader);
149149
else
150150
throw new InvalidDataException(string.Format(Resources.TheEnteredDataIsInvalid, pluginContext.Id));
151151
}
152152

153-
private string StructuredDataToCsv(List<Dictionary<string, object>>? data, string? delimiter = ",")
153+
private string StructuredDataToCsv(List<Dictionary<string, object>>? data, string? delimiter = ",", bool? hasHeader = true)
154154
{
155155
if (data == null || data.Count == 0)
156156
return string.Empty;
@@ -159,7 +159,7 @@ private string StructuredDataToCsv(List<Dictionary<string, object>>? data, strin
159159
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
160160
{
161161
Delimiter = delimiter ?? ",",
162-
HasHeaderRecord = true,
162+
HasHeaderRecord = hasHeader ?? true,
163163
TrimOptions = TrimOptions.Trim,
164164
DetectColumnCountChanges = true,
165165
BadDataFound = null
@@ -198,7 +198,7 @@ private async Task<string> ToCsvStringAsync(IEnumerable<ExpandoObject> records,
198198
{
199199
Delimiter = inputParameter.Delimiter ?? ",",
200200
IgnoreBlankLines = inputParameter.IgnoreBlankLines ?? true,
201-
HasHeaderRecord = true,
201+
HasHeaderRecord = inputParameter.HasHeader ?? true,
202202
TrimOptions = TrimOptions.Trim,
203203
DetectColumnCountChanges = true,
204204
BadDataFound = null

src/Models/InputParameter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ internal class InputParameter
66
public object? Data { get; set; }
77
public string? Delimiter { get; set; } = ",";
88
public bool? IgnoreBlankLines { get; set; } = true;
9+
public bool? HasHeader { get; set; } = true;
910
public IEnumerable<string>? Mappings { get; set; }
1011
public object? Filters { get; set; }
1112
}

0 commit comments

Comments
 (0)