Skip to content

Commit

Permalink
make validate optional and use structs for prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Jul 9, 2024
1 parent fe51371 commit 401c86e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
31 changes: 23 additions & 8 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Manifest struct {
db *jsonwall.DB
}

// Read loads a Manifest from a file without performing any validation. The file
// is assumed to be both valid jsonwall and a valid Manifest (see [Validate]).
func Read(absPath string) (manifest *Manifest, err error) {
defer func() {
if err != nil {
Expand All @@ -68,21 +70,24 @@ func Read(absPath string) (manifest *Manifest, err error) {
}

manifest = &Manifest{db: jsonwallDB}
err = validate(manifest)
if err != nil {
return nil, err
}
return manifest, nil
}

func (manifest *Manifest) IteratePath(prefix string, f func(Path) error) (err error) {
func (manifest *Manifest) IteratePath(pathPrefix string, f func(Path) error) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("cannot read manifest: %s", err)
}
}()

iter, err := manifest.db.IteratePrefix(map[string]string{"kind": "path", "path": prefix})
prefix := struct {
Kind string `json:"kind"`
Path string `json:"path"`
}{
Kind: "path",
Path: pathPrefix,
}
iter, err := manifest.db.IteratePrefix(prefix)
if err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +137,14 @@ func (manifest *Manifest) IterateSlices(pkgName string, f func(Slice) error) (er
}
}()

iter, err := manifest.db.IteratePrefix(map[string]string{"kind": "slice", "name": pkgName})
prefix := struct {
Kind string `json:"kind"`
Name string `json:"name"`
}{
Kind: "slice",
Name: pkgName,
}
iter, err := manifest.db.IteratePrefix(&prefix)
if err != nil {
return err
}
Expand All @@ -150,7 +162,10 @@ func (manifest *Manifest) IterateSlices(pkgName string, f func(Slice) error) (er
return nil
}

func validate(manifest *Manifest) (err error) {
// Validate checks that the Manifest is valid. Note that to do that it has to
// load practically the whole manifest into memory and unmarshall all the
// entries.
func Validate(manifest *Manifest) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf(`invalid manifest: %s`, err)
Expand Down
12 changes: 7 additions & 5 deletions internal/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ var manifestTests = []struct {
{"jsonwall":"1.0","schema":"1.0","count":1}
{"kind":"content","slice":"pkg1_manifest","path":"/manifest/manifest.wall"}
`,
error: `cannot read manifest: invalid manifest: slice pkg1_manifest not found in slices`,
error: `invalid manifest: slice pkg1_manifest not found in slices`,
}, {
summary: "Package not found",
input: `
{"jsonwall":"1.0","schema":"1.0","count":1}
{"kind":"slice","name":"pkg1_manifest"}
`,
error: `cannot read manifest: invalid manifest: package "pkg1" not found in packages`,
error: `invalid manifest: package "pkg1" not found in packages`,
}, {
summary: "Path not found in contents",
input: `
{"jsonwall":"1.0","schema":"1.0","count":1}
{"kind":"path","path":"/dir/","mode":"01777","slices":["pkg1_myslice"]}
`,
error: `cannot read manifest: invalid manifest: path /dir/ has no matching entry in contents`,
error: `invalid manifest: path /dir/ has no matching entry in contents`,
}, {
summary: "Content and path have different slices",
input: `
Expand All @@ -90,7 +90,7 @@ var manifestTests = []struct {
{"kind":"path","path":"/dir/","mode":"01777","slices":["pkg1_myslice"]}
{"kind":"slice","name":"pkg1_myotherslice"}
`,
error: `cannot read manifest: invalid manifest: path /dir/ and content have diverging slices: \["pkg1_myslice"\] != \["pkg1_myotherslice"\]`,
error: `invalid manifest: path /dir/ and content have diverging slices: \["pkg1_myslice"\] != \["pkg1_myotherslice"\]`,
}, {
summary: "Content not found in paths",
input: `
Expand All @@ -99,7 +99,7 @@ var manifestTests = []struct {
{"kind":"package","name":"pkg1","version":"v1","sha256":"hash1","arch":"arch1"}
{"kind":"slice","name":"pkg1_myslice"}
`,
error: `cannot read manifest: invalid manifest: content path /dir/ has no matching entry in paths`,
error: `invalid manifest: content path /dir/ has no matching entry in paths`,
}}

func (s *S) TestRun(c *C) {
Expand All @@ -126,6 +126,8 @@ func (s *S) TestRun(c *C) {
c.Assert(test.input, DeepEquals, orderedInput, Commentf("input jsonwall lines should be ordered"))

mfest, err := manifest.Read(manifestPath)
c.Assert(err, IsNil)
err = manifest.Validate(mfest)
if test.error != "" {
c.Assert(err, ErrorMatches, test.error)
continue
Expand Down

0 comments on commit 401c86e

Please sign in to comment.