-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Clean up structured attrs parsing using JSON utils #14754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,76 +21,53 @@ static std::optional<std::string> | |
| getStringAttr(const StringMap & env, const StructuredAttrs * parsed, const std::string & name) | ||
| { | ||
| if (parsed) { | ||
| auto i = parsed->structuredAttrs.find(name); | ||
| if (i == parsed->structuredAttrs.end()) | ||
| return {}; | ||
| else { | ||
| if (!i->second.is_string()) | ||
| throw Error("attribute '%s' of must be a string", name); | ||
| return i->second.get<std::string>(); | ||
| } | ||
| if (auto * i = get(parsed->structuredAttrs, name)) | ||
| try { | ||
| return getString(*i); | ||
| } catch (Error & e) { | ||
| e.addTrace({}, "while parsing attribute \"%s\"", name); | ||
| throw; | ||
| } | ||
| } else { | ||
| auto i = env.find(name); | ||
| if (i == env.end()) | ||
| return {}; | ||
| else | ||
| return i->second; | ||
| if (auto * i = get(env, name)) | ||
| return *i; | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| static bool getBoolAttr(const StringMap & env, const StructuredAttrs * parsed, const std::string & name, bool def) | ||
| { | ||
| if (parsed) { | ||
| auto i = parsed->structuredAttrs.find(name); | ||
| if (i == parsed->structuredAttrs.end()) | ||
| return def; | ||
| else { | ||
| if (!i->second.is_boolean()) | ||
| throw Error("attribute '%s' must be a Boolean", name); | ||
| return i->second.get<bool>(); | ||
| } | ||
| if (auto * i = get(parsed->structuredAttrs, name)) | ||
| try { | ||
| return getBoolean(*i); | ||
| } catch (Error & e) { | ||
| e.addTrace({}, "while parsing attribute \"%s\"", name); | ||
| throw; | ||
| } | ||
| } else { | ||
| auto i = env.find(name); | ||
| if (i == env.end()) | ||
| return def; | ||
| else | ||
| return i->second == "1"; | ||
| if (auto * i = get(env, name)) | ||
| return *i == "1"; | ||
| } | ||
| return def; | ||
| } | ||
|
|
||
| static std::optional<Strings> | ||
| getStringsAttr(const StringMap & env, const StructuredAttrs * parsed, const std::string & name) | ||
| static std::optional<StringSet> | ||
| getStringSetAttr(const StringMap & env, const StructuredAttrs * parsed, const std::string & name) | ||
| { | ||
| if (parsed) { | ||
| auto i = parsed->structuredAttrs.find(name); | ||
| if (i == parsed->structuredAttrs.end()) | ||
| return {}; | ||
| else { | ||
| if (!i->second.is_array()) | ||
| throw Error("attribute '%s' must be a list of strings", name); | ||
| auto & a = getArray(i->second); | ||
| Strings res; | ||
| for (auto j = a.begin(); j != a.end(); ++j) { | ||
| if (!j->is_string()) | ||
| throw Error("attribute '%s' must be a list of strings", name); | ||
| res.push_back(j->get<std::string>()); | ||
| if (auto * i = get(parsed->structuredAttrs, name)) | ||
| try { | ||
| return getStringSet(*i); | ||
| } catch (Error & e) { | ||
| e.addTrace({}, "while parsing attribute \"%s\"", name); | ||
| throw; | ||
| } | ||
| return res; | ||
| } | ||
| } else { | ||
| auto i = env.find(name); | ||
| if (i == env.end()) | ||
| return {}; | ||
| else | ||
| return tokenizeString<Strings>(i->second); | ||
| if (auto * i = get(env, name)) | ||
| return tokenizeString<StringSet>(*i); | ||
| } | ||
| } | ||
|
|
||
| static std::optional<StringSet> | ||
| getStringSetAttr(const StringMap & env, const StructuredAttrs * parsed, const std::string & name) | ||
| { | ||
| auto ss = getStringsAttr(env, parsed, name); | ||
| return ss ? (std::optional{StringSet{ss->begin(), ss->end()}}) : (std::optional<StringSet>{}); | ||
| return {}; | ||
| } | ||
|
|
||
| template<typename Inputs> | ||
|
|
@@ -233,45 +210,30 @@ DerivationOptions<SingleDerivedPath> derivationOptionsFromStructuredAttrs( | |
| std::map<std::string, OutputChecks<SingleDerivedPath>> res; | ||
| if (auto * outputChecks = get(structuredAttrs, "outputChecks")) { | ||
| for (auto & [outputName, output_] : getObject(*outputChecks)) { | ||
| OutputChecks<SingleDerivedPath> checks; | ||
|
|
||
| auto & output = getObject(output_); | ||
|
|
||
| if (auto maxSize = get(output, "maxSize")) | ||
| checks.maxSize = maxSize->get<uint64_t>(); | ||
|
|
||
| if (auto maxClosureSize = get(output, "maxClosureSize")) | ||
| checks.maxClosureSize = maxClosureSize->get<uint64_t>(); | ||
|
|
||
| auto get_ = | ||
| [&](const std::string & name) -> std::optional<std::set<DrvRef<SingleDerivedPath>>> { | ||
| if (auto i = get(output, name)) { | ||
| std::set<DrvRef<SingleDerivedPath>> res; | ||
| for (auto j = i->begin(); j != i->end(); ++j) { | ||
| if (!j->is_string()) | ||
| throw Error("attribute '%s' must be a list of strings", name); | ||
| res.insert(parseRef(j->get<std::string>())); | ||
| if (auto * i = get(output, name)) { | ||
| try { | ||
| std::set<DrvRef<SingleDerivedPath>> res; | ||
| for (auto & s : getStringList(*i)) | ||
| res.insert(parseRef(s)); | ||
| return res; | ||
| } catch (Error & e) { | ||
| e.addTrace( | ||
| {}, "while parsing attribute 'outputChecks.\"%s\".%s'", outputName, name); | ||
| throw; | ||
| } | ||
| return res; | ||
| } | ||
| return {}; | ||
| }; | ||
|
|
||
| res.insert_or_assign( | ||
| outputName, | ||
| OutputChecks<SingleDerivedPath>{ | ||
| .maxSize = [&]() -> std::optional<uint64_t> { | ||
| if (auto maxSize = get(output, "maxSize")) | ||
| return maxSize->get<uint64_t>(); | ||
| else | ||
| return std::nullopt; | ||
| }(), | ||
| .maxClosureSize = [&]() -> std::optional<uint64_t> { | ||
| if (auto maxClosureSize = get(output, "maxClosureSize")) | ||
| return maxClosureSize->get<uint64_t>(); | ||
| else | ||
| return std::nullopt; | ||
| }(), | ||
xokdvium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .maxSize = ptrToOwned<uint64_t>(get(output, "maxSize")), | ||
| .maxClosureSize = ptrToOwned<uint64_t>(get(output, "maxClosureSize")), | ||
| .allowedReferences = get_("allowedReferences"), | ||
| .disallowedReferences = | ||
| get_("disallowedReferences").value_or(std::set<DrvRef<SingleDerivedPath>>{}), | ||
|
|
@@ -307,13 +269,13 @@ DerivationOptions<SingleDerivedPath> derivationOptionsFromStructuredAttrs( | |
| std::map<std::string, bool> res; | ||
|
|
||
| if (parsed) { | ||
| auto & structuredAttrs = parsed->structuredAttrs; | ||
|
|
||
| if (auto * udr = get(structuredAttrs, "unsafeDiscardReferences")) { | ||
| for (auto & [outputName, output] : getObject(*udr)) { | ||
| if (!output.is_boolean()) | ||
| throw Error("attribute 'unsafeDiscardReferences.\"%s\"' must be a Boolean", outputName); | ||
| res.insert_or_assign(outputName, output.get<bool>()); | ||
| if (auto * udr = get(parsed->structuredAttrs, "unsafeDiscardReferences")) { | ||
| try { | ||
| for (auto & [outputName, output] : getObject(*udr)) | ||
| res.insert_or_assign(outputName, getBoolean(output)); | ||
| } catch (Error & e) { | ||
| e.addTrace({}, "while parsing attribute 'unsafeDiscardReferences'"); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we don't loose context versus old
Ericson2314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -340,7 +302,7 @@ DerivationOptions<SingleDerivedPath> derivationOptionsFromStructuredAttrs( | |
| std::map<std::string, std::set<SingleDerivedPath>> ret; | ||
|
|
||
| if (parsed) { | ||
| auto * e = optionalValueAt(parsed->structuredAttrs, "exportReferencesGraph"); | ||
| auto * e = get(parsed->structuredAttrs, "exportReferencesGraph"); | ||
| if (!e) | ||
| return ret; | ||
| if (!e->is_object()) { | ||
|
|
@@ -595,8 +557,8 @@ DerivationOptions<SingleDerivedPath> adl_serializer<DerivationOptions<SingleDeri | |
| .outputChecks = [&]() -> OutputChecksVariant<SingleDerivedPath> { | ||
| auto outputChecks = getObject(valueAt(json, "outputChecks")); | ||
|
|
||
| auto forAllOutputsOpt = optionalValueAt(outputChecks, "forAllOutputs"); | ||
| auto perOutputOpt = optionalValueAt(outputChecks, "perOutput"); | ||
| auto forAllOutputsOpt = get(outputChecks, "forAllOutputs"); | ||
| auto perOutputOpt = get(outputChecks, "perOutput"); | ||
|
|
||
| if (forAllOutputsOpt && !perOutputOpt) { | ||
| return static_cast<OutputChecks<SingleDerivedPath>>(*forAllOutputsOpt); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,15 @@ Strings getStringList(const nlohmann::json & value); | |
| StringMap getStringMap(const nlohmann::json & value); | ||
| StringSet getStringSet(const nlohmann::json & value); | ||
|
|
||
| template<typename T> | ||
| static inline std::optional<T> ptrToOwned(const nlohmann::json * ptr) | ||
|
Comment on lines
+78
to
+79
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moving it to the right namespace |
||
| { | ||
| if (ptr) | ||
| return std::optional{*ptr}; | ||
| else | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| } // namespace nix | ||
|
|
||
| namespace nlohmann { | ||
|
|
@@ -114,13 +123,4 @@ struct adl_serializer<std::optional<T>> | |
| } | ||
| }; | ||
|
|
||
| template<typename T> | ||
| static inline std::optional<T> ptrToOwned(const json * ptr) | ||
| { | ||
| if (ptr) | ||
| return std::optional{*ptr}; | ||
| else | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| } // namespace nlohmann | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.