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 | 7f10780 | 2023-11-06 23:05:25 -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 |
| 20 | std::optional<std::string> aliasBlobBaseId; // Optional |
| 21 | bool migrateToAlias = false; // Optional |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * @brief Parse parameters from a config json |
| 26 | * @param j: input json object |
| 27 | * @param config: output BinaryBlobConfig |
| 28 | * @throws: exception if config doesn't have required fields |
| 29 | */ |
| 30 | static inline void parseFromConfigFile(const json& j, BinaryBlobConfig& config) |
| 31 | { |
| 32 | j.at("blobBaseId").get_to(config.blobBaseId); |
| 33 | j.at("sysFilePath").get_to(config.sysFilePath); |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 34 | if (j.contains("offsetBytes")) |
| 35 | { |
Willy Tu | 62872f5 | 2022-02-24 15:53:42 -0800 | [diff] [blame] | 36 | j.at("offsetBytes").get_to(config.offsetBytes.emplace()); |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | if (j.contains("maxSizeBytes")) |
| 40 | { |
Willy Tu | 62872f5 | 2022-02-24 15:53:42 -0800 | [diff] [blame] | 41 | j.at("maxSizeBytes").get_to(config.maxSizeBytes.emplace()); |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 42 | } |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame^] | 43 | |
| 44 | if (j.contains("aliasBlobBaseId")) |
| 45 | { |
| 46 | j.at("aliasBlobBaseId").get_to(config.aliasBlobBaseId.emplace()); |
| 47 | } |
| 48 | |
| 49 | if (j.contains("migrateToAlias")) |
| 50 | { |
| 51 | config.migrateToAlias = j.at("migrateToAlias"); |
| 52 | } |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | } // namespace conf |