-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.cs
More file actions
189 lines (170 loc) · 5.91 KB
/
Copy pathFunction.cs
File metadata and controls
189 lines (170 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System.Text;
using System.Text.Json;
using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;
// Use the System.Text.Json Lambda serializer.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace CsvParserLambda;
/// <summary>
/// AWS Lambda replacement for a SQL CLR CSV table-valued function.
///
/// The core parsing logic (SplitLines + SplitRespectingQuotes) is copied
/// verbatim from the original CLR assembly (see ../../clr-reference/CsvParser.cs).
/// Only the entry-point wrapper changed: a Lambda handler that accepts a JSON
/// request and returns rows as a JSON array.
/// </summary>
public class Function
{
public APIGatewayProxyResponse FunctionHandler(
APIGatewayProxyRequest request, ILambdaContext context)
{
try
{
var body = JsonSerializer.Deserialize<ParseRequest>(
request.Body ?? "{}",
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
);
if (string.IsNullOrEmpty(body?.CsvData))
{
return CreateResponse(400, new { success = false, error = "csvData is required" });
}
string delimiter = string.IsNullOrEmpty(body.Delimiter) ? "," : body.Delimiter;
char delimChar = delimiter[0];
bool skipHeader = body.SkipHeader;
var rows = new List<ParsedRow>();
string[] lines = SplitLines(body.CsvData);
int rowNum = 0;
bool isFirst = true;
foreach (string line in lines)
{
if (string.IsNullOrWhiteSpace(line)) continue;
if (isFirst && skipHeader)
{
isFirst = false;
continue;
}
isFirst = false;
rowNum++;
string[] fields = SplitRespectingQuotes(line.TrimEnd('\r'), delimChar);
rows.Add(new ParsedRow { RowNum = rowNum, Fields = fields });
}
var result = new { success = true, rowCount = rows.Count, rows };
return CreateResponse(200, result);
}
catch (JsonException ex)
{
context.Logger.LogError($"JSON parse error: {ex.Message}");
return CreateResponse(400, new { success = false, error = "Invalid JSON in request body" });
}
catch (Exception ex)
{
context.Logger.LogError($"Unhandled error: {ex.Message}");
return CreateResponse(500, new { success = false, error = "Internal server error" });
}
}
/// <summary>
/// RFC 4180 compliant CSV field splitter.
/// Handles quoted fields, embedded delimiters, and escaped quotes ("").
/// COPIED VERBATIM FROM THE CLR ASSEMBLY.
/// </summary>
private static string[] SplitRespectingQuotes(string input, char delimiter)
{
var fields = new List<string>();
var field = new StringBuilder();
bool inQuotes = false;
int i = 0;
while (i < input.Length)
{
char c = input[i];
if (c == '"')
{
if (inQuotes && i + 1 < input.Length && input[i + 1] == '"')
{
field.Append('"'); // escaped quote -> literal quote
i += 2;
continue;
}
inQuotes = !inQuotes;
}
else if (c == delimiter && !inQuotes)
{
fields.Add(field.ToString());
field.Clear();
}
else
{
field.Append(c);
}
i++;
}
fields.Add(field.ToString());
return fields.ToArray();
}
/// <summary>
/// Splits into logical records, respecting quotes so that a newline inside a
/// quoted field does NOT start a new record. Quote characters are preserved
/// so the field-level splitter can use them.
/// COPIED VERBATIM FROM THE CLR ASSEMBLY.
/// </summary>
private static string[] SplitLines(string input)
{
var lines = new List<string>();
var currentLine = new StringBuilder();
bool inQuotes = false;
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if (c == '"')
{
if (inQuotes && i + 1 < input.Length && input[i + 1] == '"')
{
currentLine.Append('"');
currentLine.Append('"');
i++;
continue;
}
inQuotes = !inQuotes;
currentLine.Append(c);
}
else if (c == '\n' && !inQuotes)
{
lines.Add(currentLine.ToString());
currentLine.Clear();
}
else if (c == '\r' && !inQuotes)
{
continue;
}
else
{
currentLine.Append(c);
}
}
if (currentLine.Length > 0)
lines.Add(currentLine.ToString());
return lines.ToArray();
}
private static APIGatewayProxyResponse CreateResponse(int statusCode, object body)
{
return new APIGatewayProxyResponse
{
StatusCode = statusCode,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } },
Body = JsonSerializer.Serialize(body, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
})
};
}
}
public class ParseRequest
{
public string CsvData { get; set; } = "";
public string Delimiter { get; set; } = ",";
public bool SkipHeader { get; set; } = false;
}
public class ParsedRow
{
public int RowNum { get; set; }
public string[] Fields { get; set; } = Array.Empty<string>();
}