Skip to content

Commit

Permalink
Improved parsing of package.json 'author' section
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Radkowski <piotr@n0de.pl>
  • Loading branch information
Piotr Radkowski committed Oct 7, 2024
1 parent 13c6876 commit aed89aa
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions syft/pkg/cataloger/javascript/parse_package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ func parsePackageJSON(_ context.Context, _ file.Resolver, _ *generic.Environment

func (a *author) UnmarshalJSON(b []byte) error {
var authorStr string
var fields map[string]string
var auth author

if err := json.Unmarshal(b, &authorStr); err != nil {
// string parsing did not work, assume a map was given
// for more information: https://docs.npmjs.com/files/package.json#people-fields-author-contributors
if err := json.Unmarshal(b, &authorStr); err == nil {
// successfully parsed as a string, now parse that string into fields
fields := internal.MatchNamedCaptureGroups(authorPattern, authorStr)
if err := mapstructure.Decode(fields, &auth); err != nil {
return fmt.Errorf("unable to decode package.json author: %w", err)
}
} else {
// it's a map that may contain fields of various data types (not just strings)
var fields map[string]interface{}
if err := json.Unmarshal(b, &fields); err != nil {
return fmt.Errorf("unable to parse package.json author: %w", err)
}
} else {
// parse out "name <email> (url)" into an author struct
fields = internal.MatchNamedCaptureGroups(authorPattern, authorStr)
}

// translate the map into a structure
if err := mapstructure.Decode(fields, &auth); err != nil {
return fmt.Errorf("unable to decode package.json author: %w", err)
if err := mapstructure.Decode(fields, &auth); err != nil {
return fmt.Errorf("unable to decode package.json author: %w", err)
}
}

*a = auth
Expand Down

0 comments on commit aed89aa

Please # to comment.