-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-source.cpp
More file actions
129 lines (105 loc) · 3.31 KB
/
Copy pathdata-source.cpp
File metadata and controls
129 lines (105 loc) · 3.31 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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Diego Lopes <diego95lopes@gmail.com>
#include "data-source.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include <sstream>
using json = nlohmann::json;
std::vector<Record> JsonFileDataSource::GetData() const
{
std::vector<Record> records;
std::ifstream f(filePath);
if (!f.is_open()) {
throw std::runtime_error("Failed to open data source file: " + filePath);
}
json j;
f >> j;
if (!j.is_array()) {
throw std::runtime_error("Expected JSON array in data source file: " + filePath);
}
for (const auto& item : j) {
if (!item.is_object())
continue;
Record record;
for (auto it = item.begin(); it != item.end(); ++it) {
if (it.value().is_string()) {
record[it.key()] = it.value().get<std::string>();
} else {
record[it.key()] = it.value().dump();
}
}
records.push_back(std::move(record));
}
return records;
}
std::vector<Record> CsvFileDataSource::GetData() const
{
std::vector<Record> records;
std::ifstream f(filePath);
if (!f.is_open()) {
throw std::runtime_error("Failed to open data source file: " + filePath);
}
std::string line;
std::vector<std::string> headers;
// Read header line
if (std::getline(f, line)) {
std::istringstream ss(line);
std::string cell;
while (std::getline(ss, cell, ',')) {
headers.push_back(cell);
}
} else {
throw std::runtime_error("CSV file is empty: " + filePath);
}
// Read data lines
while (std::getline(f, line)) {
std::istringstream ss(line);
std::string cell;
Record record;
size_t idx = 0;
while (std::getline(ss, cell, ',')) {
if (idx < headers.size()) {
record[headers[idx]] = cell;
}
idx++;
}
records.push_back(std::move(record));
}
return records;
}
ManualDataSource::ManualDataSource(Table table) : m_table(std::move(table)) {}
std::vector<Record> ManualDataSource::GetData() const
{
std::lock_guard<std::mutex> lock(m_mutex);
std::vector<Record> records;
records.reserve(m_table.rows.size());
for (const auto& row : m_table.rows) {
Record record;
for (size_t c = 0; c < m_table.columns.size(); ++c) {
const std::string& name = m_table.columns[c].name;
// No element id can be empty, so a column with no name cannot
// match one — skip it rather than storing a key nothing reads.
if (name.empty()) continue;
// Record is an unordered_map, so two columns sharing a name
// collapse into one entry — last column wins, silently.
record[name] = c < row.size() ? row[c] : "";
}
records.push_back(std::move(record));
}
return records;
}
std::string ManualDataSource::GetDisplayName() const
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_table.name;
}
ManualDataSource::Table ManualDataSource::GetTable() const
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_table;
}
void ManualDataSource::SetTable(Table table)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_table = std::move(table);
}