Asylo
proto_flag.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2020 Asylo authors
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef ASYLO_UTIL_PROTO_FLAG_H_
20 #define ASYLO_UTIL_PROTO_FLAG_H_
21 
22 #include <string>
23 
24 #include <google/protobuf/text_format.h>
25 #include "absl/strings/str_cat.h"
26 #include "absl/strings/string_view.h"
27 #include "asylo/util/logging.h"
28 
29 namespace asylo {
30 
31 // @file proto_flag.h
32 // @brief The functions in this file add support for parsing protobuf flags that
33 // are passed as textprotos. Only protobufs in the asylo namespace are
34 // supported.
35 
36 template <class T>
37 bool AbslParseFlag(absl::string_view text, T *flag, std::string *error) {
38  if (!google::protobuf::TextFormat::ParseFromString(
39  std::string(text.data(), text.size()), flag)) {
40  *error =
41  absl::StrCat("Failed to parse ", flag->GetDescriptor()->full_name());
42  return false;
43  }
44  return true;
45 }
46 
47 template <class T>
49  std::string serialized_flag;
50  CHECK(google::protobuf::TextFormat::PrintToString(flag, &serialized_flag));
51  return serialized_flag;
52 }
53 
54 } // namespace asylo
55 
56 #endif // ASYLO_UTIL_PROTO_FLAG_H_
std::string AbslUnparseFlag(const T &flag)
Definition: proto_flag.h:48
bool AbslParseFlag(absl::string_view text, T *flag, std::string *error)
Definition: proto_flag.h:37