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

Updated parseEntityTransformParams to handle keys with '.' in them #11130

Merged
merged 7 commits into from May 9, 2022

Conversation

webark
Copy link
Contributor

@webark webark commented Apr 27, 2022

Signed-off-by: Mark David Avery mark@webark.cc

Hey, I just made a Pull Request!

When writing queries against the entity list, this will enable using keys with . in them. This includes annotations such as backstage.io/source-location and the like.

✔️ Checklist

  • A changeset describing the change and affected packages. (more info)
  • Added or updated documentation
  • Tests for new functionality and regression tests for bug fixes
  • Screenshots attached (for UI changes)
  • All your commits have a Signed-off-by line in the message. (more info)

…them.

Signed-off-by: Mark David Avery <mark@webark.cc>
@webark webark requested a review from a team as a code owner April 27, 2022 18:16
@github-actions github-actions bot added awaiting-review area:catalog Related to the Catalog Project Area labels Apr 27, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Apr 27, 2022

Changed Packages

Package Name Package Path Changeset Bump Current Version
@backstage/plugin-catalog-backend plugins/catalog-backend patch v1.1.2-next.1

@webark
Copy link
Contributor Author

webark commented Apr 27, 2022

…ly handle multiple dot notated keys

Also added additional tests to handle varried scenarios.

Signed-off-by: Mark David Avery <mark@webark.cc>
@webark
Copy link
Contributor Author

webark commented May 4, 2022

edited: tweaked some of the implementations from my original posting (tweaked it again to be a reduce)

@freben so the two implementations that I settled on where

function getPathArrayAndValue(input: Entity, field: string) {
  return field.split('.').reduce(
    ([pathArray, inputSubset], pathPart, index, fieldParts) => {
      if (Object.hasOwn(inputSubset, pathPart)) {
        return [pathArray.concat(pathPart), inputSubset[pathPart]];
      } else if (fieldParts[index + 1] !== undefined) {
        fieldParts[index + 1] = `${pathPart}.${fieldParts[index + 1]}`;
        return [pathArray, inputSubset];
      }

      return [pathArray, undefined];
    },
    [[] as string[], input as any],
  );
}

and

function getPathArray(input: Entity, field: string) {
  return field.split('.').reduce((pathArray, pathPart, index, fieldParts) => {
    if (lodash.has(input, pathArray.concat(pathPart))) {
      pathArray.push(pathPart);
    } else if (fieldParts[index + 1] !== undefined) {
      fieldParts[index + 1] = `${pathPart}.${fieldParts[index + 1]}`;
    }

    return pathArray;
  }, [] as string[]);
}

My one concern is that we arent using lodash to retrieve or check (with _.has) the key, but we will be using it to set it later on, and if that could be an area for odd bugs to arise.

I'm open to your call and feedback @freben . thanks for taking the time! 😊

webark added 2 commits May 4, 2022 14:51
…s the path array

Signed-off-by: Mark David Avery <mark@webark.cc>
Signed-off-by: Mark David Avery <mark@webark.cc>
@webark
Copy link
Contributor Author

webark commented May 4, 2022

I ended up just pushing the getPathArrayAndValue option since that was what you were suggesting. If we want to move back to the lodash.has approach, I'm fine with that and can update it as such. Thanks!

@webark
Copy link
Contributor Author

webark commented May 5, 2022

Oh, hasOwn was introduced in 16, and we're still supporting 14. Makes sense. I'll update thatt.

@github-actions
Copy link
Contributor

github-actions bot commented May 5, 2022

Thanks for the contribution!
All commits need to be DCO signed before merging. Please refer to the the DCO section in CONTRIBUTING.md or the DCO status for more info.

Signed-off-by: Mark David Avery <mark@webark.cc>
@webark webark force-pushed the entity-query-with-dot-params branch from 86b7d9c to 74c218a Compare May 5, 2022 19:06
@webark
Copy link
Contributor Author

webark commented May 5, 2022

OK, I fixed that commit that wasn't signed.

Let me know if you want me to squash the commits, or if that's something that you will do when you merge/rebase in the PR.

function getPathArrayAndValue(input: Entity, field: string) {
return field.split('.').reduce(
([pathArray, inputSubset], pathPart, index, fieldParts) => {
if (inputSubset.hasOwnProperty(pathPart)) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you can do this in a reduce easily - before even going into the if statements you need to check basically that typeof inputSubset === 'object' or similar, and if it isn't, bail out early and return undefined - because there is no point in trying to descend into a null or a number or so. So this should probably be a for..of loop instead over the parts, or similar

Copy link
Member

Choose a reason for hiding this comment

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

You could write a test that tries to fetch spec.type, and then a test that tries to fetch spec.type.doesnotexist

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh sure. I was using a "hasOwn" before, but that was not supported in node 14. 😞

I'll add that test.

I had a for of loop originally, but it felt to cumbersome. I'll switch it back if that's preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@freben so

You could write a test that tries to fetch spec.type, and then a test that tries to fetch spec.type.doesnotexist

hasOwnProperty exists on all javascript types except null and undefined. So I added some tests before I thought of the null and undefined scenarios, and will update to handle those later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I switched it to loash's hasIn which seems to be a polyfill for 'hasOwn`. I like this, cause it serves the need of using lodah to check the key, as well as set the key, so there's less likely to be an odd bug between the two.

I kept it as a reduce for now. I could go back to a for of, that was a little like

function getPathArray(input: Entity, field: string) {
  const pathArray = [];
  let currentPathPart = '';

  for (const pathPart of field.split('.')) {
    currentPathPart += pathPart;

    if (lodash.has(input, pathArray.concat(currentPathPart))) {
      pathArray.push(currentPathPart);
      currentPathPart = '';
    } else {
      currentPathPart += '.';
    }
  }

  return pathArray;
}

Let me know if you want to be to go back to that approach, but with stepping through the object each cycle to get the value.

… under values

Signed-off-by: Mark David Avery <mark@webark.cc>
Signed-off-by: Mark David Avery <mark@webark.cc>
@freben freben merged commit ac5b0e5 into backstage:master May 9, 2022
@webark webark deleted the entity-query-with-dot-params branch October 20, 2022 16:54
@suuus
Copy link
Contributor

suuus commented Oct 24, 2022

Thank you for contributing during Hacktoberfest 🍂! You can claim the Backstage Hacktoberfest Holopin at https://bck.st/hacktoberfest-holopin 🙌🏻

@webark webark mentioned this pull request Mar 22, 2023
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:catalog Related to the Catalog Project Area
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants