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 variants to AggregateType to support spec version 1.5 #681

Merged
merged 3 commits into from
Apr 29, 2024

Conversation

justahero
Copy link
Contributor

@justahero justahero commented Apr 29, 2024

This PR expands the aggregateType enum type with a few more variants.

  • change Algorithm, validation is similar as other enum types instead of raising exceptions
  • refactor construction of Algorithm, in tests as well
  • refactor AggregateType & expand variants, validated similar to Classification
  • remove InvalidEnumVariant error variant, validation is handled similar to other enum types

* change `Algorithm`, validation is similar as other enum types instead
  of raising exceptions
* refactor construction of `Algorithm`, in tests as well
* refactor `AggregateType` & expand variants, validated similar to
  `Classification`

Signed-off-by: Sebastian Ziebell <sebastian.ziebell@ferrous-systems.com>
@justahero justahero requested a review from a team as a code owner April 29, 2024 09:17
@justahero justahero mentioned this pull request Apr 29, 2024
34 tasks
@@ -709,7 +708,7 @@ mod test {
tools: None,
}),
}),
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
signature: Some(Signature::single("HS512", "abcdefgh")),
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to also have a constructor that accepts an enum instead of having to hardcode strings. That would surface errors at compile time, instead of at runtime during validation.

This applies to all similar enums with "unknown" variant. I assume this appears elsewhere in the codebase?

Do you think that is reasonable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reverted the change for the model type.

There is a pattern for these enum types, where the Unknown variant functions as the catch all for all (yet) unknown values. The enum types in models are typically constructed via a new_unchecked constructor that maps the a string to its associated variant. An accompanying validation function raises a ValidationError in case it's the Unknown variant. It might be a good idea to check the code base that this pattern (or a better alternative) is consistently used.

Copy link
Contributor

Choose a reason for hiding this comment

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

While I recognize the need for a conversion from a string, I would prefer to have the primary constructor to be used in the API to accept the enum type.

Incidentally, why not just make the conversion from an &str simply an impl From<&str>? I see that new_unchecked<A: AsRef<str>> is generic and works with String etc, but on the flip side it causes the function to be separately monomorphised for each type it accepts, increasing compilation time and binary size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to be consistent with the other enum types & how they use new_unchecked. It's not an ideal pattern to be used & there is room for improvement. I also removed the FromStr for Algorithm as this returns an Err rather than the Unknown variant.

My suggestion is to refactor this pattern to more idiomatic code in a separate PR. Instead of using new_unchecked all affected enum types should implement From<&str> then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good. I'll open an issue for that, and merge this as is for now.

Instead of passing in a `&str`, the `Algorithm` is used.

Signed-off-by: Sebastian Ziebell <sebastian.ziebell@ferrous-systems.com>
All other `enum` related types use a `new_unchecked` constructor that
does not raise an `Err` but rather collects the unknown value into an
`Unknown` variant.

Signed-off-by: Sebastian Ziebell <sebastian.ziebell@ferrous-systems.com>
@Shnatsel Shnatsel merged commit 48139bd into CycloneDX:main Apr 29, 2024
9 checks passed
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