osquery-1/osquery/utils/conversions/castvariant.h
Jeremy Calvert c8bb439442 OSQueryd changes for encoding type in JSON syntax
Summary:
Add log_numerics_as_numbers flag.

Internal SQLite query has method that returns QueryDataTyped (instead of QueryData), which are boost::variant<string, double, long long>.

Ints are encoded as such if and only if new log_numerics_as_numbers flag is set to true.

Reviewed By: fmanco

Differential Revision: D13778323

fbshipit-source-id: 7d7bb31781486f63fcc088cd479d3b6f255a5cb4
2019-03-04 09:15:21 -08:00

43 lines
1.0 KiB
C++

/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed in accordance with the terms specified in
* the LICENSE file found in the root directory of this source tree.
*/
#pragma once
#include <boost/lexical_cast.hpp>
#include <string>
namespace osquery {
/* We do this so that we get '0.0' from double 0.0 instead of '0'
*/
class CastVisitor : public boost::static_visitor<std::string> {
public:
std::string operator()(const long long& i) const {
return std::to_string(i);
}
std::string operator()(const double& d) const {
std::string s{boost::lexical_cast<std::string>(d)};
if (s.find('.') == std::string::npos) {
s += ".0";
}
return s;
}
std::string operator()(const std::string& str) const {
return str;
}
};
inline std::string castVariant(
const boost::variant<long long, double, std::string>& var) {
static const CastVisitor visitor;
return boost::apply_visitor(visitor, var);
}
} // namespace osquery