Skip to content

Commit

Permalink
refactor: renaming
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 committed May 7, 2024
1 parent fee8c55 commit f83ca44
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 58 deletions.
4 changes: 2 additions & 2 deletions pkg/dependency/parser/c/conan/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func (p *Parser) parseV1(lock LockFile) ([]ftypes.Package, []ftypes.Dependency,

// Determine if the package is a direct dependency or not
direct := slices.Contains(directDeps, i)
lib.Relationship = lo.Ternary(direct, ftypes.RelationshipDirect, ftypes.RelationshipIndirect)
pkg.Relationship = lo.Ternary(direct, ftypes.RelationshipDirect, ftypes.RelationshipIndirect)

parsed[i] = lib
parsed[i] = pkg
}

// Parse dependency graph
Expand Down
6 changes: 3 additions & 3 deletions pkg/dependency/parser/conda/environment/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
for _, dep := range env.Dependencies {
pkg := p.toPackage(dep)
// Skip empty libs
if lib.Name == "" {
if pkg.Name == "" {
continue
}
pkgs = append(pkgs, lib)
pkgs = append(pkgs, pkg)
}

sort.Sort(pkgs)
return pkgs, nil, nil
}

func (p *Parser) toLibrary(dep Dependency) ftypes.Package {
func (p *Parser) toPackage(dep Dependency) ftypes.Package {
name, ver := p.parseDependency(dep.Value)
if ver == "" {
p.once.Do(func() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/golang/sum/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc

// go.sum records and sorts all non-major versions
// with the latest version as last entry
uniqueLibs[s[0]] = strings.TrimSuffix(strings.TrimPrefix(s[1], "v"), "/go.mod")
uniquePkgs[s[0]] = strings.TrimSuffix(strings.TrimPrefix(s[1], "v"), "/go.mod")
}
if err := scanner.Err(); err != nil {
return nil, nil, xerrors.Errorf("scan error: %w", err)
}

for k, v := range uniqueLibs {
for k, v := range uniquePkgs {
pkgs = append(pkgs, ftypes.Package{
ID: dependency.ID(ftypes.GoModule, k, v),
Name: k,
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/julia/manifest/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestParse(t *testing.T) {
require.NoError(t, err)

sort.Sort(ftypes.Packages(tt.want))
assert.Equal(t, tt.want, gotLibs)
assert.Equal(t, tt.want, gotPkgs)
if tt.wantDeps != nil {
sort.Sort(ftypes.Dependencies(tt.wantDeps))
assert.Equal(t, tt.wantDeps, gotDeps)
Expand Down
32 changes: 16 additions & 16 deletions pkg/dependency/parser/nodejs/npm/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ func (p *Parser) parseV2(packages map[string]Package) ([]ftypes.Package, []ftype

// There are cases when similar libraries use same dependencies
// we need to add location for each these dependencies
if savedPkgs, ok := libs[pkgID]; ok {
savedLib.Dev = savedLib.Dev && pkg.Dev
if savedLib.Relationship == ftypes.RelationshipIndirect && !pkgIndirect {
savedLib.Relationship = ftypes.RelationshipDirect
if savedPkg, ok := pkgs[pkgID]; ok {
savedPkg.Dev = savedPkg.Dev && pkg.Dev
if savedPkg.Relationship == ftypes.RelationshipIndirect && !pkgIndirect {
savedPkg.Relationship = ftypes.RelationshipDirect
}

if ref.URL != "" && !slices.Contains(savedLib.ExternalReferences, ref) {
savedLib.ExternalReferences = append(savedLib.ExternalReferences, ref)
sortExternalReferences(savedLib.ExternalReferences)
if ref.URL != "" && !slices.Contains(savedPkg.ExternalReferences, ref) {
savedPkg.ExternalReferences = append(savedPkg.ExternalReferences, ref)
sortExternalReferences(savedPkg.ExternalReferences)
}

savedLib.Locations = append(savedLib.Locations, location)
sort.Sort(savedLib.Locations)
savedPkg.Locations = append(savedPkg.Locations, location)
sort.Sort(savedPkg.Locations)

libs[pkgID] = savedLib
pkgs[pkgID] = savedPkg
continue
}

Expand All @@ -159,7 +159,7 @@ func (p *Parser) parseV2(packages map[string]Package) ([]ftypes.Package, []ftype
ExternalReferences: lo.Ternary(ref.URL != "", []ftypes.ExternalRef{ref}, nil),
Locations: []ftypes.Location{location},
}
libs[pkgID] = lib
pkgs[pkgID] = lib

// npm builds graph using optional deps. e.g.:
// └─┬ [email protected]
Expand All @@ -186,7 +186,7 @@ func (p *Parser) parseV2(packages map[string]Package) ([]ftypes.Package, []ftype

}

return maps.Values(libs), deps
return maps.Values(pkgs), deps
}

// for local package npm uses links. e.g.:
Expand Down Expand Up @@ -299,7 +299,7 @@ func (p *Parser) parseV1(dependencies map[string]Dependency, versions map[string
},
},
}
pkgs = append(pkgs, lib)
pkgs = append(pkgs, pkg)

dependsOn := make([]string, 0, len(dep.Requires))
for libName, requiredVer := range dep.Requires {
Expand All @@ -323,15 +323,15 @@ func (p *Parser) parseV1(dependencies map[string]Dependency, versions map[string

if len(dependsOn) > 0 {
deps = append(deps, ftypes.Dependency{
ID: packageID(lib.Name, lib.Version),
ID: packageID(pkg.Name, pkg.Version),
DependsOn: dependsOn,
})
}

if dep.Dependencies != nil {
// Recursion
childLibs, childDeps := p.parseV1(dep.Dependencies, maps.Clone(versions))
pkgs = append(pkgs, childLibs...)
childpkgs, childDeps := p.parseV1(dep.Dependencies, maps.Clone(versions))
pkgs = append(pkgs, childpkgs...)
deps = append(deps, childDeps...)
}
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/dependency/parser/nuget/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
continue
}

pkg := ftypes.Package{
pkgs = append(pkgs, ftypes.Package{
Name: pkg.ID,
Version: pkg.Version,
}

pkgs = append(pkgs, lib)
})
}

return utils.UniquePackages(pkgs), nil, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/php/composer/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestParse(t *testing.T) {
{
name: "happy path",
file: "testdata/composer_happy.lock",
wantLibs: composerLibs,
wantPkgs: composerPkgs,
wantDeps: composerDeps,
},
}
Expand All @@ -164,7 +164,7 @@ func TestParse(t *testing.T) {
gotLibs, gotDeps, err := NewParser().Parse(f)
require.NoError(t, err)

assert.Equal(t, tt.wantLibs, gotLibs)
assert.Equal(t, tt.wantPkgs, gotLibs)
assert.Equal(t, tt.wantDeps, gotDeps)
})
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dependency/parser/python/poetry/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ func TestParser_Parse(t *testing.T) {
{
name: "normal",
file: "testdata/poetry_normal.lock",
wantLibs: poetryNormal,
wantPkgs: poetryNormal,
wantErr: assert.NoError,
},
{
name: "many",
file: "testdata/poetry_many.lock",
wantLibs: poetryMany,
wantPkgs: poetryMany,
wantDeps: poetryManyDeps,
wantErr: assert.NoError,
},
{
name: "flask",
file: "testdata/poetry_flask.lock",
wantLibs: poetryFlask,
wantPkgs: poetryFlask,
wantDeps: poetryFlaskDeps,
wantErr: assert.NoError,
},
Expand All @@ -51,7 +51,7 @@ func TestParser_Parse(t *testing.T) {
if !tt.wantErr(t, err, fmt.Sprintf("Parse(%v)", tt.file)) {
return
}
assert.Equalf(t, tt.wantLibs, gotLibs, "Parse(%v)", tt.file)
assert.Equalf(t, tt.wantPkgs, gotLibs, "Parse(%v)", tt.file)
assert.Equalf(t, tt.wantDeps, gotDeps, "Parse(%v)", tt.file)
})
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/dependency/parser/rust/binary/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ func TestParse(t *testing.T) {
{
name: "ELF",
inputFile: "testdata/test.elf",
want: libs,
want: pkgs,
wantDeps: deps,
},
{
name: "PE",
inputFile: "testdata/test.exe",
want: libs,
want: pkgs,
wantDeps: deps,
},
{
name: "Mach-O",
inputFile: "testdata/test.macho",
want: libs,
want: pkgs,
wantDeps: deps,
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/swift/cocoapods/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestParse(t *testing.T) {
{
name: "happy path",
inputFile: "testdata/happy.lock",
wantLibs: []ftypes.Package{
wantPkgs: []ftypes.Package{
{
ID: "[email protected]",
Name: "AppCenter",
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestParse(t *testing.T) {
gotLibs, gotDeps, err := cocoapods.NewParser().Parse(f)
require.NoError(t, err)

assert.Equal(t, tt.wantLibs, gotLibs)
assert.Equal(t, tt.wantPkgs, gotLibs)
assert.Equal(t, tt.wantDeps, gotDeps)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/swift/swift/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
pins = lockFile.Pins
}
for _, pin := range pins {
name := libraryName(pin, lockFile.Version)
name := pkgName(pin, lockFile.Version)

// Skip packages for which we cannot resolve the version
if pin.State.Version == "" && pin.State.Branch == "" {
Expand Down
16 changes: 8 additions & 8 deletions pkg/dependency/parser/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestUniqueLibraries(t *testing.T) {
}{
{
name: "happy path merge locations",
libs: []ftypes.Package{
pkgs: []ftypes.Package{
{
ID: "[email protected]",
Name: "asn1",
Expand All @@ -38,7 +38,7 @@ func TestUniqueLibraries(t *testing.T) {
},
},
},
wantLibs: []ftypes.Package{
wantPkgs: []ftypes.Package{
{
ID: "[email protected]",
Name: "asn1",
Expand All @@ -58,7 +58,7 @@ func TestUniqueLibraries(t *testing.T) {
},
{
name: "happy path Dev and Root deps",
libs: []ftypes.Package{
pkgs: []ftypes.Package{
{
ID: "[email protected]",
Name: "asn1",
Expand All @@ -72,7 +72,7 @@ func TestUniqueLibraries(t *testing.T) {
Dev: false,
},
},
wantLibs: []ftypes.Package{
wantPkgs: []ftypes.Package{
{
ID: "[email protected]",
Name: "asn1",
Expand All @@ -83,7 +83,7 @@ func TestUniqueLibraries(t *testing.T) {
},
{
name: "happy path Root and Dev deps",
libs: []ftypes.Package{
pkgs: []ftypes.Package{
{
ID: "[email protected]",
Name: "asn1",
Expand All @@ -97,7 +97,7 @@ func TestUniqueLibraries(t *testing.T) {
Dev: true,
},
},
wantLibs: []ftypes.Package{
wantPkgs: []ftypes.Package{
{
ID: "[email protected]",
Name: "asn1",
Expand All @@ -110,8 +110,8 @@ func TestUniqueLibraries(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotLibs := UniquePackages(tt.libs)
require.Equal(t, tt.wantLibs, gotLibs)
gotLibs := UniquePackages(tt.pkgs)
require.Equal(t, tt.wantPkgs, gotLibs)
})
}
}
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ func (ag AnalyzerGroup) PostAnalyze(ctx context.Context, compositeFS *CompositeF
// The analysis result could contain packages listed in SBOM.
// The files of those packages don't have to be analyzed.
// This is especially helpful for expensive post-analyzers such as the JAR analyzer.
if lib.FilePath != "" {
skippedFiles = append(skippedFiles, lib.FilePath)
if pkg.FilePath != "" {
skippedFiles = append(skippedFiles, pkg.FilePath)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/php/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (a composerAnalyzer) mergeComposerJson(fsys fs.FS, dir string, app *types.A

for i, pkg := range app.Packages {
// Identify the direct/transitive dependencies
if _, ok := p[lib.Name]; ok {
if _, ok := p[pkg.Name]; ok {
app.Packages[i].Relationship = types.RelationshipDirect
} else {
app.Packages[i].Indirect = true
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/python/packaging/packaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (a packagingAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAna
func (a packagingAnalyzer) fillAdditionalData(fsys fs.FS, app *types.Application) error {
for i, pkg := range app.Packages {
var licenses []string
for _, lic := range lib.Licenses {
for _, lic := range pkg.Licenses {
// Parser adds `file://` prefix to filepath from `License-File` field
// We need to read this file to find licenses
// Otherwise, this is the name of the license
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/python/poetry/poetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (a poetryAnalyzer) mergePyProject(fsys fs.FS, dir string, app *types.Applic

for i, pkg := range app.Packages {
// Identify the direct/transitive dependencies
if _, ok := p[lib.Name]; ok {
if _, ok := p[pkg.Name]; ok {
app.Packages[i].Relationship = types.RelationshipDirect
} else {
app.Packages[i].Indirect = true
Expand Down
10 changes: 5 additions & 5 deletions pkg/fanal/applier/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,17 @@ func ApplyLayers(layers []ftypes.BlobInfo) ftypes.ArtifactDetail {
for _, app := range mergedLayer.Applications {
for i, pkg := range app.Packages {
// Skip lookup for SBOM
if lo.IsEmpty(lib.Layer) {
originLayerDigest, originLayerDiffID := lookupOriginLayerForLib(app.FilePath, lib, layers)
if lo.IsEmpty(pkg.Layer) {
originLayerDigest, originLayerDiffID := lookupOriginLayerForLib(app.FilePath, pkg, layers)
app.Packages[i].Layer = ftypes.Layer{
Digest: originLayerDigest,
DiffID: originLayerDiffID,
}
}
if lib.Identifier.PURL == nil {
app.Packages[i].Identifier.PURL = newPURL(app.Type, types.Metadata{}, lib)
if pkg.Identifier.PURL == nil {
app.Packages[i].Identifier.PURL = newPURL(app.Type, types.Metadata{}, pkg)
}
app.Packages[i].Identifier.UID = calcPkgUID(app.FilePath, lib)
app.Packages[i].Identifier.UID = calcPkgUID(app.FilePath, pkg)
}
}

Expand Down

0 comments on commit f83ca44

Please sign in to comment.