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 template <>
134 inline Try<mesos::CapabilityInfo> parse(const std::string& value)
135 {
136  Try<JSON::Object> json = parse<JSON::Object>(value);
137  if (json.isError()) {
138  return Error(json.error());
139  }
140 
141  return protobuf::parse<mesos::CapabilityInfo>(json.get());
142 }
143 
144 
145 template <>
146 inline Try<mesos::Environment> parse(const std::string& value)
147 {
148  Try<JSON::Object> json = parse<JSON::Object>(value);
149  if (json.isError()) {
150  return Error(json.error());
151  }
152 
153  return protobuf::parse<mesos::Environment>(json.get());
154 }
155 
156 
157 template <>
158 inline Try<mesos::RLimitInfo> parse(const std::string& value)
159 {
160  Try<JSON::Object> json = parse<JSON::Object>(value);
161  if (json.isError()) {
162  return Error(json.error());
163  }
164 
165  return protobuf::parse<mesos::RLimitInfo>(json.get());
166 }
167 
168 
169 template <>
170 inline Try<mesos::DomainInfo> parse(const std::string& value)
171 {
172  Try<JSON::Object> json = parse<JSON::Object>(value);
173  if (json.isError()) {
174  return Error(json.error());
175  }
176 
177  return protobuf::parse<mesos::DomainInfo>(json.get());
178 }
179 
180 
181 template <>
182 inline Try<mesos::FrameworkID> parse(const std::string& value)
183 {
184  mesos::FrameworkID frameworkId;
185  frameworkId.set_value(value);
186 
187  return frameworkId;
188 }
189 
190 
191 template <>
192 inline Try<mesos::ExecutorID> parse(const std::string& value)
193 {
194  mesos::ExecutorID executorId;
195  executorId.set_value(value);
196 
197  return executorId;
198 }
199 
200 
201 template <>
202 inline Try<mesos::SlaveID> parse(const std::string& value)
203 {
204  mesos::SlaveID slaveId;
205  slaveId.set_value(value);
206 
207  return slaveId;
208 }
209 
210 } // namespace flags {
211 
212 #endif // __COMMON_PARSE_HPP__
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Definition: check.hpp:33
std::map< std::string, Value > values
Definition: json.hpp:194
bool is() const
Definition: json.hpp:338
void json(JSON::ObjectWriter *writer, const asV1Protobuf &protobuf)
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
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:43
Definition: json.hpp:247
Iterable< V > map(F &&f, const Iterable< U, Us... > &input)
Definition: lambda.hpp:46
bool isError() const
Definition: try.hpp:78
Definition: json.hpp:79
std::string stringify(int flags)
Definition: parse.hpp:33