Skip to content

Commit

Permalink
refactor(pom): update logic to detect dep location
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Jan 24, 2024
1 parent 45a49ec commit 0dce852
Showing 1 changed file with 20 additions and 37 deletions.
57 changes: 20 additions & 37 deletions pkg/java/pom/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pom
import (
"encoding/xml"
"fmt"
"golang.org/x/xerrors"
"io"
"maps"
"reflect"
Expand Down Expand Up @@ -310,51 +311,33 @@ func (props *properties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error
func (deps *pomDependencies) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
*deps = pomDependencies{}
for {
var dep pomDependency
err := d.Decode(&dep)
if err == io.EOF {
break
} else if err != nil {
return err
token, err := d.Token()
if err != nil {
if err == io.EOF {
break // End of file, exit loop
}
return xerrors.Errorf("Error decoding XML: %w")
}

dep.EndLine, _ = d.InputPos()
dep.StartLine = dep.EndLine - lineNumberShift(dep)

(*deps).Dependency = append((*deps).Dependency, dep)
}
return nil
}

// lineNumberShift defines number of lines used by dependency to shift end line
// This is necessary because InputPos() returns line number of the `dependency` end tag (</dependency>).
func lineNumberShift(dep pomDependency) int {
shift := 1 // Compensate </dependency> tag
switch t := token.(type) {
case xml.StartElement:
if t.Name.Local == "dependency" {
var dep pomDependency
dep.StartLine, _ = d.InputPos() // <dependency> tag starts

for _, v := range []string{dep.GroupID, dep.ArtifactID, dep.Version, dep.Scope} {
if v != "" {
shift += 1
}
}
// Decode <dependency>
err = d.DecodeElement(&dep, &t)
if err != nil {
return xerrors.Errorf("Error decoding dependency: %w")
}

if dep.Optional {
shift += 1
}
dep.EndLine, _ = d.InputPos()

if dep.Exclusions.Exclusion != nil {
shift += 2 // <exclusions> + </exclusions> tags
for _, exclusion := range dep.Exclusions.Exclusion {
shift += 2 // <exclusion> + </exclusions> tags
if exclusion.GroupID != "" {
shift += 1
}
if exclusion.ArtifactID != "" {
shift += 1
(*deps).Dependency = append((*deps).Dependency, dep)
}
}
}

return shift
return nil
}

func findDep(name string, depManagement []pomDependency) (pomDependency, bool) {
Expand Down

0 comments on commit 0dce852

Please sign in to comment.