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

fix(jar): remove duplicates of jar libs #221

Merged
merged 5 commits into from
May 31, 2023

Conversation

DmitriyLewen
Copy link
Collaborator

@DmitriyLewen DmitriyLewen commented May 30, 2023

@DmitriyLewen DmitriyLewen marked this pull request as ready for review May 30, 2023 07:58
@DmitriyLewen DmitriyLewen requested a review from knqyf263 May 30, 2023 07:58
@DmitriyLewen DmitriyLewen self-assigned this May 30, 2023
Comment on lines 422 to 434
uniqLibs := map[string]types.Library{}
for _, lib := range libs {
// comparing ArtifactID and GroupID
l, ok := uniqLibs[lib.Name]
// comparing Version and FilePath
if ok && lib.Version == l.Version && lib.FilePath == l.FilePath {
continue
}
uniqLibs[lib.Name] = lib
}
libSlice := maps.Values(uniqLibs)
sort.Sort(types.Libraries(libSlice))
return libSlice
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about this approach?

Suggested change
uniqLibs := map[string]types.Library{}
for _, lib := range libs {
// comparing ArtifactID and GroupID
l, ok := uniqLibs[lib.Name]
// comparing Version and FilePath
if ok && lib.Version == l.Version && lib.FilePath == l.FilePath {
continue
}
uniqLibs[lib.Name] = lib
}
libSlice := maps.Values(uniqLibs)
sort.Sort(types.Libraries(libSlice))
return libSlice
return := lo.FindDuplicatesBy(libs, func(lib types.Library) {
return fmt.Sprintf("%s::%s::%s", lib.Name, lib.Version, lib.FilePath)
})

Copy link
Collaborator Author

@DmitriyLewen DmitriyLewen May 30, 2023

Choose a reason for hiding this comment

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

lo.FindDuplicatesBy takes only duplicates and skips unique libs.
lo.FindUniquesBy takes only unique libs and skips duplicates.

types.Library is not comparable type, so we can't use slice.Contains function to find duplicates in libs.

But i found 1 way - we will take all uniq libs(lo.FindUniquesBy) and one lib for each duplicate(lo.FindDuplicatesBy) - e0a855f
I added tests so you can see it.

Copy link
Collaborator Author

@DmitriyLewen DmitriyLewen May 30, 2023

Choose a reason for hiding this comment

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

or we can use map[string]types.Library here -

var libs []types.Library

and check for unique libs when append it into this map.

Copy link
Collaborator Author

@DmitriyLewen DmitriyLewen May 30, 2023

Choose a reason for hiding this comment

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

FYI this and this testscases are contained in TestParse.
I have updated io.quarkus.gizmo.gizmo-1.1.1.Final.jar test file to move testcase when jar contains 2 jars with same name and different versions in TestParse and make removeLibraryDuplicates function not exportable.

Changes in this commit - 6e2198e

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, yes, I was confused with UniqBy. How about that? https://github.com/samber/lo#uniqby

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I need to check docs better... It works! Thanks for the advice!
Updated in 9346bf2

@knqyf263 knqyf263 merged commit 12c25a7 into aquasecurity:main May 31, 2023
Sq34sy pushed a commit to Sq34sy/go-dep-parser that referenced this pull request Jul 28, 2023
Sq34sy pushed a commit to Sq34sy/go-dep-parser that referenced this pull request Jul 28, 2023
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.

2 participants