File indexing completed on 2024-12-29 04:50:01

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KITINERARY_JSONLDFILTERENGINE_H
0008 #define KITINERARY_JSONLDFILTERENGINE_H
0009 
0010 #include "kitinerary_export.h"
0011 
0012 #include <cstddef>
0013 
0014 class QJsonArray;
0015 class QJsonObject;
0016 
0017 namespace KItinerary {
0018 
0019 namespace JsonLd {
0020     /** Rename a property, if present and the new name isn't in use already. */
0021     void renameProperty(QJsonObject &obj, const char *oldName, const char *newName);
0022 }
0023 
0024 /** JSON-LD filtering for input normalization or type transforms.
0025  *  @internal only exported for unit tests
0026  */
0027 class KITINERARY_EXPORT JsonLdFilterEngine
0028 {
0029 public:
0030     explicit JsonLdFilterEngine();
0031     ~JsonLdFilterEngine();
0032 
0033     /** Recursively apply filtering rules to @p obj. */
0034     void filterRecursive(QJsonObject &obj);
0035     void filterRecursive(QJsonArray &array);
0036 
0037     /** Type mappings.
0038      *  Has to be sorted by @c fromType.
0039      */
0040     struct TypeMapping {
0041         const char *fromType;
0042         const char *toType;
0043     };
0044     void setTypeMappings(const TypeMapping *typeMappings, std::size_t count);
0045     template <std::size_t N>
0046     inline void setTypeMappings(const TypeMapping (&typeMappings)[N])
0047     {
0048         setTypeMappings(typeMappings, N);
0049     }
0050 
0051     /** Type filter functions.
0052      *  Has to be sorted by @c type.
0053      */
0054     struct TypeFilter {
0055         const char* type;
0056         void(*filterFunc)(QJsonObject&);
0057     };
0058     void setTypeFilters(const TypeFilter *typeFilters, std::size_t count);
0059     template <std::size_t N>
0060     inline void setTypeFilters(const TypeFilter (&typeFilters)[N])
0061     {
0062         setTypeFilters(typeFilters, N);
0063     }
0064 
0065     /** Property mappings.
0066      *  Has to be sorted by @c type.
0067      */
0068     struct PropertyMapping {
0069         const char* type;
0070         const char* fromName;
0071         const char* toName;
0072     };
0073     void setPropertyMappings(const PropertyMapping *propertyMappings, std::size_t count);
0074     template <std::size_t N>
0075     inline void setPropertyMappings(const PropertyMapping (&propertyMappings)[N])
0076     {
0077         setPropertyMappings(propertyMappings, N);
0078     }
0079 
0080 private:
0081     const TypeMapping *m_typeMappings = nullptr;
0082     std::size_t m_typeMappingsSize;
0083     const TypeFilter *m_typeFilters = nullptr;
0084     std::size_t m_typeFiltersSize;
0085     const PropertyMapping *m_propertyMappings = nullptr;
0086     std::size_t m_propertyMappingsSize;
0087 };
0088 
0089 }
0090 
0091 #endif // KITINERARY_JSONLDFILTERENGINE_H