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

Reduce utility duplication by implementing document conversions #167

Closed
kzantow opened this issue Oct 13, 2022 · 0 comments · Fixed by #172
Closed

Reduce utility duplication by implementing document conversions #167

kzantow opened this issue Oct 13, 2022 · 0 comments · Fixed by #172

Comments

@kzantow
Copy link
Collaborator

kzantow commented Oct 13, 2022

As part of the suggested refactorings I proposed, it was noted that the utility functions such as idsearcher.go, which duplicate functionality for SPDX 2.1, 2.2, etc. could be made more generic using some reflection (and/or generics, which I don't think will help here).

Additionally, there is a desire to have the ability to convert between format versions. I propose using a "migration" approach for this and have created some functionality that does quite a lot of the work automatically, with heavy use of reflection.

I've created a draft PR here: kzantow-anchore#2 to illustrate how this can work, along with a full document conversion from a 2_2 to a 2_3 struct.

The gist of this approach is twofold:

  1. Use reflection to recursively map all fields from one struct to another based on convertible fields:
  • fields with the same name and convertible types are automatically handled
  • slices, maps, structs, and primitive types are currently handled
  • each struct may implement a ConvertFrom method, which runs after the automatic conversion to correct, add, or modify the resulting struct, being passed the struct from which it was originally converted
  1. Set up a migration list so each version (v2_2, v2_3) can implement a ConvertFrom method from the previous (and possibly next) version. Implement a function that, given a specific version and a specific target, walks the list through each intermediate version conversion.

A ConvertFrom function for v2_3.Document would look something like:

func (t *Document) ConvertFrom(in interface{}) error {
	if prior, ok := in.(v2_2.Document); ok {
           // do any specific stuff here
        }
        t.SPDXVersion = "SPDX-2.3"
	return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant