-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Combined parsing and schema generations (libfetchers, WIP) #9273
Draft
roberth
wants to merge
12
commits into
NixOS:master
Choose a base branch
from
hercules-ci:libfetchers-combined-parsing-schema
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6700924
Add nix::fetchers::attrType
roberth 52a5328
Add nix::fetchers::parsers::String
roberth e3e8771
Add nix::fetchers::parsers::{Bool,Int}
roberth 9cf0927
Add nix::fetchers::Schema::Attrs
roberth 6f528f9
libutil: Add maybeGet to return optional from map lookup
roberth cf3bc19
nix::fetchers::parsers: Make Attr fully qualified
roberth f571cb3
Add convenience initializer to fetchers::Schema::Attrs
roberth c25db0c
Add nix::fetchers::parsers::Attrs etc
roberth 2f8f456
Add const to Parser::{parse,getSchema}
roberth eebdec5
refact: Move src/libfetchers/tests to tests/unit/libfetchers
roberth 7fef87c
Fix compilation error
roberth f014413
WIP make it bidirectional and use for type = tarball
roberth 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
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
|
||
namespace nix::fetchers { | ||
|
||
/** | ||
* A primitive value that can be used in a fetcher attribute. | ||
*/ | ||
typedef std::variant<std::string, uint64_t, Explicit<bool>> Attr; | ||
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. In some later PR we should probably do a wrapper struct (per #7479 (comment)) so the functions below can be methods. |
||
|
||
/** | ||
|
@@ -21,6 +24,13 @@ typedef std::variant<std::string, uint64_t, Explicit<bool>> Attr; | |
*/ | ||
typedef std::map<std::string, Attr> Attrs; | ||
|
||
/** | ||
* A lowercase string designating the type of an `Attr`. | ||
* | ||
* Matches `builtins.typeOf` in Nix. | ||
*/ | ||
std::string attrType(const Attr & attr); | ||
|
||
Attrs jsonToAttrs(const nlohmann::json & json); | ||
|
||
nlohmann::json attrsToJSON(const Attrs & attrs); | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "parser.hh" | ||
#include "schema.hh" | ||
|
||
namespace nix::fetchers { | ||
|
||
// parsers::String | ||
|
||
std::shared_ptr<Schema> parsers::String::getSchema() const { | ||
return std::make_shared<Schema>(Schema::Primitive::String); | ||
} | ||
|
||
std::string parsers::String::parse (const nix::fetchers::Attr & in) const { | ||
const std::string * r = std::get_if<std::string>(&in); | ||
if (r) | ||
return *r; | ||
else | ||
throw Error("expected a string, but value is of type " + attrType(in)); | ||
} | ||
|
||
nix::fetchers::Attr parsers::String::unparse (const std::string & in) const { | ||
return in; | ||
} | ||
|
||
// parsers::Int | ||
|
||
std::shared_ptr<Schema> parsers::Int::getSchema() const { | ||
return std::make_shared<Schema>(Schema::Primitive::Int); | ||
} | ||
|
||
uint64_t parsers::Int::parse (const nix::fetchers::Attr & in) const { | ||
const uint64_t * r = std::get_if<uint64_t>(&in); | ||
if (r) | ||
return *r; | ||
else | ||
throw Error("expected an int, but value is of type " + attrType(in)); | ||
} | ||
|
||
nix::fetchers::Attr parsers::Int::unparse (const uint64_t & in) const { | ||
return in; | ||
} | ||
|
||
// parsers::Bool | ||
|
||
std::shared_ptr<Schema> parsers::Bool::getSchema() const { | ||
return std::make_shared<Schema>(Schema::Primitive::Bool); | ||
} | ||
|
||
bool parsers::Bool::parse (const nix::fetchers::Attr & in) const { | ||
auto * r = std::get_if<Explicit<bool>>(&in); | ||
if (r) | ||
return r->t; | ||
else | ||
throw Error("expected a bool, but value is of type " + attrType(in)); | ||
} | ||
|
||
nix::fetchers::Attr parsers::Bool::unparse (const bool & in) const { | ||
return Explicit<bool>{in}; | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
our usual format