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

Add template func hasField, update README #1754

Merged
merged 4 commits into from
Apr 21, 2023
Merged

Conversation

jlehman9
Copy link
Contributor

  • Adds the template function hasField.
    • Allows template to check if a field is defined in a given JSON struct.
  • Update README.md to document custom template functions.

This came from an issue I faced trying to write a CSV template function that grabbed vendor/author/publisher data. Unfortunately those fields aren't always defined for every package, and attempting to access them will cause Syft to exit with something like
* template: csv.tmpl:3:55: executing "csv.tmpl" at <.Metadata.Author>: can't evaluate field Author in type interface {}

I figured it would also be good to document the where the functions are defined in the README.md so others can see they exist and offer new ones.

Thanks to @kzantow for the help discovering this solution!

@kzantow
Copy link
Contributor

kzantow commented Apr 20, 2023

Hey, thanks @jlehman9 ! would you be able to add a simple test that executes this function with a template?

@kzantow kzantow changed the title Add template func hasField, update README Add template func hasField, update README Apr 20, 2023
@jlehman9
Copy link
Contributor Author

Hey, thanks @jlehman9 ! would you be able to add a simple test that executes this function with a template?

I have no idea what I'm doing since I don't really know Go, but I think I've got tests working that test the hasField function. Let me know if there is anything else I can do/fix/etc.

@kzantow
Copy link
Contributor

kzantow commented Apr 20, 2023

It looks like there are quite a few failing tests due to the changes, maybe just make a new test?

Also, you'll need to sign-off your commits.

- Adds the template function hasField.
	- Allows template to check if a field is defined in a struct
- Update README.md to document custom template functions.

Signed-off-by: Lehman, Alex <alex.lehman@gtri.gatech.edu>
Signed-off-by: Lehman, Alex <alex.lehman@gtri.gatech.edu>
This reverts commit df49fa9.

Signed-off-by: Lehman, Alex <alex.lehman@gtri.gatech.edu>
Create a new test and supporting test functions for the new hasField template function

Signed-off-by: Lehman, Alex <alex.lehman@gtri.gatech.edu>
Copy link
Contributor

@kzantow kzantow left a comment

Choose a reason for hiding this comment

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

LGTM

@jlehman9
Copy link
Contributor Author

I believe I've undone my previous poor commit, created a separate test, re-signed the bad commit, and ensured all of the tests pass on my local machine. Hopefully all is good?

Previously I had improperly assumed that the failing tests were all due to my local environment, mainly due to not having docker daemon installed since I usually run docker from minikube but the tests couldn't see that. Sorry about that!

@kzantow kzantow merged commit b2b332e into anchore:main Apr 21, 2023
9 checks passed
@kzantow
Copy link
Contributor

kzantow commented Apr 21, 2023

Thanks for the contribution @jlehman9!

spiffcs added a commit that referenced this pull request Apr 26, 2023
* main:
  chore(deps): bump modernc.org/sqlite from 1.21.2 to 1.22.0 (#1758)
  chore: go-rpmdb update (#1757)
  chore(deps): bump github.com/CycloneDX/cyclonedx-go from 0.7.1-0.20221222100750-41a1ac565cce to 0.7.1 (#1706)
  fix: Improve pnpm support (#1752)
  feat: Add template func `hasField` (#1754)
  fix: only cache java packages and not source content (#1750)

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
GijsCalis pushed a commit to GijsCalis/syft that referenced this pull request Feb 19, 2024
Signed-off-by: Lehman, Alex <alex.lehman@gtri.gatech.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants