typed values instead of mere strings in the handler + check number of attributes at the end
/**
* Relational pipes
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <regex>
#include <stdexcept>
#include <sstream>
#include <codecvt>
#include <iomanip>
#include <relpipe/common/type/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/cli/RelpipeCLIException.h>
#include "Configuration.h"
namespace relpipe {
namespace tr {
namespace serialize {
class SerializeHandler : public relpipe::reader::handlers::RelationalReaderValueHandler {
private:
Configuration configuration;
shared_ptr<relpipe::writer::RelationalWriter> writer;
std::wstring_convert<codecvt_utf8<wchar_t>> convertor;
class RelationContext {
public:
relpipe::common::type::StringX name;
std::vector<relpipe::reader::handlers::AttributeMetadata> readerMetadata;
std::vector<relpipe::writer::AttributeMetadata> writerMetadata;
} relationContext;
class RecordContext {
public:
std::stringstream buffer;
shared_ptr<relpipe::writer::RelationalWriter> writer;
size_t attributeIndex = 0;
} recordContext;
relpipe::common::type::StringX toHex(const std::string& octets) {
std::stringstream hex;
hex << std::hex << std::setfill('0') << std::hex;
for (size_t i = 0, size = octets.size(); i < size; i++) hex << std::setw(2) << (0xff & octets[i]);
return convertor.from_bytes(hex.str());
}
public:
SerializeHandler(shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) {
// TODO: configurable relation name
// TODO: configurable attribute name
// TODO: optional custom attributes with constant value or ordinal number
// TODO: optional serialization of only certain relations? and certain fields?
// TODO: optional pass-through of certain relations?
// TODO: multiple modes? one output record per a) one input record, b) several input records, c) one input relation, … ?
writer->startRelation(L"message",{
{L"data", relpipe::writer::TypeId::STRING} // TODO: octet-string (when supported) instead of HEX
}, true);
}
virtual ~SerializeHandler() = default;
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
relationContext = RelationContext();
relationContext.name = name;
relationContext.readerMetadata = attributes;
for (relpipe::reader::handlers::AttributeMetadata readerMetadata : attributes) {
relationContext.writerMetadata.push_back({readerMetadata.getAttributeName(), writer->toTypeId(readerMetadata.getTypeName())});
}
}
void attribute(const void* value, const std::type_info& type) override {
if (recordContext.attributeIndex == 0) {
recordContext = RecordContext();
recordContext.writer.reset(relpipe::writer::Factory::create(recordContext.buffer));
recordContext.writer->startRelation(relationContext.name, relationContext.writerMetadata, true);
}
recordContext.writer->writeAttribute(value, type);
recordContext.attributeIndex++;
if (recordContext.attributeIndex % relationContext.readerMetadata.size() == 0) {
writer->writeAttribute(toHex(recordContext.buffer.str()));
recordContext.attributeIndex = 0;
}
}
void endOfPipe() {
if (recordContext.attributeIndex != 0) throw relpipe::reader::RelpipeReaderException(L"Invalid number of attributes at the end of the pipe – incomplete record.");
}
};
}
}
}