Apache Mesos
parse.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __COMMON_PARSE_HPP__
14 #define __COMMON_PARSE_HPP__
15 
16 #include <mesos/mesos.hpp>
17 
19 
20 #include <mesos/module/module.hpp>
21 
22 #include <stout/error.hpp>
23 #include <stout/hashmap.hpp>
24 #include <stout/json.hpp>
25 #include <stout/protobuf.hpp>
26 #include <stout/stringify.hpp>
27 #include <stout/try.hpp>
28 
29 #include <stout/flags/parse.hpp>
30 
31 #include "messages/messages.hpp"
32 
33 namespace flags {
34 
35 template <>
36 inline Try<mesos::ACLs> parse(const std::string& value)
37 {
38  // Convert from string or file to JSON.
39  Try<JSON::Object> json = parse<JSON::Object>(value);
40  if (json.isError()) {
41  return Error(json.error());
42  }
43 
44  // Convert from JSON to Protobuf.
45  return protobuf::parse<mesos::ACLs>(json.get());
46 }
47 
48 
49 template <>
50 inline Try<mesos::RateLimits> parse(const std::string& value)
51 {
52  // Convert from string or file to JSON.
53  Try<JSON::Object> json = parse<JSON::Object>(value);
54  if (json.isError()) {
55  return Error(json.error());
56  }
57 
58  // Convert from JSON to Protobuf.
59  return protobuf::parse<mesos::RateLimits>(json.get());
60 }
61 
62 
63 template <>
64 inline Try<mesos::Modules> parse(const std::string& value)
65 {
66  // Convert from string or file to JSON.
67  Try<JSON::Object> json = parse<JSON::Object>(value);
68  if (json.isError()) {
69  return Error(json.error());
70  }
71 
72  // Convert from JSON to Protobuf.
73  return protobuf::parse<mesos::Modules>(json.get());
74 }
75 
76 
77 template <>
78 inline Try<mesos::ContainerInfo> parse(const std::string& value)
79 {
80  // Convert from string or file to JSON.
81  Try<JSON::Object> json = parse<JSON::Object>(value);
82  if (json.isError()) {
83  return Error(json.error());
84  }
85 
86  // Convert from JSON to Protobuf.
87  return protobuf::parse<mesos::ContainerInfo>(json.get());
88 }
89 
90 
91 template <>
92 inline Try<mesos::DeviceWhitelist> parse(const std::string& value)
93 {
94  // Convert from string or file to JSON.
95  Try<JSON::Object> json = parse<JSON::Object>(value);
96  if (json.isError()) {
97  return Error(json.error());
98  }
99 
100  // Convert from JSON to Protobuf.
101  return protobuf::parse<mesos::DeviceWhitelist>(json.get());
102 }
103 
104 
105 // When the same variable is listed multiple times,
106 // uses only the last value.
107 template <>
108 inline Try<hashmap<std::string, std::string>> parse(const std::string& value)
109 {
110  // Convert from string or file to JSON.
111  Try<JSON::Object> json = parse<JSON::Object>(value);
112  if (json.isError()) {
113  return Error(json.error());
114  }
115 
116  // Convert from JSON to Hashmap.
118  foreachpair (const std::string& key,
119  const JSON::Value& value,
120  json->values) {
121  if (!value.is<JSON::String>()) {
122  return Error(
123  "The value of key '" + key + "' in '" + stringify(json.get()) + "'"
124  " is not a string");
125  }
126 
127  map[key] = value.as<JSON::String>().value;
128  }
129  return map;
130 }
131 
132 
133 // TODO(klueska): Generalize this parser to take any comma separated
134 // list and convert it to its appropriate type (i.e., not just for
135 // unsigned ints). Issues could arise when the generic type is a
136 // string that contains commas though, so generalizing this is not as
137 // straightforward as it looks at first glance.
138 template <>
139 inline Try<std::vector<unsigned int>> parse(const std::string& value)
140 {
141  std::vector<unsigned int> result;
142 
143  foreach (const std::string& token, strings::tokenize(value, ",")) {
144  Try<unsigned int> number = numify<unsigned int>(token);
145 
146  if (number.isError()) {
147  return Error("Failed to numify '" + token + "': " + number.error());
148  }
149 
150  result.push_back(number.get());
151  }
152 
153  return result;
154 }
155 
156 
157 // NOTE: Strings in the set cannot contain commas, since that
158 // is the delimiter and we provide no way to escape it.
159 //
160 // TODO(klueska): Generalize this parser to take any comma separated
161 // list and convert it to its appropriate type (i.e., not just for
162 // unsigned ints).
163 template <>
164 inline Try<std::set<std::string>> parse(const std::string& value)
165 {
166  std::set<std::string> result;
167 
168  foreach (const std::string& token, strings::tokenize(value, ",")) {
169  if (result.count(token) > 0) {
170  return Error("Duplicate token '" + token + "'");
171  }
172 
173  result.insert(token);
174  }
175 
176  return result;
177 }
178 
179 
180 template <>
181 inline Try<mesos::CapabilityInfo> parse(const std::string& value)
182 {
183  Try<JSON::Object> json = parse<JSON::Object>(value);
184  if (json.isError()) {
185  return Error(json.error());
186  }
187 
188  return protobuf::parse<mesos::CapabilityInfo>(json.get());
189 }
190 
191 
192 template <>
193 inline Try<mesos::Environment> parse(const std::string& value)
194 {
195  Try<JSON::Object> json = parse<JSON::Object>(value);
196  if (json.isError()) {
197  return Error(json.error());
198  }
199 
200  return protobuf::parse<mesos::Environment>(json.get());
201 }
202 
203 
204 template <>
205 inline Try<mesos::RLimitInfo> parse(const std::string& value)
206 {
207  Try<JSON::Object> json = parse<JSON::Object>(value);
208  if (json.isError()) {
209  return Error(json.error());
210  }
211 
212  return protobuf::parse<mesos::RLimitInfo>(json.get());
213 }
214 
215 
216 template <>
217 inline Try<mesos::DomainInfo> parse(const std::string& value)
218 {
219  Try<JSON::Object> json = parse<JSON::Object>(value);
220  if (json.isError()) {
221  return Error(json.error());
222  }
223 
224  return protobuf::parse<mesos::DomainInfo>(json.get());
225 }
226 
227 
228 template <>
229 inline Try<mesos::FrameworkID> parse(const std::string& value)
230 {
231  mesos::FrameworkID frameworkId;
232  frameworkId.set_value(value);
233 
234  return frameworkId;
235 }
236 
237 
238 template <>
239 inline Try<mesos::ExecutorID> parse(const std::string& value)
240 {
241  mesos::ExecutorID executorId;
242  executorId.set_value(value);
243 
244  return executorId;
245 }
246 
247 
248 template <>
249 inline Try<mesos::SlaveID> parse(const std::string& value)
250 {
251  mesos::SlaveID slaveId;
252  slaveId.set_value(value);
253 
254  return slaveId;
255 }
256 
257 } // namespace flags {
258 
259 #endif // __COMMON_PARSE_HPP__
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
std::map< std::string, Value > values
Definition: json.hpp:194
bool is() const
Definition: json.hpp:338
std::vector< std::string > tokenize(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:139
Try< mesos::ACLs > parse(const std::string &value)
Returns the OCI v1 descriptor, image index, image manifest and image configuration from the given str...
Definition: parse.hpp:36
void json(JSON::ObjectWriter *writer, const Task &task)
const T & as() const &
Definition: json.hpp:353
#define foreachpair(KEY, VALUE, ELEMS)
Definition: foreach.hpp:51
static Try error(const E &e)
Definition: try.hpp:42
Definition: json.hpp:247
Iterable< V > map(F &&f, const Iterable< U, Us... > &input)
Definition: lambda.hpp:46
bool isError() const
Definition: try.hpp:71
Definition: json.hpp:79
std::string stringify(int flags)
Definition: parse.hpp:33