src/DeserializeHandler.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Apr 2022 00:12:47 +0200
branchv_0
changeset 2 d586db80b6cd
parent 1 d53041bb781b
permissions -rw-r--r--
use common hex function, read value from the 'data' attribute

/**
 * 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"
#include "UnionAllHandler.h"
#include "Hex.h"

namespace relpipe {
namespace tr {
namespace deserialize {

class DeserializeHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
private:
	Configuration configuration;
	shared_ptr<relpipe::writer::RelationalWriter> writer;
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor;
	UnionAllHandler unionAllHandler;

	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;
		size_t attributeIndex = 0;
	} recordContext;
	
public:

	DeserializeHandler(shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration), unionAllHandler(writer, configuration) {
		// TODO: configurable relation name?
		// TODO: configurable attribute name?
		// TODO: optional custom attributes with constant value or ordinal number?
		// TODO: optional deserialization of only certain relations? and certain fields?
		// TODO: optional pass-through of certain relations?
	}

	virtual ~DeserializeHandler() = 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 relpipe::common::type::StringX& value) override {
		auto attributeName = relationContext.readerMetadata[recordContext.attributeIndex].getAttributeName();

		if (recordContext.attributeIndex == 0) recordContext = RecordContext();

		if (attributeName == L"data") recordContext.buffer = Hex::fromHex(value);  // TODO: skip this hex-decoding once we have octet-string data type + typed values instead of strings

		recordContext.attributeIndex++;

		if (recordContext.attributeIndex % relationContext.readerMetadata.size() == 0) {
			// TODO: skip empty or invalid values?
			std::shared_ptr<relpipe::reader::RelationalReader> reader(relpipe::reader::Factory::create(recordContext.buffer));
			reader->addHandler(&unionAllHandler);
			reader->process();
			recordContext.attributeIndex = 0;
		}
	}

	void endOfPipe() {
	}

};

}
}
}