Decouple parser from CommitRequest
This commit is contained in:
22
src/main.cpp
22
src/main.cpp
@@ -1,5 +1,6 @@
|
||||
#include "commit_request.hpp"
|
||||
#include "config.hpp"
|
||||
#include "json_commit_request_parser.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void print_stats(const CommitRequest &request) {
|
||||
@@ -63,6 +64,7 @@ int main(int argc, char *argv[]) {
|
||||
std::cout << "\n--- CommitRequest Demo ---" << std::endl;
|
||||
|
||||
CommitRequest request;
|
||||
JsonCommitRequestParser parser;
|
||||
|
||||
const std::string sample_json = R"({
|
||||
"request_id": "demo-12345",
|
||||
@@ -85,7 +87,7 @@ int main(int argc, char *argv[]) {
|
||||
})";
|
||||
auto copy = sample_json;
|
||||
|
||||
if (request.parse_json(copy.data(), copy.size())) {
|
||||
if (parser.parse(request, copy.data(), copy.size())) {
|
||||
print_stats(request);
|
||||
} else {
|
||||
std::cout << "✗ Failed to parse commit request" << std::endl;
|
||||
@@ -95,8 +97,9 @@ int main(int argc, char *argv[]) {
|
||||
std::cout << "\n--- Streaming Parse Demo ---" << std::endl;
|
||||
|
||||
CommitRequest streaming_request;
|
||||
JsonCommitRequestParser streaming_parser;
|
||||
|
||||
if (streaming_request.begin_streaming_parse()) {
|
||||
if (streaming_parser.begin_streaming_parse(streaming_request)) {
|
||||
std::cout << "✓ Initialized streaming parser" << std::endl;
|
||||
|
||||
// Simulate receiving data in small chunks like from a network socket
|
||||
@@ -106,10 +109,11 @@ int main(int argc, char *argv[]) {
|
||||
size_t offset = 0;
|
||||
int chunk_count = 0;
|
||||
|
||||
CommitRequest::ParseStatus status = CommitRequest::ParseStatus::Incomplete;
|
||||
CommitRequestParser::ParseStatus status =
|
||||
CommitRequestParser::ParseStatus::Incomplete;
|
||||
|
||||
while (offset < copy.size() &&
|
||||
status == CommitRequest::ParseStatus::Incomplete) {
|
||||
status == CommitRequestParser::ParseStatus::Incomplete) {
|
||||
size_t len = std::min(chunk_size, copy.size() - offset);
|
||||
std::string chunk = copy.substr(offset, len);
|
||||
|
||||
@@ -118,18 +122,18 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
// Need mutable data for weaseljson
|
||||
std::string mutable_chunk = chunk;
|
||||
status = streaming_request.parse_chunk(mutable_chunk.data(),
|
||||
mutable_chunk.size());
|
||||
status = streaming_parser.parse_chunk(
|
||||
streaming_request, mutable_chunk.data(), mutable_chunk.size());
|
||||
|
||||
offset += len;
|
||||
}
|
||||
|
||||
if (status == CommitRequest::ParseStatus::Incomplete) {
|
||||
if (status == CommitRequestParser::ParseStatus::Incomplete) {
|
||||
std::cout << " Finalizing parse..." << std::endl;
|
||||
status = streaming_request.finish_streaming_parse();
|
||||
status = streaming_parser.finish_streaming_parse(streaming_request);
|
||||
}
|
||||
|
||||
if (status == CommitRequest::ParseStatus::Complete) {
|
||||
if (status == CommitRequestParser::ParseStatus::Complete) {
|
||||
print_stats(streaming_request);
|
||||
} else {
|
||||
std::cout << "✗ Streaming parse failed" << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user