12 switch (value.value_case())
14 case swarmio::data::Variant::ValueCase::kBoolArray:
15 case swarmio::data::Variant::ValueCase::kIntArray:
16 case swarmio::data::Variant::ValueCase::kUintArray:
17 case swarmio::data::Variant::ValueCase::kDoubleArray:
18 case swarmio::data::Variant::ValueCase::kStringArray:
19 case swarmio::data::Variant::ValueCase::kMapArray:
28 switch (value.value_case())
30 case swarmio::data::Variant::ValueCase::kBoolArray:
31 return value.bool_array().elements_size();
33 case swarmio::data::Variant::ValueCase::kIntArray:
34 return value.int_array().elements_size();
36 case swarmio::data::Variant::ValueCase::kUintArray:
37 return value.uint_array().elements_size();
39 case swarmio::data::Variant::ValueCase::kDoubleArray:
40 return value.double_array().elements_size();
42 case swarmio::data::Variant::ValueCase::kStringArray:
43 return value.string_array().elements_size();
45 case swarmio::data::Variant::ValueCase::kMapArray:
46 return value.map_array().elements_size();
55 switch (value.value_case())
57 case swarmio::data::Variant::ValueCase::kBoolArray:
58 return swarmio::data::Variant::ValueCase::kBoolValue;
60 case swarmio::data::Variant::ValueCase::kIntArray:
61 return swarmio::data::Variant::ValueCase::kIntValue;
63 case swarmio::data::Variant::ValueCase::kUintArray:
64 return swarmio::data::Variant::ValueCase::kUintValue;
66 case swarmio::data::Variant::ValueCase::kDoubleArray:
67 return swarmio::data::Variant::ValueCase::kDoubleValue;
69 case swarmio::data::Variant::ValueCase::kStringArray:
70 return swarmio::data::Variant::ValueCase::kStringValue;
72 case swarmio::data::Variant::ValueCase::kMapArray:
73 return swarmio::data::Variant::ValueCase::kMapValue;
76 return value.value_case();
82 discovery::Schema schema;
83 auto& fields = *schema.mutable_fields();
84 for (
const auto& pair : value.pairs())
93 discovery::Field field;
94 switch (value.value_case())
96 case Variant::ValueCase::kBoolValue:
97 case Variant::ValueCase::kBoolArray:
101 case Variant::ValueCase::kIntValue:
102 case Variant::ValueCase::kIntArray:
103 field.set_type(discovery::Type::INT);
106 case Variant::ValueCase::kUintValue:
107 case Variant::ValueCase::kUintArray:
108 field.set_type(discovery::Type::UINT);
111 case Variant::ValueCase::kDoubleValue:
112 case Variant::ValueCase::kDoubleArray:
113 field.set_type(discovery::Type::DOUBLE);
116 case Variant::ValueCase::kStringValue:
117 case Variant::ValueCase::kStringArray:
121 case Variant::ValueCase::kMapValue:
125 case Variant::ValueCase::kMapArray:
126 if (value.map_array().elements_size() > 0)
142 if (includeArraySizes)
144 field.set_fixed_size(
GetCount(value));
148 field.set_is_variable_size(
true);
156 std::ostringstream stream;
163 std::ostringstream stream;
170 std::ostringstream stream;
175 std::string
Helper::ToString(
const google::protobuf::Map<std::string, swarmio::data::Variant>& value,
bool prettyPrint)
177 std::ostringstream stream;
218 if (
'\x00' <= c && c <=
'\x1f')
220 stream <<
"\\u" << std::hex << std::setw(4) << std::setfill(
'0') << (int)c;
248 for (
int i = 0; i <= indentationLevel; ++i)
270 switch (value.value_case())
272 case Variant::ValueCase::kMapArray:
273 for (
const auto& e : value.map_array().elements())
280 case Variant::ValueCase::kStringArray:
281 for (
const auto& e : value.string_array().elements())
288 case Variant::ValueCase::kBoolArray:
289 for (
auto e : value.bool_array().elements())
292 stream << std::boolalpha << e;
296 case Variant::ValueCase::kIntArray:
297 for (
auto e : value.int_array().elements())
304 case Variant::ValueCase::kUintArray:
305 for (
auto e : value.uint_array().elements())
312 case Variant::ValueCase::kDoubleArray:
313 for (
auto e : value.double_array().elements())
328 for (
int i = 0; i < indentationLevel; ++i)
338 switch (value.value_case())
340 case Variant::ValueCase::kMapValue:
341 WriteToStream(stream, value.map_value(), prettyPrint, indentationLevel);
344 case Variant::ValueCase::kStringValue:
348 case Variant::ValueCase::kBoolValue:
349 stream << std::boolalpha << value.bool_value();
352 case Variant::ValueCase::kIntValue:
353 stream << value.int_value();
356 case Variant::ValueCase::kUintValue:
357 stream << value.uint_value();
360 case Variant::ValueCase::kDoubleValue:
361 stream << value.double_value();
372 WriteToStream(stream, value.pairs(), prettyPrint, indentationLevel);
375 void Helper::WriteToStream(std::ostream& stream,
const google::protobuf::Map<std::string, swarmio::data::Variant>& value,
bool prettyPrint,
int indentationLevel)
378 if (value.size() == 0)
391 std::list<std::string> keys;
392 for (
const auto& element : value)
394 keys.push_back(element.first);
400 for (
const auto& key : keys)
416 WriteToStream(stream, value.at(key), prettyPrint, indentationLevel + 1);
423 for (
int i = 0; i < indentationLevel; ++i)
439 case discovery::Type::INT:
442 case discovery::Type::UINT:
445 case discovery::Type::DOUBLE:
456 void Helper::WriteToStream(std::ostream& stream,
const discovery::Schema& value,
bool prettyPrint,
int indentationLevel)
458 if (value.fields_size() == 0)
471 std::list<std::string> keys;
472 for (
const auto& element : value.fields())
474 keys.push_back(element.first);
480 for (
const auto& key : keys)
483 const auto& field = value.fields().at(key);
489 if (field.multiplicity_case() == discovery::Field::MultiplicityCase::kFixedSize)
493 else if (field.is_variable_size())
510 if (field.descriptor_case() == discovery::Field::DescriptorCase::kSchema)
512 WriteToStream(stream, field.schema(), prettyPrint, indentationLevel + 1);
524 for (
int i = 0; i < indentationLevel; ++i)
static Variant::ValueCase GetBaseType(const Variant &value)
Get the base type of the variant array, or its type if it is not an array.
static void WriteToStream(std::ostream &stream, const Variant &value, bool prettyPrint=true, int indentationLevel=0)
Write a string representation of a variant to the stream.
Exception class thrown by all library classes.
static void WriteEscapedAndQuotedStringToStream(std::ostream &stream, const std::string &value)
Write an escaped and quoted string to the stream.
static std::string ToString(const discovery::Schema &value, bool prettyPrint=true)
Get a string representation of a schema.
static void WriteElementSeparatorToStream(std::ostream &stream, bool &first, bool prettyPrint, int indentationLevel)
Write the element separator between two elements of an array.
static const char * TypeToString(discovery::Type type)
Look up the human readable name of a type.
static size_t GetCount(const Variant &value)
Get the item count of the variant array, or 1 if it is not an array.
static bool IsArray(const Variant &value)
Check whether the variant contains an array type.
static discovery::Field GetFieldDescriptor(const Variant &value, bool includeArraySizes=false)
Get the schema for an existing map.
static discovery::Schema GetSchemaDescriptor(const Map &value, bool includeArraySizes=false)
Get the schema for an existing map.