-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathProcessorConfigUtils.h
More file actions
194 lines (165 loc) · 8.66 KB
/
ProcessorConfigUtils.h
File metadata and controls
194 lines (165 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 <optional>
#include <set>
#include <string>
#include <vector>
#include "api/core/ProcessContext.h"
#include "utils/Enum.h"
#include "utils/expected.h"
#include "utils/OptionalUtils.h"
#include "utils/ParsingUtils.h"
#include "minifi-cpp/Exception.h"
namespace org::apache::nifi::minifi::api::utils {
inline std::string parseProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
return ctx.getProperty(property.name, flow_file)
| minifi::utils::orThrow(fmt::format("Expected valid value from \"{}\"", property.name));
}
inline bool parseBoolProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
return ctx.getProperty(property.name, flow_file)
| minifi::utils::andThen(parsing::parseBool)
| minifi::utils::orThrow(fmt::format("Expected parsable bool from \"{}\"", property.name));
}
inline uint64_t parseU64Property(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
return ctx.getProperty(property.name, flow_file)
| minifi::utils::andThen(parsing::parseIntegral<uint64_t>)
| minifi::utils::orThrow(fmt::format("Expected parsable uint64_t from \"{}\"", property.name));
}
inline int64_t parseI64Property(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
return ctx.getProperty(property.name, flow_file)
| minifi::utils::andThen(parsing::parseIntegral<int64_t>)
| minifi::utils::orThrow(fmt::format("Expected parsable int64_t from \"{}\"", property.name));
}
inline std::chrono::milliseconds parseDurationProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
return ctx.getProperty(property.name, flow_file)
| minifi::utils::andThen(parsing::parseDuration<std::chrono::milliseconds>)
| minifi::utils::orThrow(fmt::format("Expected parsable duration from \"{}\"", property.name));
}
inline uint64_t parseDataSizeProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
return ctx.getProperty(property.name, flow_file)
| minifi::utils::andThen(parsing::parseDataSize)
| minifi::utils::orThrow(fmt::format("Expected parsable data size from \"{}\"", property.name));
}
inline std::optional<std::string> parseOptionalProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
return ctx.getProperty(property.name, flow_file)
| minifi::utils::toOptional();
}
inline std::optional<bool> parseOptionalBoolProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
if (const auto property_str = ctx.getProperty(property.name, flow_file)) {
return parsing::parseBool(*property_str)
| minifi::utils::orThrow(fmt::format("Expected parsable bool from \"{}\"", property.name));
}
return std::nullopt;
}
inline std::optional<uint64_t> parseOptionalU64Property(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
if (const auto property_str = ctx.getProperty(property.name, flow_file)) {
if (property_str->empty()) {
return std::nullopt;
}
return parsing::parseIntegral<uint64_t>(*property_str)
| minifi::utils::orThrow(fmt::format("Expected parsable uint64_t from \"{}\"", property.name));
}
return std::nullopt;
}
inline std::optional<int64_t> parseOptionalI64Property(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
if (const auto property_str = ctx.getProperty(property.name, flow_file)) {
if (property_str->empty()) {
return std::nullopt;
}
return parsing::parseIntegral<int64_t>(*property_str)
| minifi::utils::orThrow(fmt::format("Expected parsable int64_t from \"{}\"", property.name));
}
return std::nullopt;
}
inline std::optional<std::chrono::milliseconds> parseOptionalDurationProperty(const core::ProcessContext& ctx,
const minifi::core::PropertyReference& property,
const core::FlowFile* flow_file = nullptr) {
if (const auto property_str = ctx.getProperty(property.name, flow_file)) {
if (property_str->empty()) {
return std::nullopt;
}
return parsing::parseDuration(*property_str)
| minifi::utils::orThrow(fmt::format("Expected parsable duration from \"{}\"", property.name));
}
return std::nullopt;
}
inline std::optional<uint64_t> parseOptionalDataSizeProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
if (const auto property_str = ctx.getProperty(property.name, flow_file)) {
if (property_str->empty()) {
return std::nullopt;
}
return parsing::parseDataSize(*property_str)
| minifi::utils::orThrow(fmt::format("Expected parsable data size from \"{}\"", property.name));
}
return std::nullopt;
}
inline std::optional<float> parseOptionalFloatProperty(const core::ProcessContext& ctx, const minifi::core::PropertyReference& property, const core::FlowFile* flow_file = nullptr) {
if (const auto property_str = ctx.getProperty(property.name, flow_file)) {
if (property_str->empty()) {
return std::nullopt;
}
return parsing::parseFloat(*property_str)
| minifi::utils::orThrow(fmt::format("Expected parsable float from \"{}\"", property.name));
}
return std::nullopt;
}
template<typename T>
T parseEnumProperty(const core::ProcessContext& context, const minifi::core::PropertyReference& prop, const core::FlowFile* flow_file = nullptr) {
const auto enum_str = context.getProperty(prop.name, flow_file);
if (!enum_str) {
throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Property '" + std::string(prop.name) + "' is missing");
}
auto result = magic_enum::enum_cast<T>(*enum_str);
if (!result) {
throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Property '" + std::string(prop.name) + "' has invalid value: '" + *enum_str + "'");
}
return result.value();
}
template<typename T>
std::optional<T> parseOptionalEnumProperty(const core::ProcessContext& context, const minifi::core::PropertyReference& prop) {
const auto enum_str = context.getProperty(prop.name);
if (!enum_str) {
return std::nullopt;
}
auto result = magic_enum::enum_cast<T>(*enum_str);
if (!result) {
throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Property '" + std::string(prop.name) + "' has invalid value: '" + *enum_str + "'");
}
return result.value();
}
template<typename ControllerServiceType>
ControllerServiceType* parseOptionalControllerService(const core::ProcessContext& context, const minifi::core::PropertyReference& prop) {
const auto controller_service_name = context.getProperty(prop.name);
if (!controller_service_name || controller_service_name->empty()) {
return nullptr;
}
auto service = context.getControllerService(*controller_service_name, minifi::core::className<ControllerServiceType>());
if (!service) {
return nullptr;
}
return reinterpret_cast<ControllerServiceType*>(*service);
}
template<typename ControllerServiceType>
gsl::not_null<ControllerServiceType*> parseControllerService(const core::ProcessContext& context, const minifi::core::PropertyReference& prop) {
auto controller_service = parseOptionalControllerService<ControllerServiceType>(context, prop);
if (!controller_service) {
throw Exception(PROCESS_SCHEDULE_EXCEPTION, fmt::format("Required controller service property '{}' is missing", prop.name));
}
return gsl::make_not_null(controller_service);
}
} // namespace org::apache::nifi::minifi::api::utils