diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-03-05 06:04:04 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-03-05 12:16:43 +0000 |
commit | f1c2464fd83e6623000ad6eb59272b189fffb78e (patch) | |
tree | 55288880cd7f6a05125079aaa432c5740fae28ef /staging_vespalib/src | |
parent | ede6354f72951bde6247519e03d6445d3f155985 (diff) |
Transform to slime.
Diffstat (limited to 'staging_vespalib/src')
3 files changed, 117 insertions, 0 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt index 12970a7d39b..f3afda94fe6 100644 --- a/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt +++ b/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt @@ -5,6 +5,7 @@ vespa_add_library(staging_vespalib_vespalib_objects OBJECT namedobject.cpp objectvisitor.cpp objectdumper.cpp + object2slime.cpp visit.cpp objectpredicate.cpp objectoperation.cpp diff --git a/staging_vespalib/src/vespa/vespalib/objects/object2slime.cpp b/staging_vespalib/src/vespa/vespalib/objects/object2slime.cpp new file mode 100644 index 00000000000..f06060529c3 --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/objects/object2slime.cpp @@ -0,0 +1,75 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include "object2slime.h" +#include <vespa/vespalib/util/stringfmt.h> +#include <vespa/vespalib/data/slime/cursor.h> + + +namespace vespalib { + +Object2Slime::Object2Slime(slime::Cursor & cursor) + : _cursor(&cursor), + _stack() +{ +} + +Object2Slime::~Object2Slime() = default; + +//----------------------------------------------------------------------------- + +void +Object2Slime::openStruct(const vespalib::string &name, const vespalib::string &type) +{ + _stack.push_back(_cursor); + + if (name.empty()) { + _cursor = & _cursor->setObject(type); + } else { + _cursor = & _cursor->setObject(name); + _cursor->setString("[type]", type); + } +} + +void +Object2Slime::closeStruct() +{ + _cursor = _stack.back(); + _stack.pop_back(); +} + +void +Object2Slime::visitBool(const vespalib::string &name, bool value) +{ + _cursor->setBool(name, value); +} + +void +Object2Slime::visitInt(const vespalib::string &name, int64_t value) +{ + _cursor->setLong(name, value); +} + +void +Object2Slime::visitFloat(const vespalib::string &name, double value) +{ + _cursor->setDouble(name, value); +} + +void +Object2Slime::visitString(const vespalib::string &name, const vespalib::string &value) +{ + _cursor->setString(name, value); +} + +void +Object2Slime::visitNull(const vespalib::string &name) +{ + _cursor->setNix(name); +} + +void +Object2Slime::visitNotImplemented() +{ + _cursor->setNix("not_implemented"); +} + +} // namespace vespalib diff --git a/staging_vespalib/src/vespa/vespalib/objects/object2slime.h b/staging_vespalib/src/vespa/vespalib/objects/object2slime.h new file mode 100644 index 00000000000..9c147047a74 --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/objects/object2slime.h @@ -0,0 +1,41 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include "objectvisitor.h" +#include <vector> + +namespace vespalib { + +namespace slime { class Cursor; } + +/** + * This is a concrete object visitor that will build up a structured + * human-readable string representation of an object. + **/ +class Object2Slime : public ObjectVisitor +{ +private: + slime::Cursor * _cursor; + std::vector<slime::Cursor *> _stack; + +public: + /** + * Create an object dumper with the given indent size; default is + * 4 spaces per indent level. + * + * @param indent indent size in number of spaces + **/ + Object2Slime(slime::Cursor & cursor); + ~Object2Slime(); + + void openStruct(const vespalib::string &name, const vespalib::string &type) override; + void closeStruct() override; + void visitBool(const vespalib::string &name, bool value) override; + void visitInt(const vespalib::string &name, int64_t value) override; + void visitFloat(const vespalib::string &name, double value) override; + void visitString(const vespalib::string &name, const vespalib::string &value) override; + void visitNull(const vespalib::string &name) override; + void visitNotImplemented() override; +}; + +} // namespace vespalib |