Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstdint> |
| 4 | #include <nlohmann/json.hpp> |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 5 | #include <optional> |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 6 | #include <string> |
| 7 | |
| 8 | using std::uint32_t; |
| 9 | using json = nlohmann::json; |
| 10 | |
| 11 | namespace conf |
| 12 | { |
| 13 | |
| 14 | struct BinaryBlobConfig |
| 15 | { |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 16 | std::string blobBaseId; // Required |
| 17 | std::string sysFilePath; // Required |
| 18 | std::optional<uint32_t> offsetBytes; // Optional |
| 19 | std::optional<uint32_t> maxSizeBytes; // Optional |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * @brief Parse parameters from a config json |
| 24 | * @param j: input json object |
| 25 | * @param config: output BinaryBlobConfig |
| 26 | * @throws: exception if config doesn't have required fields |
| 27 | */ |
| 28 | static inline void parseFromConfigFile(const json& j, BinaryBlobConfig& config) |
| 29 | { |
| 30 | j.at("blobBaseId").get_to(config.blobBaseId); |
| 31 | j.at("sysFilePath").get_to(config.sysFilePath); |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 32 | if (j.contains("offsetBytes")) |
| 33 | { |
| 34 | uint32_t val; |
| 35 | j.at("offsetBytes").get_to(val); |
| 36 | config.offsetBytes = val; |
| 37 | } |
| 38 | |
| 39 | if (j.contains("maxSizeBytes")) |
| 40 | { |
| 41 | uint32_t val; |
| 42 | j.at("maxSizeBytes").get_to(val); |
| 43 | config.maxSizeBytes = val; |
| 44 | } |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | } // namespace conf |