Fix for errors+failures parsing package.json

closes: #230

Signed-off-by: Toure Dunnon <toure.dunnon@anchore.com>
This commit is contained in:
Toure Dunnon 2020-10-21 10:44:05 -04:00
parent 931c796158
commit 5b08616e47
4 changed files with 13 additions and 26 deletions

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/anchore/go-testutils v0.0.0-20200925183923-d5f45b0d3c04
github.com/anchore/go-version v1.2.2-0.20200701162849-18adb9c92b9b
github.com/anchore/stereoscope v0.0.0-20200925184903-c82da54e98fe
github.com/apex/log v1.3.0
github.com/bmatcuk/doublestar v1.3.1
github.com/docker/docker v17.12.0-ce-rc1.0.20200309214505-aa6a9891b09c+incompatible
github.com/dustin/go-humanize v1.0.0

View File

@ -35,36 +35,32 @@ type Author struct {
URL string `json:"url" mapstruct:"url"`
}
// match example: "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)"
// ---> name: "Isaac Z. Schlueter" email: "i@izs.me" url: "http://blog.izs.me"
var authorPattern = regexp.MustCompile(`^\s*(?P<name>[^<(]*)(\s+<(?P<email>.*)>)?(\s\((?P<url>.*)\))?\s*$`)
func (a *Author) UnmarshalJSON(b []byte) error {
var authorStr string
var fields map[string]string
var author 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
var fields map[string]string
var author Author
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.MatchCaptureGroups(authorPattern, authorStr)
}
// translate the map into a structure
if err := mapstructure.Decode(fields, &author); err != nil {
return fmt.Errorf("unable to decode package.json author: %w", err)
}
*a = author
} else {
// parse out "name <email> (url)" into an Author struct
var fields = internal.MatchCaptureGroups(authorPattern, authorStr)
*a = Author{
Name: fields["name"],
Email: fields["email"],
URL: fields["url"],
}
}
if a.Name == "" {
return fmt.Errorf("package.json author name is empty")
}
*a = author
return nil
}

View File

@ -62,6 +62,7 @@ func TestParsePackageJSON(t *testing.T) {
}
for _, d := range deep.Equal(actual[0], test.ExpectedPkg) {
t.Errorf("diff: %+v", d)
}
})

View File

@ -1,11 +0,0 @@
package pkg
type MetadataType string
const (
UnknownMetadataType MetadataType = "UnknownMetadata"
ApkMetadataType MetadataType = "apk-metadata"
DpkgMetadataType MetadataType = "dpkg-metadata"
GemgMetadataType MetadataType = "gem-metadata"
RpmdbMetadataType MetadataType = "rpmdb-metadata"
)