-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_to_swift_endpoint_enum.cpp
125 lines (109 loc) · 3.44 KB
/
csv_to_swift_endpoint_enum.cpp
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
//
// enumGenerator.cpp
//
//
// Created by Stefan Schmitt on 09/10/2023.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
struct Endpoint
{
std::string name;
std::string url;
std::string method;
Endpoint(const std::string &_name, const std::string &_url, const std::string &_method)
: name(_name), url(_url), method(_method) {}
};
/// @brief given https://example.com/api/endpoint1
/// @param method the HTTP method
/// @param url the endpoint
/// @return the generated name
std::string generateName(const std::string &method, const std::string &url)
{
std::string name = method;
for (char &c : name)
{
c = std::tolower(c);
}
name += url;
std::string toFind = "https://";
std::size_t startPos = name.find(toFind);
size_t endPos = startPos + toFind.length();
name.erase(0, endPos);
std::replace(name.begin(), name.end(), '.', '_');
std::replace(name.begin(), name.end(), '/', '_');
return name;
}
// Function to parse a CSV file and store the data in a vector of Endpoint structs
std::vector<Endpoint> parseCSV(const std::string &filename)
{
std::vector<Endpoint> endpoints;
std::ifstream file(filename);
if (file.is_open())
{
std::string line;
// Skip the header line if present
std::getline(file, line);
while (std::getline(file, line))
{
std::istringstream iss(line);
std::string name, url, method;
// Assuming CSV format: url, method
if (std::getline(iss, url, ',') && std::getline(iss, method, ','))
{
name = generateName(method, url);
endpoints.push_back(Endpoint(name, url, method));
}
}
file.close();
}
else
{
std::cout << "Error opening file." << std::endl;
}
return endpoints;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " input.csv > output.swift" << std::endl;
return 1;
}
const std::string inputFilename = argv[1];
const std::vector<Endpoint> endpoints = parseCSV(inputFilename);
// Generate the Swift enum code
std::cout << "// auto generated using csv_to_swift_endpoint_enum" << std::endl;
std::cout << std::endl;
std::cout << "import AsyncHTTPClient" << std::endl;
std::cout << "import NIOHTTP1" << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << "public enum Endpoint {" << std::endl;
for (const Endpoint &endpoint : endpoints)
{
std::cout << " case " << endpoint.name << std::endl;
}
std::cout << std::endl;
std::cout << " var url: String {" << std::endl;
std::cout << " switch self {" << std::endl;
for (const Endpoint &endpoint : endpoints)
{
std::cout << " case ." << endpoint.name << ": return \"" << endpoint.url << "\"" << std::endl;
}
std::cout << " }" << std::endl;
std::cout << " }" << std::endl;
std::cout << " var httpMethod: HTTPMethod {" << std::endl;
std::cout << " switch self {" << std::endl;
for (const Endpoint &endpoint : endpoints)
{
std::cout << " case ." << endpoint.name << ": return ." << endpoint.method << std::endl;
}
std::cout << " }" << std::endl;
std::cout << " }" << std::endl;
std::cout << "}" << std::endl;
return 0;
}