#ifndef GOOGLE_PROTOBUF_UTIL_INTERNAL_DEFAULT_VALUE_OBJECTWRITER_H__
#define GOOGLE_PROTOBUF_UTIL_INTERNAL_DEFAULT_VALUE_OBJECTWRITER_H__
#include <cstdint>
#include <functional>
#include <memory>
#include <stack>
#include <vector>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/util/internal/datapiece.h>
#include <google/protobuf/util/internal/object_writer.h>
#include <google/protobuf/util/internal/type_info.h>
#include <google/protobuf/util/internal/utility.h>
#include <google/protobuf/util/type_resolver.h>
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace util {
namespace converter {
class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
public:
typedef std::function<bool(
const std::vector<std::string>& ,
const google::protobuf::Field* )>
FieldScrubCallBack;
DefaultValueObjectWriter(TypeResolver* type_resolver,
const google::protobuf::Type& type,
ObjectWriter* ow);
~DefaultValueObjectWriter() override;
DefaultValueObjectWriter* StartObject(StringPiece name) override;
DefaultValueObjectWriter* EndObject() override;
DefaultValueObjectWriter* StartList(StringPiece name) override;
DefaultValueObjectWriter* EndList() override;
DefaultValueObjectWriter* RenderBool(StringPiece name,
bool value) override;
DefaultValueObjectWriter* RenderInt32(StringPiece name,
int32_t value) override;
DefaultValueObjectWriter* RenderUint32(StringPiece name,
uint32_t value) override;
DefaultValueObjectWriter* RenderInt64(StringPiece name,
int64_t value) override;
DefaultValueObjectWriter* RenderUint64(StringPiece name,
uint64_t value) override;
DefaultValueObjectWriter* RenderDouble(StringPiece name,
double value) override;
DefaultValueObjectWriter* RenderFloat(StringPiece name,
float value) override;
DefaultValueObjectWriter* RenderString(StringPiece name,
StringPiece value) override;
DefaultValueObjectWriter* RenderBytes(StringPiece name,
StringPiece value) override;
DefaultValueObjectWriter* RenderNull(StringPiece name) override;
void RegisterFieldScrubCallBack(FieldScrubCallBack field_scrub_callback);
void set_suppress_empty_list(bool value) { suppress_empty_list_ = value; }
void set_preserve_proto_field_names(bool value) {
preserve_proto_field_names_ = value;
}
void set_print_enums_as_ints(bool value) { use_ints_for_enums_ = value; }
protected:
enum NodeKind {
PRIMITIVE = 0,
OBJECT = 1,
LIST = 2,
MAP = 3,
};
class PROTOBUF_EXPORT Node {
public:
Node(const std::string& name, const google::protobuf::Type* type,
NodeKind kind, const DataPiece& data, bool is_placeholder,
const std::vector<std::string>& path, bool suppress_empty_list,
bool preserve_proto_field_names, bool use_ints_for_enums,
FieldScrubCallBack field_scrub_callback);
virtual ~Node() {
for (size_t i = 0; i < children_.size(); ++i) {
delete children_[i];
}
}
void AddChild(Node* child) { children_.push_back(child); }
Node* FindChild(StringPiece name);
virtual void PopulateChildren(const TypeInfo* typeinfo);
virtual void WriteTo(ObjectWriter* ow);
const std::string& name() const { return name_; }
const std::vector<std::string>& path() const { return path_; }
const google::protobuf::Type* type() const { return type_; }
void set_type(const google::protobuf::Type* type) { type_ = type; }
NodeKind kind() const { return kind_; }
int number_of_children() const { return children_.size(); }
void set_data(const DataPiece& data) { data_ = data; }
bool is_any() const { return is_any_; }
void set_is_any(bool is_any) { is_any_ = is_any; }
void set_is_placeholder(bool is_placeholder) {
is_placeholder_ = is_placeholder;
}
protected:
const google::protobuf::Type* GetMapValueType(
const google::protobuf::Type& found_type, const TypeInfo* typeinfo);
void WriteChildren(ObjectWriter* ow);
std::string name_;
const google::protobuf::Type* type_;
NodeKind kind_;
bool is_any_;
DataPiece data_;
std::vector<Node*> children_;
bool is_placeholder_;
std::vector<std::string> path_;
bool suppress_empty_list_;
bool preserve_proto_field_names_;
bool use_ints_for_enums_;
FieldScrubCallBack field_scrub_callback_;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node);
};
virtual Node* CreateNewNode(const std::string& name,
const google::protobuf::Type* type, NodeKind kind,
const DataPiece& data, bool is_placeholder,
const std::vector<std::string>& path,
bool suppress_empty_list,
bool preserve_proto_field_names,
bool use_ints_for_enums,
FieldScrubCallBack field_scrub_callback);
static DataPiece CreateDefaultDataPieceForField(
const google::protobuf::Field& field, const TypeInfo* typeinfo) {
return CreateDefaultDataPieceForField(field, typeinfo, false);
}
static DataPiece CreateDefaultDataPieceForField(
const google::protobuf::Field& field, const TypeInfo* typeinfo,
bool use_ints_for_enums);
protected:
Node* current() { return current_; }
private:
void MaybePopulateChildrenOfAny(Node* node);
void WriteRoot();
void RenderDataPiece(StringPiece name, const DataPiece& data);
static DataPiece FindEnumDefault(const google::protobuf::Field& field,
const TypeInfo* typeinfo,
bool use_ints_for_enums);
const TypeInfo* typeinfo_;
bool own_typeinfo_;
const google::protobuf::Type& type_;
std::vector<std::unique_ptr<std::string>> string_values_;
Node* current_;
std::unique_ptr<Node> root_;
std::stack<Node*> stack_;
bool suppress_empty_list_;
bool preserve_proto_field_names_;
bool use_ints_for_enums_;
FieldScrubCallBack field_scrub_callback_;
ObjectWriter* ow_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultValueObjectWriter);
};
} } } }
#include <google/protobuf/port_undef.inc>
#endif