-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathPropertyDefinitionBuilder.h
More file actions
147 lines (127 loc) · 5.27 KB
/
Copy pathPropertyDefinitionBuilder.h
File metadata and controls
147 lines (127 loc) · 5.27 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
/**
* 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 <utility>
#include "minifi-cpp/core/PropertyDefinition.h"
#include "minifi-cpp/core/PropertyValidator.h"
#include "core/ClassName.h"
namespace org::apache::nifi::minifi::core {
namespace detail {
template<typename... Types>
inline constexpr auto TypeNames = std::array<std::string_view, sizeof...(Types)>{core::className<Types>()...};
template <size_t N>
struct StringLiteral {
char value[N];
constexpr StringLiteral(const char (&str)[N]) { // NOLINT(runtime/explicit)
for (size_t i = 0; i < N; ++i) {
value[i] = str[i];
}
}
};
// A variable template that creates permanent static memory for the span to point to
template <StringLiteral str>
inline constexpr auto StaticAllowedType = std::array<std::string_view, 1>{std::string_view{str.value, sizeof(str.value) - 1}};
} // namespace detail
template<size_t NumAllowedValues = 0>
struct PropertyDefinitionBuilder {
static constexpr PropertyDefinitionBuilder<NumAllowedValues> createProperty(std::string_view name) {
PropertyDefinitionBuilder<NumAllowedValues> builder;
builder.property.name = name;
return builder;
}
static constexpr PropertyDefinitionBuilder<NumAllowedValues> createProperty(std::string_view name, std::string_view display_name) {
PropertyDefinitionBuilder<NumAllowedValues> builder;
builder.property.name = name;
builder.property.display_name = display_name;
return builder;
}
constexpr PropertyDefinitionBuilder<NumAllowedValues> withDescription(std::string_view description) {
property.description = description;
return *this;
}
constexpr PropertyDefinitionBuilder<NumAllowedValues> isRequired(bool required) {
property.is_required = required;
return *this;
}
constexpr PropertyDefinitionBuilder<NumAllowedValues> isSensitive(bool sensitive) {
property.is_sensitive = sensitive;
return *this;
}
constexpr PropertyDefinitionBuilder<NumAllowedValues> supportsExpressionLanguage(bool supports_expression_language) {
property.supports_expression_language = supports_expression_language;
return *this;
}
constexpr PropertyDefinitionBuilder<NumAllowedValues> withDefaultValue(std::string_view default_value) {
property.default_value = std::optional<std::string_view>{default_value}; // workaround for gcc 11.1; on gcc 11.3 and later, `property.default_value = default_value` works, too
return *this;
}
constexpr PropertyDefinitionBuilder<NumAllowedValues> withAllowedValues(
std::array<std::string_view, NumAllowedValues> allowed_values) {
property.allowed_values = allowed_values;
return *this;
}
template<typename... AllowedTypes>
constexpr PropertyDefinitionBuilder<NumAllowedValues> withAllowedTypes() {
property.allowed_types = {detail::TypeNames<AllowedTypes...>};
return *this;
}
template <detail::StringLiteral TypeName>
constexpr PropertyDefinitionBuilder<NumAllowedValues> withAllowedType() {
property.allowed_types = detail::StaticAllowedType<TypeName>;
return *this;
}
constexpr PropertyDefinitionBuilder<NumAllowedValues> withValidator(const PropertyValidator& property_validator) {
#if defined(__clang__) && (__clang_major__ <= 18)
property.validator = &property_validator;
#else
property.validator = gsl::make_not_null(&property_validator);
#endif
return *this;
}
constexpr PropertyDefinition<NumAllowedValues> build() {
if (property.name.size() == 0) {
throw std::logic_error("A Property must have a name");
}
if (property.supports_expression_language) {
if (property.allowed_values.size() > 0) {
throw std::logic_error("Either supports EL or has allowed values");
}
if (property.validator != &StandardPropertyValidators::NON_BLANK_VALIDATOR && property.validator != &StandardPropertyValidators::ALWAYS_VALID_VALIDATOR) {
throw std::logic_error("Only ALWAYS_VALID_VALIDATOR and NON_BLANK_VALIDATOR has EL support");
}
}
return property;
}
PropertyDefinition<NumAllowedValues> property{
.name = {},
.display_name = {},
.description = {},
.is_required = false,
.is_sensitive = false,
.allowed_values = {},
.allowed_types = {},
.default_value = {},
#if defined(__clang__) && (__clang_major__ <= 18)
.validator = &StandardPropertyValidators::ALWAYS_VALID_VALIDATOR,
#else
.validator = gsl::make_not_null(&StandardPropertyValidators::ALWAYS_VALID_VALIDATOR),
#endif
.supports_expression_language = false,
.version = 1
};
};
} // namespace org::apache::nifi::minifi::core