diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2020-06-17 03:52:01 +0000 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2020-06-17 03:52:01 +0000 |
commit | 21ef342172366cf66a5ff952da9ba1d825aec064 (patch) | |
tree | 3944c04a65b1f29f2cb162b806e04285eafed541 /src/libfetchers/attrs.cc | |
parent | bcde5456cc3295061a0726881c3e441444dd6680 (diff) | |
parent | 29542865cee37ab22efe1bd142900b69f6c59f0d (diff) |
Merge remote-tracking branch 'upstream/master' into derivation-header-include-order
Diffstat (limited to 'src/libfetchers/attrs.cc')
-rw-r--r-- | src/libfetchers/attrs.cc | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/libfetchers/attrs.cc b/src/libfetchers/attrs.cc new file mode 100644 index 000000000..feb0a6085 --- /dev/null +++ b/src/libfetchers/attrs.cc @@ -0,0 +1,107 @@ +#include "attrs.hh" +#include "fetchers.hh" + +#include <nlohmann/json.hpp> + +namespace nix::fetchers { + +Attrs jsonToAttrs(const nlohmann::json & json) +{ + Attrs attrs; + + for (auto & i : json.items()) { + if (i.value().is_number()) + attrs.emplace(i.key(), i.value().get<int64_t>()); + else if (i.value().is_string()) + attrs.emplace(i.key(), i.value().get<std::string>()); + else if (i.value().is_boolean()) + attrs.emplace(i.key(), i.value().get<bool>()); + else + throw Error("unsupported input attribute type in lock file"); + } + + return attrs; +} + +nlohmann::json attrsToJson(const Attrs & attrs) +{ + nlohmann::json json; + for (auto & attr : attrs) { + if (auto v = std::get_if<int64_t>(&attr.second)) { + json[attr.first] = *v; + } else if (auto v = std::get_if<std::string>(&attr.second)) { + json[attr.first] = *v; + } else if (auto v = std::get_if<Explicit<bool>>(&attr.second)) { + json[attr.first] = v->t; + } else abort(); + } + return json; +} + +std::optional<std::string> maybeGetStrAttr(const Attrs & attrs, const std::string & name) +{ + auto i = attrs.find(name); + if (i == attrs.end()) return {}; + if (auto v = std::get_if<std::string>(&i->second)) + return *v; + throw Error("input attribute '%s' is not a string %s", name, attrsToJson(attrs).dump()); +} + +std::string getStrAttr(const Attrs & attrs, const std::string & name) +{ + auto s = maybeGetStrAttr(attrs, name); + if (!s) + throw Error("input attribute '%s' is missing", name); + return *s; +} + +std::optional<int64_t> maybeGetIntAttr(const Attrs & attrs, const std::string & name) +{ + auto i = attrs.find(name); + if (i == attrs.end()) return {}; + if (auto v = std::get_if<int64_t>(&i->second)) + return *v; + throw Error("input attribute '%s' is not an integer", name); +} + +int64_t getIntAttr(const Attrs & attrs, const std::string & name) +{ + auto s = maybeGetIntAttr(attrs, name); + if (!s) + throw Error("input attribute '%s' is missing", name); + return *s; +} + +std::optional<bool> maybeGetBoolAttr(const Attrs & attrs, const std::string & name) +{ + auto i = attrs.find(name); + if (i == attrs.end()) return {}; + if (auto v = std::get_if<int64_t>(&i->second)) + return *v; + throw Error("input attribute '%s' is not a Boolean", name); +} + +bool getBoolAttr(const Attrs & attrs, const std::string & name) +{ + auto s = maybeGetBoolAttr(attrs, name); + if (!s) + throw Error("input attribute '%s' is missing", name); + return *s; +} + +std::map<std::string, std::string> attrsToQuery(const Attrs & attrs) +{ + std::map<std::string, std::string> query; + for (auto & attr : attrs) { + if (auto v = std::get_if<int64_t>(&attr.second)) { + query.insert_or_assign(attr.first, fmt("%d", *v)); + } else if (auto v = std::get_if<std::string>(&attr.second)) { + query.insert_or_assign(attr.first, *v); + } else if (auto v = std::get_if<Explicit<bool>>(&attr.second)) { + query.insert_or_assign(attr.first, v->t ? "1" : "0"); + } else abort(); + } + return query; +} + +} |