LCOV - code coverage report
Current view: top level - model - TypeConverter.hpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 100.0 % 5 5
Test Date: 2025-02-14 17:40:40 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :  ___________________________________________
       3              : |    _     ___                        _     |
       4              : |   | |   |__ \                      | |    |
       5              : |   | |__    ) |__ _  __ _  ___ _ __ | |_   |
       6              : |   | '_ \  / // _` |/ _` |/ _ \ '_ \| __|  |  HTTP/2 AGENT FOR MOCK TESTING
       7              : |   | | | |/ /| (_| | (_| |  __/ | | | |_   |  Version 0.0.z
       8              : |   |_| |_|____\__,_|\__, |\___|_| |_|\__|  |  https://github.com/testillano/h2agent
       9              : |                     __/ |                 |
      10              : |                    |___/                  |
      11              : |___________________________________________|
      12              : 
      13              : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
      14              : SPDX-License-Identifier: MIT
      15              : Copyright (c) 2021 Eduardo Ramos
      16              : 
      17              : Permission is hereby  granted, free of charge, to any  person obtaining a copy
      18              : of this software and associated  documentation files (the "Software"), to deal
      19              : in the Software  without restriction, including without  limitation the rights
      20              : to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
      21              : copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
      22              : furnished to do so, subject to the following conditions:
      23              : 
      24              : The above copyright notice and this permission notice shall be included in all
      25              : copies or substantial portions of the Software.
      26              : 
      27              : THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
      28              : IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
      29              : FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
      30              : AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
      31              : LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      32              : OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
      33              : SOFTWARE.
      34              : */
      35              : 
      36              : #pragma once
      37              : 
      38              : 
      39              : #include <nlohmann/json.hpp>
      40              : #include <string>
      41              : #include <sstream>
      42              : #include <cstdint>
      43              : #include <unordered_map>
      44              : 
      45              : namespace h2agent
      46              : {
      47              : namespace model
      48              : {
      49              : /**
      50              :  * Search/replace all the items in provided string
      51              :  *
      52              :  * @param str string passed by reference
      53              :  * @param from pattern to search
      54              :  * @param to value to replace in pattern ocurrences
      55              :  */
      56              : void searchReplaceAll(std::string& str, const std::string& from, const std::string& to);
      57              : 
      58              : /**
      59              :  * Replace variable patterns with their variable values using 2 sources:
      60              :  * local variables and global variables. Local variables are replaced with
      61              :  * priority over existing global variables with the same name.
      62              :  *
      63              :  * @param str string to update with replaced values
      64              :  * @param patterns map of patterns (pattern, varname) where pattern is @{varname}
      65              :  * @param vars local variables source map
      66              :  * @param gvars global variables source map
      67              :  */
      68              : void replaceVariables(std::string &str, const std::map<std::string, std::string> &patterns, const std::map<std::string,std::string> &vars, const std::unordered_map<std::string,std::string> &gvars);
      69              : 
      70              : 
      71              : class TypeConverter {
      72              : 
      73              : public:
      74              : 
      75              :     enum NativeType { String = 0, Integer, Unsigned, Float, Boolean, Object };
      76              : 
      77              : private:
      78              : 
      79              :     std::string s_value_{}; // string
      80              :     std::int64_t i_value_{}; // integer
      81              :     std::uint64_t u_value_{}; // unsigned integer
      82              :     double f_value_{}; // float number
      83              :     bool b_value_{}; // boolean
      84              :     nlohmann::json j_value_{}; // json object
      85              : 
      86              :     NativeType native_type_{};
      87              : 
      88              : public:
      89              : 
      90              :     /**
      91              :     * Clears class content
      92              :     */
      93              :     void clear();
      94              : 
      95              :     /**
      96              :     * Default constructor
      97              :     */
      98          112 :     TypeConverter() {
      99          112 :         clear() ;
     100          112 :     }
     101              : 
     102              :     // setters
     103              : 
     104              :     /**
     105              :     * Sets string to vault
     106              :     *
     107              :     * @param str String assigned
     108              :     */
     109              :     void setString(const std::string &str);
     110              : 
     111              :     /**
     112              :     * Sets string to vault replacing variables in form @{varname}
     113              :     * This is done here to avoid a string copy
     114              :     *
     115              :     * @param str string assigned
     116              :     * @param patterns map of patterns (pattern, varname) where pattern is @{varname}
     117              :     * @param vars local variables source map
     118              :     * @param gvars global variables source map
     119              :     */
     120              :     void setStringReplacingVariables(const std::string &str, const std::map<std::string, std::string> &patterns, const std::map<std::string,std::string> &vars, const std::unordered_map<std::string,std::string> &gvars);
     121              : 
     122              :     /**
     123              :     * Sets integer to vault
     124              :     *
     125              :     * @param i Number assigned
     126              :     */
     127              :     void setInteger(const std::int64_t i);
     128              : 
     129              :     /**
     130              :     * Sets unsigned integer to vault
     131              :     *
     132              :     * @param u Unsigned integer assigned
     133              :     */
     134              :     void setUnsigned(const std::uint64_t u);
     135              : 
     136              :     /**
     137              :     * Sets float number to vault
     138              :     *
     139              :     * @param f float number assigned
     140              :     */
     141              :     void setFloat(const double f);
     142              : 
     143              :     /**
     144              :     * Sets boolean to vault
     145              :     *
     146              :     * @param boolean Boolean assigned
     147              :     */
     148              :     void setBoolean(bool boolean);
     149              : 
     150              :     /**
     151              :     * Sets object to vault
     152              :     *
     153              :     * If the object is a string, this automatically will assing a string (@see setString()).
     154              :     * If the object is a integer, this automatically will assing an integer (@see setInteger()).
     155              :     * If the object is an unsigned integer, this automatically will assing an unsigned integer (@see setUnsigned()).
     156              :     * If the object is a float number, this automatically will assing a float number (@see setFloat()).
     157              :     * If the object is a boolean, this automatically will assing a boolean (@see setBoolean()).
     158              :     *
     159              :     * @param jsonSource Json Document from which extract the information
     160              :     * @param path Json path to extract from provided json document
     161              :     *
     162              :     * @return Return boolean for successful extraction (path is found), false otherwise (null extracted)
     163              :     */
     164              :     bool setObject(const nlohmann::json &jsonSource, const std::string &path);
     165              : 
     166              :     // getters
     167              : 
     168              :     /**
     169              :     * Gets native type selected from source
     170              :     * Thisi is useful to use the corresponding getter if no conversion is desired
     171              :     */
     172           58 :     NativeType getNativeType() const {
     173           58 :         return native_type_;
     174              :     }
     175              : 
     176              :     /**
     177              :     * Gets string type from vault
     178              :     *
     179              :     * @param success operation boolean result passed by reference
     180              :     *
     181              :     * @result String data reference (you may check success result)
     182              :     * @warning multiple accesses could imply multiple conversions
     183              :     */
     184              :     const std::string &getString(bool &success);
     185              : 
     186              :     /**
     187              :     * Gets integer type from vault
     188              :     *
     189              :     * @param success operation boolean result passed by reference
     190              :     *
     191              :     * @result Integer data reference (you may check success result)
     192              :     * @warning multiple accesses could imply multiple conversions
     193              :     */
     194              :     std::int64_t getInteger(bool &success);
     195              : 
     196              :     /**
     197              :     * Gets unsigned integer type from vault
     198              :     *
     199              :     * @param success operation boolean result passed by reference
     200              :     *
     201              :     * @result Unsigned integer data reference (you may check success result)
     202              :     * @warning multiple accesses could imply multiple conversions
     203              :     */
     204              :     std::uint64_t getUnsigned(bool &success);
     205              : 
     206              :     /**
     207              :     * Gets float number type from vault
     208              :     *
     209              :     * @param success operation boolean result passed by reference
     210              :     *
     211              :     * @result Float number data reference (you may check success result)
     212              :     * @warning multiple accesses could imply multiple conversions
     213              :     */
     214              :     double getFloat(bool &success);
     215              : 
     216              :     /**
     217              :     * Gets boolean type from vault
     218              :     *
     219              :     * @param success operation boolean result passed by reference
     220              :     *
     221              :     * @result Boolean data reference (you may check success result)
     222              :     * @warning multiple accesses could imply multiple conversions
     223              :     */
     224              :     bool getBoolean(bool &success);
     225              : 
     226              :     /**
     227              :     * Gets json object type from vault
     228              :     *
     229              :     * @param success operation boolean result passed by reference
     230              :     *
     231              :     * @result Json object data reference (you may check success result)
     232              :     * @warning no conversions from other types are done here
     233              :     */
     234              :     const nlohmann::json &getObject(bool &success);
     235              : 
     236              :     /**
     237              :     * Class string representation
     238              :     *
     239              :     * Member attributes may not be updated until their corresponding
     240              :     * getters are invoked, then, this representation is ONLY RELIABLE
     241              :     * for current native type.
     242              :     */
     243              :     std::string asString();
     244              : };
     245              : 
     246              : }
     247              : }
        

Generated by: LCOV version 2.0-1