diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
commit | 6352e8e3196f78388b6c771073f9e03eaa612673 (patch) | |
tree | e23772f79a7fbc41bc9108951e9e136857484bf4 /source/json_tool/json_tool.hpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/json_tool/json_tool.hpp')
-rw-r--r-- | source/json_tool/json_tool.hpp | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/source/json_tool/json_tool.hpp b/source/json_tool/json_tool.hpp new file mode 100644 index 0000000..6f4c337 --- /dev/null +++ b/source/json_tool/json_tool.hpp @@ -0,0 +1,150 @@ +#ifndef JSON_TOOL_HPP +#define JSON_TOOL_HPP + +#include "StarFormattedJson.hpp" +#include "StarJsonPath.hpp" + +namespace Star { + +struct GetCommand { + JsonPath::PathPtr path; + bool opt; + bool children; +}; + +struct SetCommand { + JsonPath::PathPtr path; + FormattedJson value; +}; + +struct AddCommand { + JsonPath::PathPtr path; + FormattedJson value; +}; + +struct RemoveCommand { + JsonPath::PathPtr path; +}; + +struct EditCommand { + JsonPath::PathPtr path; +}; + +typedef MVariant<GetCommand, SetCommand, AddCommand, RemoveCommand, EditCommand> Command; + +struct AtBeginning {}; + +struct AtEnd {}; + +struct BeforeKey { + String key; +}; + +struct AfterKey { + String key; +}; + +typedef MVariant<AtBeginning, AtEnd, BeforeKey, AfterKey> InsertLocation; + +STAR_CLASS(JsonInputFormat); + +class JsonInputFormat { +public: + virtual ~JsonInputFormat() {} + virtual FormattedJson toJson(String const& input) const = 0; + virtual String fromJson(FormattedJson const& json) const = 0; + virtual FormattedJson getDefault() const = 0; +}; + +class GenericInputFormat : public JsonInputFormat { +public: + virtual FormattedJson toJson(String const& input) const override; + virtual String fromJson(FormattedJson const& json) const override; + virtual FormattedJson getDefault() const override; +}; + +class CommaSeparatedStrings : public JsonInputFormat { +public: + virtual FormattedJson toJson(String const& input) const override; + virtual String fromJson(FormattedJson const& json) const override; + virtual FormattedJson getDefault() const override; +}; + +class StringInputFormat : public JsonInputFormat { +public: + virtual FormattedJson toJson(String const& input) const override; + virtual String fromJson(FormattedJson const& json) const override; + virtual FormattedJson getDefault() const override; +}; + +STAR_CLASS(Output); + +class Output { +public: + virtual ~Output() {} + virtual void out(FormattedJson const& json) = 0; + virtual void flush() = 0; + + function<void(FormattedJson const& json)> toFunction() { + return [this](FormattedJson const& json) { this->out(json); }; + } +}; + +class OutputOnSeparateLines : public Output { +public: + virtual void out(FormattedJson const& json) override; + virtual void flush() override; +}; + +class ArrayOutput : public Output { +public: + ArrayOutput(bool unique) : m_unique(unique), m_results() {} + + virtual void out(FormattedJson const& json) override; + virtual void flush() override; + +private: + bool m_unique; + List<FormattedJson> m_results; +}; + +struct Options { + Options() : inPlace(false), insertLocation(), editFormat(nullptr), editorImages(), output() {} + + bool inPlace; + InsertLocation insertLocation; + JsonInputFormatPtr editFormat; + List<JsonPath::PathPtr> editorImages; + OutputPtr output; +}; + +struct JsonLiteralInput { + String json; +}; + +struct FileInput { + String filename; +}; + +struct FindInput { + String directory; + String filenameSuffix; +}; + +typedef MVariant<JsonLiteralInput, FileInput, FindInput> Input; + +struct ParsedArgs { + ParsedArgs() : inputs(), command(), options() {} + + List<Input> inputs; + Command command; + Options options; +}; + +FormattedJson addOrSet(bool add, JsonPath::PathPtr path, FormattedJson const& input, InsertLocation insertLocation, FormattedJson const& value); +String reprWithLineEnding(FormattedJson const& json); +StringList findFiles(FindInput const& findArgs); + +} + +#endif |