/*
 * Copyright 2015 - 2022 gary@drinkingtea.net
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include "write.hpp"

namespace ox {

OrganicClawWriter::OrganicClawWriter(int unionIdx) noexcept: m_unionIdx(unionIdx) {
}

OrganicClawWriter::OrganicClawWriter(Json::Value json, int unionIdx) noexcept:
	m_json(json),
	m_unionIdx(unionIdx) {
}

Error OrganicClawWriter::field(const char *key, SerStr val) noexcept {
	if (targetValid() && val.len()) {
		value(key) = val.c_str();
	}
	++m_fieldIt;
	return OxError(0);
}

Error OrganicClawWriter::fieldCString(const char *key, const char *const*val, int len) noexcept {
	if (targetValid() && len) {
		value(key) = *val;
	}
	++m_fieldIt;
	return OxError(0);
}

Error OrganicClawWriter::fieldCString(const char *key, char **val, int len) noexcept {
	return fieldCString(key, const_cast<const char**>(val), len);
}

Error OrganicClawWriter::fieldCString(const char *key, const char *const*val) noexcept {
	return fieldCString(key, const_cast<const char**>(val), {});
}

Error OrganicClawWriter::fieldCString(const char *key, char **val) noexcept {
	return fieldCString(key, const_cast<const char**>(val), {});
}

Json::Value &OrganicClawWriter::value(const char *key) noexcept {
	if (m_json.isArray()) {
		return m_json[m_fieldIt];
	} else {
		return m_json[key];
	}
}

}
