Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Add attr path lookup helper #28

Closed
wants to merge 3 commits into from
Closed

Add attr path lookup helper #28

wants to merge 3 commits into from

Conversation

zmitchell
Copy link
Contributor

@zmitchell zmitchell commented Aug 11, 2023

This adds a small executable id2attrpath with the following interface

$ id2attrpath <SOURCE> <ID>

that returns a space-delimited attribute path corresponding to the AttrSets.id passed in. Future PRs will extend this to Packages.id.

This program is only intended to make testing easier and shouldn't be used elsewhere.

@zmitchell zmitchell self-assigned this Aug 11, 2023
# ---------------------------------------------------------------------------- #

setup_file() {
export DBPATH="$BATS_FILE_TMPDIR/test-cli.sqlite";
Copy link
Contributor

Choose a reason for hiding this comment

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

Unclear to me what we want our convention to be for this var:

> rg "export DBPATH"
tests/pathological.bats
17:  export DBPATH="$BATS_FILE_TMPDIR/test-cli.sqlite";

tests/cli.bats
17:  export DBPATH="$BATS_FILE_TMPDIR/test-cli.sqlite";

tests/nixpkgs-flox.bats
43:  export DBPATH="$BATS_FILE_TMPDIR/test-nixpkgs-flox.sqlite";

tests/sqlite3.bats
17:  export DBPATH="$BATS_FILE_TMPDIR/test.sqlite";

tests/id2attrpath.bats
17:  export DBPATH="$BATS_FILE_TMPDIR/test-cli.sqlite";

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just use the basement test.sqlite for all tests for consistency. It's not a real problem to use arbitrary names though.

tests/id2attrpath.cc Outdated Show resolved Hide resolved
tests/id2attrpath.cc Outdated Show resolved Hide resolved
{
std::cout << attrPath[i] << " ";
}
std::cout << attrPath[attrPath.size() - 1] << std::endl;
Copy link
Contributor

@mkenigs mkenigs Aug 11, 2023

Choose a reason for hiding this comment

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

I was wondering if it would be better to use an STL one liner instead of writing this algorithm and realized this will segfault if id2attrpath is called with ID = 0

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use attrPath.back() to handle the concern @mkenigs raised. Good catch.

Copy link
Contributor

@aakropotkin aakropotkin left a comment

Choose a reason for hiding this comment

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

Handle the attrPath.back() or use nix::concatStringsSep and you'll be ready to roll.

size_t is nice too.

Changing the test db names is optional, I don't have strong opinions either way, but feel free to do it in this PR.

Makefile Show resolved Hide resolved
# ---------------------------------------------------------------------------- #

setup_file() {
export DBPATH="$BATS_FILE_TMPDIR/test-cli.sqlite";
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just use the basement test.sqlite for all tests for consistency. It's not a real problem to use arbitrary names though.

return EXIT_FAILURE;
}

for (unsigned long i = 0; i < attrPath.size() - 1; i++)
Copy link
Contributor

Choose a reason for hiding this comment

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

What you wrote here works perfectly. There's a shorthand helper that the nix headers provide if you want something a bit less verbose :

#include <nix/util.hh>
//...
  std::cout << nix::concatStringsSep( " ", attrPath ) << std::endl;

( feel free to use either, I just wanted to share a tip )

Co-authored-by: Matthew Kenigsberg <[email protected]>
tests/id2attrpath.bats Outdated Show resolved Hide resolved
@aakropotkin
Copy link
Contributor

@zmitchell since you're out for the day I'm going to merge this ( especially the test cases ) into other pkgdb get * subcommand ticket.

Thanks for helping out on this!

@aakropotkin aakropotkin deleted the get-attrpath branch September 19, 2023 20:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants