Skip to content

Commit

Permalink
fixup! setValue shouldn't write only strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcomets committed Apr 20, 2018
1 parent 2d6b197 commit 4f9ad53
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions zipkin_opentracing/src/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,49 +79,59 @@ static std::string toJson(const Value &value) {

namespace {

struct ToStringValueVisitor {
struct SetValueVisitor {
BinaryAnnotation& annotation;
const Value &original_value;

std::string operator()(bool value) const {
return std::to_string(value);
// natively handled types

void operator()(bool value) const {
annotation.setValue(value);
}

std::string operator()(double value) const {
return std::to_string(value);
void operator()(double value) const {
annotation.setValue(value);
}

std::string operator()(int64_t value) const {
return std::to_string(value);
void operator()(int64_t value) const {
annotation.setValue(value);
}

std::string operator()(uint64_t value) const {
// There's no uint64_t value type so cast to an int64_t.
return std::to_string(value);
void operator()(const std::string &s) const {
annotation.setValue(s);
}

std::string operator()(const std::string &s) const { return s; }
// overrides

std::string operator()(std::nullptr_t) const { return "0"; }
void operator()(uint64_t value) const {
// There's no uint64_t value type so cast to an int64_t.
(*this)(static_cast<int64_t>(value));
}

std::string operator()(const char *s) const { return s; }
void operator()(std::nullptr_t) const {
(*this)("0");
}

void operator()(const char *s) const {
(*this)(std::string(s));
}

std::string operator()(const Values & /*unused*/) const {
return toJson(original_value);
void operator()(const Values & /*unused*/) const {
(*this)(toJson(original_value));
}

std::string operator()(const Dictionary & /*unused*/) const {
return toJson(original_value);
void operator()(const Dictionary & /*unused*/) const {
(*this)(toJson(original_value));
}
};

} // anonymous namespace

BinaryAnnotation toBinaryAnnotation(string_view key, const Value &value) {
ToStringValueVisitor value_visitor{value};
const std::string str = apply_visitor(value_visitor, value);
BinaryAnnotation annotation;
annotation.setKey(key);
annotation.setValue(str);
SetValueVisitor visitor{annotation, value};
apply_visitor(visitor, value);
return annotation;
}

Expand Down

0 comments on commit 4f9ad53

Please sign in to comment.