Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MINIFICPP-1817 Use magic_enum library instead of SMART_ENUM #1609

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ include(Date)
# expected-lite
include(ExpectedLite)

# magic_enum
include(MagicEnum)

# Update passthrough args used in configurations in patch commands
if (WIN32)
set(PASSTHROUGH_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w")
Expand Down
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -3392,3 +3392,27 @@ This product contains code snippets from https://bitwizeshift.github.io, which i
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

This product bundles 'magic_enum' which is available under The MIT License.

MIT License

Copyright (c) 2019 - 2023 Daniil Goncharov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ This software includes third party software subject to the following copyrights:
- LZ4 Library - Copyright (c) 2011-2020, Yann Collet
- OpenSSL - Copyright (c) 1998-2022 The OpenSSL Project, Copyright (c) 1995-1998 Eric A. Young, Tim J. Hudson. All rights reserved.
- bitwizeshift.github.io - Copyright (c) 2020 Matthew Rodusek
- magic_enum - Copyright (c) 2019 - 2023 Daniil Goncharov

The licenses for these third party components are included in LICENSE.txt

Expand Down
42 changes: 21 additions & 21 deletions PROCESSORS.md

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions cmake/MagicEnum.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.

include(FetchContent)

FetchContent_Declare(magic_enum
URL https://github.com/Neargye/magic_enum/archive/refs/tags/v0.9.3.tar.gz
URL_HASH SHA256=3cadd6a05f1bffc5141e5e731c46b2b73c2dbff025e723c8abaa659e0a24f072)

FetchContent_GetProperties(magic_enum)
if(NOT magic_enum_POPULATED)
FetchContent_Populate(magic_enum)
add_library(magic_enum INTERFACE)
target_include_directories(magic_enum INTERFACE ${magic_enum_SOURCE_DIR}/include)
endif()
26 changes: 13 additions & 13 deletions controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ bool sendSingleCommand(std::unique_ptr<io::Socket> socket, uint8_t op, const std
}

bool stopComponent(std::unique_ptr<io::Socket> socket, const std::string& component) {
return sendSingleCommand(std::move(socket), c2::Operation::STOP, component);
return sendSingleCommand(std::move(socket), static_cast<uint8_t>(c2::Operation::stop), component);
}

bool startComponent(std::unique_ptr<io::Socket> socket, const std::string& component) {
return sendSingleCommand(std::move(socket), c2::Operation::START, component);
return sendSingleCommand(std::move(socket), static_cast<uint8_t>(c2::Operation::start), component);
}

bool clearConnection(std::unique_ptr<io::Socket> socket, const std::string& connection) {
return sendSingleCommand(std::move(socket), c2::Operation::CLEAR, connection);
return sendSingleCommand(std::move(socket), static_cast<uint8_t>(c2::Operation::clear), connection);
}

int updateFlow(std::unique_ptr<io::Socket> socket, std::ostream &out, const std::string& file) {
if (socket->initialize() < 0) {
return -1;
}
uint8_t op = c2::Operation::UPDATE;
auto op = static_cast<uint8_t>(c2::Operation::update);
io::BufferStream stream;
stream.write(&op, 1);
stream.write("flow");
Expand All @@ -60,7 +60,7 @@ int updateFlow(std::unique_ptr<io::Socket> socket, std::ostream &out, const std:
// read the response
uint8_t resp = 0;
socket->read(resp);
if (resp == c2::Operation::DESCRIBE) {
if (resp == static_cast<uint8_t>(c2::Operation::describe)) {
uint16_t connections = 0;
socket->read(connections);
out << connections << " are full" << std::endl;
Expand All @@ -77,7 +77,7 @@ int getFullConnections(std::unique_ptr<io::Socket> socket, std::ostream &out) {
if (socket->initialize() < 0) {
return -1;
}
uint8_t op = c2::Operation::DESCRIBE;
auto op = static_cast<uint8_t>(c2::Operation::describe);
io::BufferStream stream;
stream.write(&op, 1);
stream.write("getfull");
Expand All @@ -87,7 +87,7 @@ int getFullConnections(std::unique_ptr<io::Socket> socket, std::ostream &out) {
// read the response
uint8_t resp = 0;
socket->read(resp);
if (resp == c2::Operation::DESCRIBE) {
if (resp == static_cast<uint8_t>(c2::Operation::describe)) {
uint16_t connections = 0;
socket->read(connections);
out << connections << " are full" << std::endl;
Expand All @@ -104,7 +104,7 @@ int getConnectionSize(std::unique_ptr<io::Socket> socket, std::ostream &out, con
if (socket->initialize() < 0) {
return -1;
}
uint8_t op = c2::Operation::DESCRIBE;
auto op = static_cast<uint8_t>(c2::Operation::describe);
io::BufferStream stream;
stream.write(&op, 1);
stream.write("queue");
Expand All @@ -115,7 +115,7 @@ int getConnectionSize(std::unique_ptr<io::Socket> socket, std::ostream &out, con
// read the response
uint8_t resp = 0;
socket->read(resp);
if (resp == c2::Operation::DESCRIBE) {
if (resp == static_cast<uint8_t>(c2::Operation::describe)) {
std::string size;
socket->read(size);
out << "Size/Max of " << connection << " " << size << std::endl;
Expand All @@ -128,7 +128,7 @@ int listComponents(std::unique_ptr<io::Socket> socket, std::ostream &out, bool s
return -1;
}
io::BufferStream stream;
uint8_t op = c2::Operation::DESCRIBE;
auto op = static_cast<uint8_t>(c2::Operation::describe);
stream.write(&op, 1);
stream.write("components");
if (io::isError(socket->write(stream.getBuffer()))) {
Expand All @@ -155,7 +155,7 @@ int listConnections(std::unique_ptr<io::Socket> socket, std::ostream &out, bool
return -1;
}
io::BufferStream stream;
uint8_t op = c2::Operation::DESCRIBE;
auto op = static_cast<uint8_t>(c2::Operation::describe);
stream.write(&op, 1);
stream.write("connections");
if (io::isError(socket->write(stream.getBuffer()))) {
Expand All @@ -180,7 +180,7 @@ int printManifest(std::unique_ptr<io::Socket> socket, std::ostream &out) {
return -1;
}
io::BufferStream stream;
uint8_t op = c2::Operation::DESCRIBE;
auto op = static_cast<uint8_t>(c2::Operation::describe);
stream.write(&op, 1);
stream.write("manifest");
if (io::isError(socket->write(stream.getBuffer()))) {
Expand All @@ -198,7 +198,7 @@ int getJstacks(std::unique_ptr<io::Socket> socket, std::ostream &out) {
return -1;
}
io::BufferStream stream;
uint8_t op = c2::Operation::DESCRIBE;
auto op = static_cast<uint8_t>(c2::Operation::describe);
stream.write(&op, 1);
stream.write("jstack");
if (io::isError(socket->write(stream.getBuffer()))) {
Expand Down
6 changes: 3 additions & 3 deletions extensions/azure/processors/DeleteAzureBlobStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class DeleteAzureBlobStorage final : public AzureBlobStorageSingleBlobProcessorB
public:
EXTENSIONAPI static constexpr const char* Description = "Deletes the provided blob from Azure Storage";

EXTENSIONAPI static constexpr auto DeleteSnapshotsOption = core::PropertyDefinitionBuilder<storage::OptionalDeletion::length>::createProperty("Delete Snapshots Option")
EXTENSIONAPI static constexpr auto DeleteSnapshotsOption = core::PropertyDefinitionBuilder<magic_enum::enum_count<storage::OptionalDeletion>()>::createProperty("Delete Snapshots Option")
.withDescription("Specifies the snapshot deletion options to be used when deleting a blob. None: Deletes the blob only. Include Snapshots: Delete the blob and its snapshots. "
"Delete Snapshots Only: Delete only the blob's snapshots.")
.isRequired(true)
.withDefaultValue(toStringView(storage::OptionalDeletion::NONE))
.withAllowedValues(storage::OptionalDeletion::values)
.withDefaultValue(magic_enum::enum_name(storage::OptionalDeletion::NONE))
.withAllowedValues(magic_enum::enum_names<storage::OptionalDeletion>())
.build();
EXTENSIONAPI static constexpr auto Properties = utils::array_cat(AzureBlobStorageSingleBlobProcessorBase::Properties, std::array<core::PropertyReference, 1>{DeleteSnapshotsOption});

Expand Down
5 changes: 2 additions & 3 deletions extensions/azure/processors/ListAzureBlobStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ void ListAzureBlobStorage::onSchedule(const std::shared_ptr<core::ProcessContext
}
state_manager_ = std::make_unique<minifi::utils::ListingStateManager>(state_manager);

tracking_strategy_ = azure::EntityTracking::parse(
utils::parsePropertyWithAllowableValuesOrThrow(*context, ListingStrategy.name, azure::EntityTracking::values).c_str());
tracking_strategy_ = utils::parseEnumProperty<azure::EntityTracking>(*context, ListingStrategy);

auto params = buildListAzureBlobStorageParameters(*context);
if (!params) {
Expand Down Expand Up @@ -92,7 +91,7 @@ void ListAzureBlobStorage::onTrigger(const std::shared_ptr<core::ProcessContext>
std::size_t files_transferred = 0;

for (const auto& element : *list_result) {
if (tracking_strategy_ == azure::EntityTracking::TIMESTAMPS && stored_listing_state.wasObjectListedAlready(element)) {
if (tracking_strategy_ == azure::EntityTracking::timestamps && stored_listing_state.wasObjectListedAlready(element)) {
continue;
}

Expand Down
21 changes: 7 additions & 14 deletions extensions/azure/processors/ListAzureBlobStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,20 @@
#include "core/PropertyDefinition.h"
#include "AzureBlobStorageProcessorBase.h"
#include "core/logging/LoggerConfiguration.h"
#include "utils/AzureEnums.h"

namespace org::apache::nifi::minifi::azure {

SMART_ENUM(EntityTracking,
(NONE, "none"),
(TIMESTAMPS, "timestamps")
)

namespace processors {
namespace org::apache::nifi::minifi::azure::processors {

class ListAzureBlobStorage final : public AzureBlobStorageProcessorBase {
public:
EXTENSIONAPI static constexpr const char* Description = "Lists blobs in an Azure Storage container. Listing details are attached to an empty FlowFile for use with FetchAzureBlobStorage.";

EXTENSIONAPI static constexpr auto ListingStrategy = core::PropertyDefinitionBuilder<azure::EntityTracking::length>::createProperty("Listing Strategy")
EXTENSIONAPI static constexpr auto ListingStrategy = core::PropertyDefinitionBuilder<magic_enum::enum_count<EntityTracking>()>::createProperty("Listing Strategy")
.withDescription("Specify how to determine new/updated entities. If 'timestamps' is selected it tracks the latest timestamp of listed entity to determine new/updated entities. "
"If 'none' is selected it lists an entity without any tracking, the same entity will be listed each time on executing this processor.")
.isRequired(true)
.withDefaultValue(toStringView(azure::EntityTracking::TIMESTAMPS))
.withAllowedValues(azure::EntityTracking::values)
.withDefaultValue(magic_enum::enum_name(EntityTracking::timestamps))
.withAllowedValues(magic_enum::enum_names<EntityTracking>())
.build();
EXTENSIONAPI static constexpr auto Prefix = core::PropertyDefinitionBuilder<>::createProperty("Prefix")
.withDescription("Search prefix for listing")
Expand Down Expand Up @@ -87,9 +81,8 @@ class ListAzureBlobStorage final : public AzureBlobStorageProcessorBase {
std::shared_ptr<core::FlowFile> createNewFlowFile(core::ProcessSession &session, const storage::ListContainerResultElement &element);

storage::ListAzureBlobStorageParameters list_parameters_;
azure::EntityTracking tracking_strategy_ = azure::EntityTracking::TIMESTAMPS;
azure::EntityTracking tracking_strategy_ = azure::EntityTracking::timestamps;
std::unique_ptr<minifi::utils::ListingStateManager> state_manager_;
};

} // namespace processors
} // namespace org::apache::nifi::minifi::azure
} // namespace org::apache::nifi::minifi::azure::processors
2 changes: 1 addition & 1 deletion extensions/azure/processors/ListAzureDataLakeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void ListAzureDataLakeStorage::onTrigger(const std::shared_ptr<core::ProcessCont
std::size_t files_transferred = 0;

for (const auto& element : *list_result) {
if (tracking_strategy_ == azure::EntityTracking::TIMESTAMPS && stored_listing_state.wasObjectListedAlready(element)) {
if (tracking_strategy_ == azure::EntityTracking::timestamps && stored_listing_state.wasObjectListedAlready(element)) {
continue;
}

Expand Down
23 changes: 8 additions & 15 deletions extensions/azure/processors/ListAzureDataLakeStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@
#include "core/PropertyDefinitionBuilder.h"
#include "core/PropertyType.h"
#include "utils/ArrayUtils.h"
#include "utils/AzureEnums.h"

class ListAzureDataLakeStorageTestsFixture;

namespace org::apache::nifi::minifi::azure {

SMART_ENUM(EntityTracking,
(NONE, "none"),
(TIMESTAMPS, "timestamps")
)

namespace processors {
namespace org::apache::nifi::minifi::azure::processors {

class ListAzureDataLakeStorage final : public AzureDataLakeStorageProcessorBase {
public:
Expand All @@ -56,11 +50,11 @@ class ListAzureDataLakeStorage final : public AzureDataLakeStorageProcessorBase
EXTENSIONAPI static constexpr auto PathFilter = core::PropertyDefinitionBuilder<>::createProperty("Path Filter")
.withDescription("When 'Recurse Subdirectories' is true, then only subdirectories whose paths match the given regular expression will be scanned")
.build();
EXTENSIONAPI static constexpr auto ListingStrategy = core::PropertyDefinitionBuilder<azure::EntityTracking::length>::createProperty("Listing Strategy")
EXTENSIONAPI static constexpr auto ListingStrategy = core::PropertyDefinitionBuilder<magic_enum::enum_count<azure::EntityTracking>()>::createProperty("Listing Strategy")
.withDescription("Specify how to determine new/updated entities. If 'timestamps' is selected it tracks the latest timestamp of listed entity to "
"determine new/updated entities. If 'none' is selected it lists an entity without any tracking, the same entity will be listed each time on executing this processor.")
.withDefaultValue(toStringView(azure::EntityTracking::TIMESTAMPS))
.withAllowedValues(azure::EntityTracking::values)
"determine new/updated entities. If 'none' is selected it lists an entity without any tracking, the same entity will be listed each time on executing this processor.")
.withDefaultValue(magic_enum::enum_name(azure::EntityTracking::timestamps))
.withAllowedValues(magic_enum::enum_names<azure::EntityTracking>())
.build();
EXTENSIONAPI static constexpr auto Properties = utils::array_cat(AzureDataLakeStorageProcessorBase::Properties, std::array<core::PropertyReference, 4>{
RecurseSubdirectories,
Expand Down Expand Up @@ -98,10 +92,9 @@ class ListAzureDataLakeStorage final : public AzureDataLakeStorageProcessorBase

std::optional<storage::ListAzureDataLakeStorageParameters> buildListParameters(core::ProcessContext &context);

azure::EntityTracking tracking_strategy_ = azure::EntityTracking::TIMESTAMPS;
azure::EntityTracking tracking_strategy_ = azure::EntityTracking::timestamps;
storage::ListAzureDataLakeStorageParameters list_parameters_;
std::unique_ptr<minifi::utils::ListingStateManager> state_manager_;
};

} // namespace processors
} // namespace org::apache::nifi::minifi::azure
} // namespace org::apache::nifi::minifi::azure::processors
8 changes: 4 additions & 4 deletions extensions/azure/processors/PutAzureDataLakeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ std::optional<storage::PutAzureDataLakeStorageParameters> PutAzureDataLakeStorag
if (!setFileOperationCommonParameters(params, context, flow_file)) {
return std::nullopt;
}
params.replace_file = conflict_resolution_strategy_ == azure::FileExistsResolutionStrategy::REPLACE_FILE;
params.replace_file = conflict_resolution_strategy_ == azure::FileExistsResolutionStrategy::replace;

return params;
}
Expand All @@ -82,13 +82,13 @@ void PutAzureDataLakeStorage::onTrigger(const std::shared_ptr<core::ProcessConte
const storage::UploadDataLakeStorageResult result = callback.getResult();

if (result.result_code == storage::UploadResultCode::FILE_ALREADY_EXISTS) {
gsl_Expects(conflict_resolution_strategy_ != azure::FileExistsResolutionStrategy::REPLACE_FILE);
if (conflict_resolution_strategy_ == azure::FileExistsResolutionStrategy::FAIL_FLOW) {
gsl_Expects(conflict_resolution_strategy_ != azure::FileExistsResolutionStrategy::replace);
if (conflict_resolution_strategy_ == azure::FileExistsResolutionStrategy::fail) {
logger_->log_error("Failed to upload file '%s/%s' to filesystem '%s' on Azure Data Lake storage because file already exists",
params->directory_name, params->filename, params->file_system_name);
session->transfer(flow_file, Failure);
return;
} else if (conflict_resolution_strategy_ == azure::FileExistsResolutionStrategy::IGNORE_REQUEST) {
} else if (conflict_resolution_strategy_ == azure::FileExistsResolutionStrategy::ignore) {
logger_->log_debug("Upload of file '%s/%s' was ignored because it already exits in filesystem '%s' on Azure Data Lake Storage",
params->directory_name, params->filename, params->file_system_name);
session->transfer(flow_file, Success);
Expand Down
17 changes: 9 additions & 8 deletions extensions/azure/processors/PutAzureDataLakeStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,24 @@ class AzureDataLakeStorageTestsFixture;

namespace org::apache::nifi::minifi::azure {

SMART_ENUM(FileExistsResolutionStrategy,
(FAIL_FLOW, "fail"),
(REPLACE_FILE, "replace"),
(IGNORE_REQUEST, "ignore")
)
enum class FileExistsResolutionStrategy {
fail,
replace,
ignore
};

namespace processors {

class PutAzureDataLakeStorage final : public AzureDataLakeStorageFileProcessorBase {
public:
EXTENSIONAPI static constexpr const char *Description = "Puts content into an Azure Data Lake Storage Gen 2";

EXTENSIONAPI static constexpr auto ConflictResolutionStrategy = core::PropertyDefinitionBuilder<azure::FileExistsResolutionStrategy::length>::createProperty("Conflict Resolution Strategy")
EXTENSIONAPI static constexpr auto ConflictResolutionStrategy
= core::PropertyDefinitionBuilder<magic_enum::enum_count<azure::FileExistsResolutionStrategy>()>::createProperty("Conflict Resolution Strategy")
.withDescription("Indicates what should happen when a file with the same name already exists in the output directory.")
.isRequired(true)
.withDefaultValue(toStringView(azure::FileExistsResolutionStrategy::FAIL_FLOW))
.withAllowedValues(azure::FileExistsResolutionStrategy::values)
.withDefaultValue(magic_enum::enum_name(azure::FileExistsResolutionStrategy::fail))
.withAllowedValues(magic_enum::enum_names<azure::FileExistsResolutionStrategy>())
.build();
EXTENSIONAPI static constexpr auto Properties = utils::array_cat(AzureDataLakeStorageFileProcessorBase::Properties, std::array<core::PropertyReference, 1>{ConflictResolutionStrategy});

Expand Down
Loading
Loading