bool operator==(const value& lhs, const value& rhs) { if (lhs.type() != rhs.type()) { return false; } switch (static_cast<int>(lhs.type())) { #define BSONCXX_ENUM(type, val) \ case val: \ return lhs.get_##type() == rhs.get_##type(); #include <bsoncxx/enums/type.hpp> #undef BSONCXX_ENUM } BSONCXX_UNREACHABLE; }
/// Like coerce(const value&) but assigns the value to a reference /// instead of returning it. May be more efficient for complex values /// (arrays, maps, etc.) /// /// @related proton::value template<class T> void coerce(const value& v, T& x) { codec::decoder d(v, false); if (type_id_is_scalar(v.type())) { scalar s; d >> s; x = internal::coerce<T>(s); } else {
static bool can_convert_from(const value& val) { if (val.type() != value_type::vector_type) { return false; } const auto& vec = val.get<value_vector_type>(); return vec.size() == sizeof...(T) && tuple_value_converter<T...>::types_match(vec, 0); }
static MetadataDefiniton get( value const &v ) { MetadataDefiniton m; if( v.type( ) != is_object) { throw bad_value_cast( ); } m.name = v.get<std::string>( "name" ); m.type = v.get<std::string>( "type" ); return m; }
static PostingJoinOperatorConfiguration get( value const &v ) { PostingJoinOperatorConfiguration c; if( v.type( ) != is_object ) { throw bad_value_cast( ); } c.name = v.get<std::string>( "name" ); c.description = v.get<std::string>( "description" ); return c; }
static DocumentGetRequest get( value const &v ) { DocumentGetRequest d; if( v.type( ) != is_object) { throw bad_value_cast( ); } // TODO: should we also accept int, float? how can we fall back? d.docid = v.get<std::string>( "docid", "" ); return d; }
static StorageConfiguration get( value const &v ) { StorageConfiguration c; if( v.type( ) != is_object) { throw bad_value_cast( ); } c.metadata = v.get<std::vector<struct MetadataDefiniton> >( "metadata", std::vector<struct MetadataDefiniton>( ) ); c.attributes = v.get<std::vector<std::string> >( "attributes", std::vector<std::string>( ) ); c.types = v.get<std::vector<std::string> >( "types", std::vector<std::string>( ) ); return c; }
static SummarizerFunctionConfiguration get( value const &v ) { SummarizerFunctionConfiguration c; if( v.type( ) != is_object ) { throw bad_value_cast( ); } c.name = v.get<std::string>( "name" ); c.description = v.get<std::string>( "description" ); c.parameter = v.get<std::vector<FunctionParameter> >( "parameter" ); return c; }
static ServiceConfiguration get( value const &v ) { ServiceConfiguration c; if( v.type( ) != is_object ) { throw bad_value_cast( ); } c.weighting_functions = v.get<std::vector<WeightingFunctionConfiguration> >( "weighting_functions", std::vector<WeightingFunctionConfiguration>( ) ); c.summarizer_functions = v.get<std::vector<SummarizerFunctionConfiguration> >( "summarizer_functions", std::vector<SummarizerFunctionConfiguration>( ) ); c.posting_join_operators = v.get<std::vector<PostingJoinOperatorConfiguration> >( "posting_join_operators", std::vector<PostingJoinOperatorConfiguration>( ) ); return c; }
static DocumentGetAnswer get( value const &v ) { DocumentGetAnswer a; if( v.type( ) != is_object) { throw bad_value_cast( ); } a.docno = v.get<strus::Index>( "docno" ); a.attributes = v.get<std::vector<std::pair<std::string, std::string> > >( "attributes" ); a.metadata = v.get<std::vector<std::pair<std::string, strus::NumericVariant> > >( "metadata" ); a.forward = v.get<std::vector<boost::tuple<std::string, std::string, strus::Index> > >( "forward" ); a.search = v.get<std::vector<boost::tuple<std::string, std::string, strus::Index> > >( "search" ); return a; }
static DocumentInsertRequest get( value const &v ) { DocumentInsertRequest d; if( v.type( ) != is_object) { throw bad_value_cast( ); } // TODO: should we also accept int, float? how can we fall back? d.docid = v.get<std::string>( "docid", "" ); d.attributes = v.get<std::vector<std::pair<std::string, std::string> > >( "attributes" ); d.metadata = v.get<std::vector<std::pair<std::string, strus::NumericVariant> > >( "metadata" ); d.forward = v.get<std::vector<boost::tuple<std::string, std::string, strus::Index> > >( "forward" ); d.search = v.get<std::vector<boost::tuple<std::string, std::string, strus::Index> > >( "search" ); return d; }
static boost::tuple<std::string, std::string, strus::Index> get( value const &v ) { boost::tuple<std::string, std::string, strus::Index> t; if( v.type( ) != is_object) { throw bad_value_cast( ); } t = boost::make_tuple( v.get<std::string>( "type" ), v.get<std::string>( "value" ), v.get<strus::Index>( "pos" ) ); return t; }
static void GenStat(Stat& stat, const value& v) { switch (v.type()) { case value_type::array: { const array& a = array_cast(v); for (array::const_iterator itr = a.begin(); itr != a.end(); ++itr) GenStat(stat, *itr); stat.arrayCount++; stat.elementCount += a.size(); } break; case value_type::object: { const object& o = object_cast(v); for (object::const_iterator itr = o.begin(); itr != o.end(); ++itr) { GenStat(stat, itr->second); stat.stringLength += itr->first.size(); } stat.objectCount++; stat.memberCount += o.size(); stat.stringCount += o.size(); // member names } break; case value_type::string: stat.stringCount++; stat.stringLength += string_cast(v).length(); break; case value_type::number: stat.numberCount++; break; case value_type::boolean: if (bool_cast(v)) stat.trueCount++; else stat.falseCount++; break; case value_type::null: stat.nullCount++; break; } }
static StorageCreateParameters get( value const &v ) { StorageCreateParameters p; if( v.type( ) != is_object) { throw bad_value_cast( ); } p.database = v.get<std::string>( "database", DEFAULT_DATABASE ); p.compression = v.get<bool>( "compression", LEVELDB_DATABASE_DEFAULT_COMPRESSION ); // TODO: we should introspect them via the database configuration for // the specific implementation p.cache_size = v.get<size_t>( "cache_size", LEVELDB_DATABASE_DEFAULT_LRU_CACHESIZE ); p.max_open_files = v.get<size_t>( "max_open_files", LEVELDB_DATABASE_DEFAULT_MAX_OPEN_FILES ); p.write_buffer_size = v.get<size_t>( "write_buffer_size", LEVELDB_DATABASE_DEFAULT_WRITE_BUFFER_SIZE ); p.block_size = v.get<size_t>( "block_size", LEVELDB_DATABASE_DEFAULT_BLOCK_SIZE ); p.metadata = v.get<std::vector<struct MetadataDefiniton> >( "metadata", std::vector<struct MetadataDefiniton>( ) ); return p; }
void write_value(scanner& sc, writer& wr, const value& val) { switch (val.type()) { case value_type::number_type: { read_value(sc); wr.append(stringize_number(val.number_value())); break; } case value_type::string_type: { read_value(sc); wr.append("\""); wr.append(escape_string(val.string_value())); wr.append("\""); break; } case value_type::bool_type: { read_value(sc); wr.append(val.bool_value() ? "true" : "false"); break; } case value_type::group_type: { if (sc.peek_token().is_char('{')) { write_group(sc, wr, true, val.group_value()); } else { read_value(sc); scanner dummy_scanner = make_scanner("{}"); write_group(dummy_scanner, wr, true, val.group_value()); } break; } case value_type::vector_type: { if (sc.peek_token().is_char('[')) { write_vector(sc, wr, val.vector_value()); } else { read_value(sc); scanner dummy_scanner = make_scanner("[]"); write_vector(dummy_scanner, wr, val.vector_value()); } break; } default: { break; } } }
static std::pair<std::string, strus::NumericVariant> get( value const &v ) { std::pair<std::string, strus::NumericVariant> p; if( v.type( ) != is_object) { throw bad_value_cast( ); } p.first = v.get<std::string>( "key" ); value val = v["value"]; switch( val.type( ) ) { case is_boolean: // TODO: really? Do we allow this? p.second = ( v.get<bool>( "value" ) ) ? 1 : 0; break; case is_string: // TODO: really? Do we allow this? case is_number: if( is_of_type( val, p.second.variant.Int ) ) { p.second = v.get<BOOST_TYPEOF( p.second.variant.Int )>( "value" ); } else if( is_of_type( val, p.second.variant.UInt ) ) { p.second = v.get<BOOST_TYPEOF( p.second.variant.UInt )>( "value" ); } else if( is_of_type( val, p.second.variant.Float ) ) { p.second = v.get<BOOST_TYPEOF( p.second.variant.Float )>( "value" ); } else { throw bad_value_cast( ); } break; case is_undefined: case is_null: // TODO: how do we map absence case is_object: case is_array: default: throw bad_value_cast( ); } return p; }
int value_length(const value& val, int wrap_length) { switch (val.type()) { case value_type::number_type: { return stringize_number(val.number_value()).size(); } case value_type::string_type: { return 2 + escape_string(val.string_value()).size(); } case value_type::bool_type: { return val.bool_value() ? 4 : 5; } case value_type::group_type: { return group_length(val.group_value()); } case value_type::vector_type: { return vector_length(val.vector_value()); } default: { return 0; } } }
static FunctionParameter get( value const &v ) { FunctionParameter p; if( v.type( ) != is_object ) { throw bad_value_cast( ); } std::string s = v.get<std::string>( "type" ); if( s.compare( "feature" ) == 0 ) { p.type = FUNCTION_TYPE_FEATURE; } else if( s.compare( "attribute" ) == 0 ) { p.type = FUNCTION_TYPE_ATTRIBUTE; } else if( s.compare( "metadata" ) == 0 ) { p.type = FUNCTION_TYPE_METADATA; } else if( s.compare( "numeric" ) == 0 ) { p.type = FUNCTION_TYPE_NUMERIC; } else if( s.compare( "string" ) == 0 ) { p.type = FUNCTION_TYPE_STRING; } else { throw bad_value_cast( ); } p.name = v.get<std::string>( "name" ); p.description = v.get<std::string>( "description" ); return p; }
bool sqlitehandle::query (const string &sql, value &into, const statstring &indexby) { sqlite3_stmt *qhandle; int qres; int rowcount=0; int colcount; int i; statstring curidx; bool done = false; into.clear (); if (sqlite3_prepare (hdl, sql.str(), sql.strlen(), &qhandle, 0) != SQLITE_OK) { errcode = 1; errstr = "Could not prepare: %s" %format (sqlite3_errmsg(hdl)); return false; } if (! (qres = sqlite3_step (qhandle))) { errcode = 1; errstr = "Error making first step: %s" %format (sqlite3_errmsg(hdl)); sqlite3_finalize (qhandle); return false; } colcount = sqlite3_column_count (qhandle); if (colcount == 0) { into("rowschanged") = sqlite3_changes (hdl); sqlite3_finalize (qhandle); return true; } statstring colnames[colcount]; value colidx; for (i=0; i<colcount; ++i) { colnames[i] = sqlite3_column_name (qhandle, i); colidx[colnames[i]] = i; } int indexfield = -1; if (colidx.exists (indexby)) indexfield = colidx[indexby]; if (! done) do { switch (qres) { case SQLITE_BUSY: sleep (1); sqlite3_reset(qhandle); continue; case SQLITE_MISUSE: // achtung, fallthrough case SQLITE_ERROR: errcode = 1; errstr = "Error in sqlite3_step: %s" %format (sqlite3_errmsg(hdl)); done = true; break; case SQLITE_DONE: done = true; break; case SQLITE_ROW: { if (indexfield>=0) { curidx = sqlite3_column_text (qhandle, indexfield); } value &myrow = (indexfield<0) ? into.newval() : into[curidx]; for (int i=0; i<colcount; i++) { int ctype = sqlite3_column_type (qhandle, i); statstring &curcol = colnames[i]; switch (ctype) { case SQLITE_INTEGER: myrow[curcol] = sqlite3_column_int (qhandle, i); break; case SQLITE_FLOAT: myrow[curcol] = sqlite3_column_double (qhandle, i); break; case SQLITE_BLOB: // FIXME: use sqlite3_column_blob case SQLITE_TEXT: myrow[curcol] = sqlite3_column_text (qhandle, i); break; default: break; } } } break; } rowcount++; } while ((qres = sqlite3_step (qhandle)) && !done); int finalize_result = sqlite3_finalize (qhandle); if (finalize_result != SQLITE_OK && finalize_result != SQLITE_SCHEMA) { errcode = 1; errstr = "Error finalizing: %s" %format (sqlite3_errmsg(hdl)); return false; } into ("insertid") = sqlite3_last_insert_rowid (hdl); into.type (t_dict); string tmp = into.tojson(); return true; }
value flatten(value const& v) { return {flatten(v.data()), flatten(v.type())}; }
/// Type-safe factory function to construct an event from an unchecked value. /// @param v The value to check and convert into an event. /// @returns A valid event according *v* if `v.type().check(v.data())`. static event make(value v) { return type_check(v.type(), v.data()) ? event{std::move(v)} : caf::none; }
static bool can_convert_from(const value& val) { return val.type() == value_type::vector_type; }