diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2019-04-11 15:20:08 -0400 |
---|---|---|
committer | Billy Donahue <billy.donahue@mongodb.com> | 2019-04-15 15:51:17 -0400 |
commit | 3a5d5e26360584360708f75ad0138155a366c1c8 (patch) | |
tree | 0a6c8f1997be6dead9dd3148da18ec57c7a55ac0 /src/mongo | |
parent | 1ca6c3974e68fb6bea161fa5bfa8ecffeb909c54 (diff) | |
download | mongo-3a5d5e26360584360708f75ad0138155a366c1c8.tar.gz |
SERVER-40621 Add fmt::formatter<mongo::HostAndPort>
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/util/net/hostandport.cpp | 24 | ||||
-rw-r--r-- | src/mongo/util/net/hostandport.h | 21 | ||||
-rw-r--r-- | src/mongo/util/net/hostandport_test.cpp | 20 |
3 files changed, 57 insertions, 8 deletions
diff --git a/src/mongo/util/net/hostandport.cpp b/src/mongo/util/net/hostandport.cpp index 5c30ef22aa8..d81a81fab36 100644 --- a/src/mongo/util/net/hostandport.cpp +++ b/src/mongo/util/net/hostandport.cpp @@ -110,18 +110,30 @@ std::string HostAndPort::toString() const { return ss.str(); } -void HostAndPort::append(StringBuilder& ss) const { +template <typename SinkFunc> +static void appendGeneric(const HostAndPort& hp, const SinkFunc& write) { // wrap ipv6 addresses in []s for roundtrip-ability - if (host().find(':') != std::string::npos) { - ss << '[' << host() << ']'; + if (hp.host().find(':') != std::string::npos) { + write("["); + write(hp.host()); + write("]"); } else { - ss << host(); + write(hp.host()); } - if (host().find('/') == std::string::npos) { - ss << ':' << port(); + if (hp.host().find('/') == std::string::npos) { + write(":"); + write(hp.port()); } } +void HostAndPort::append(StringBuilder& sink) const { + appendGeneric(*this, [&sink](const auto& v) { sink << v; }); +} + +void HostAndPort::append(fmt::writer& sink) const { + appendGeneric(*this, [&sink](const auto& v) { sink.write(v); }); +} + bool HostAndPort::empty() const { return _host.empty() && _port < 0; } diff --git a/src/mongo/util/net/hostandport.h b/src/mongo/util/net/hostandport.h index fdadf39d9e1..03e3ef2ee5e 100644 --- a/src/mongo/util/net/hostandport.h +++ b/src/mongo/util/net/hostandport.h @@ -29,6 +29,7 @@ #pragma once +#include <fmt/format.h> #include <iosfwd> #include <string> @@ -126,9 +127,10 @@ struct HostAndPort { std::string toString() const; /** - * Like toString(), above, but writes to "ss", instead. + * Like toString(), but writes to various `sink` instead. */ - void append(StringBuilder& ss) const; + void append(StringBuilder& sink) const; + void append(fmt::writer& sink) const; /** * Returns true if this object represents no valid HostAndPort. @@ -169,3 +171,18 @@ template <typename Allocator> StringBuilderImpl<Allocator>& operator<<(StringBuilderImpl<Allocator>& os, const HostAndPort& hp); } // namespace mongo + +template <> +struct fmt::formatter<mongo::HostAndPort> { + template <typename ParseContext> + auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template <typename FormatContext> + auto format(const mongo::HostAndPort& hp, FormatContext& ctx) -> decltype(ctx.out()) { + fmt::writer w(ctx.out()); + hp.append(w); + return ctx.out(); + } +}; diff --git a/src/mongo/util/net/hostandport_test.cpp b/src/mongo/util/net/hostandport_test.cpp index c70c8e7f4ae..69163791b3a 100644 --- a/src/mongo/util/net/hostandport_test.cpp +++ b/src/mongo/util/net/hostandport_test.cpp @@ -29,6 +29,8 @@ #include "mongo/platform/basic.h" +#include <fmt/format.h> + #include "mongo/db/server_options.h" #include "mongo/unittest/unittest.h" #include "mongo/util/assert_util.h" @@ -123,5 +125,23 @@ TEST(HostAndPort, CanIdentifyDefaultRoutes) { ASSERT_TRUE(HostAndPort("[0:0:0::00:0:0]").isDefaultRoute()); } +TEST(HostAndPort, Fmt) { + const std::string specs[] = { + "1.2.3.4", // + "1.2.3.4:123", // + "[1:2:3:4]", // + "[1:2:3:4]:123", // + "/dev/mongod.sock", // + }; + for (const auto& spec : specs) { + const HostAndPort hp(spec); + const std::string hps = hp.toString(); + ASSERT_EQUALS(fmt::format("{}", hp), hps); + ASSERT_EQUALS(fmt::format("<{}>", hp), "<" + hps + ">"); + ASSERT_EQUALS(fmt::format("{}", hp), hps); + ASSERT_EQUALS(fmt::format("{1:} says {0:}", "hello", hp), hps + " says hello"); + } +} + } // namespace } // namespace mongo |