Skip to content
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
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ perl/Makefile.config
/src/libexpr/nix.tbl
/tests/unit/libexpr/libnixexpr-tests

# /src/libfetchers/
/src/libfetchers/tests/libnixfetchers-tests

# /src/libstore/
*.gen.*
/tests/unit/libstore/libnixstore-tests
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ makefiles += \
tests/unit/libutil-support/local.mk \
tests/unit/libstore/local.mk \
tests/unit/libstore-support/local.mk \
tests/unit/libfetchers/local.mk \
tests/unit/libexpr/local.mk \
tests/unit/libexpr-support/local.mk
endif
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "symbol-table.hh"
#include "value/context.hh"
#include "input-accessor.hh"
#include "libfetchers/input-accessor.hh"

#if HAVE_BOEHMGC
#include <gc/gc_allocator.h>
Expand Down
8 changes: 8 additions & 0 deletions src/libfetchers/attrs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

namespace nix::fetchers {

std::string attrType(const Attr &attr) {
Copy link
Member

@Ericson2314 Ericson2314 Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::string attrType(const Attr &attr) {
std::string attrType(const Attr & attr) {

our usual format

return std::visit(overloaded {
[](const std::string &) { return "string"; },
[](const uint64_t &) { return "int"; },
[](const Explicit<bool> &) { return "bool"; },
}, attr);
}

Attrs jsonToAttrs(const nlohmann::json & json)
{
Attrs attrs;
Expand Down
10 changes: 10 additions & 0 deletions src/libfetchers/attrs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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.


/**
Expand All @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions src/libfetchers/parser.cc
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};
}
}
Loading
Loading