-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: A helper function to make an object of format-args type suitable for passing to `fmt::vformat` from an input map. Reviewed By: Gownta Differential Revision: D67314712 fbshipit-source-id: 60fef13adc8b946917cbca7ffc5fbf3dffae882b
- Loading branch information
1 parent
1375283
commit 5d060f7
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <fmt/args.h> | ||
|
||
#include <folly/CppAttributes.h> | ||
|
||
namespace folly { | ||
|
||
/// fmt_make_format_args_from_map_fn | ||
/// fmt_make_format_args_from_map | ||
/// | ||
/// A helper function-object type and variable for making a format-args object | ||
/// from a map. | ||
/// | ||
/// May be useful for transitioning from legacy folly::svformat to fmt::vformat. | ||
struct fmt_make_format_args_from_map_fn { | ||
template <typename Map> | ||
fmt::dynamic_format_arg_store<fmt::format_context> operator()( | ||
[[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] Map const& map) const { | ||
fmt::dynamic_format_arg_store<fmt::format_context> ret; | ||
ret.reserve(map.size(), map.size()); | ||
for (auto const& [key, val] : map) { | ||
ret.push_back(fmt::arg(key.c_str(), std::cref(val))); | ||
} | ||
return ret; | ||
} | ||
}; | ||
inline constexpr fmt_make_format_args_from_map_fn | ||
fmt_make_format_args_from_map{}; | ||
|
||
} // namespace folly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <folly/FmtUtility.h> | ||
|
||
#include <map> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace std::literals; | ||
|
||
struct FmtUtilityTest : testing::Test {}; | ||
|
||
TEST_F(FmtUtilityTest, fmt_make_format_args_from_map_fn) { | ||
EXPECT_EQ( | ||
"hello bob you silly goose", | ||
fmt::vformat( | ||
"hello {name} you {adj} goose", | ||
folly::fmt_make_format_args_from_map( | ||
std::map<std::string, std::string_view>{ | ||
{"name"s, "bob"sv}, | ||
{"adj"s, "silly"sv}, | ||
}))); | ||
} |