-
Notifications
You must be signed in to change notification settings - Fork 90
jsonblob: implement Load as documented #1559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BradLugo
wants to merge
2
commits into
quay:main
Choose a base branch
from
BradLugo:blugo/jsonblob-load
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package jsonblob | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "github.com/google/uuid" | ||
| "io" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| ) | ||
|
|
||
| // NewLoader creates a new Loader from the provided [io.Reader]. | ||
| func NewLoader(r io.Reader) (*Loader, error) { | ||
| l := Loader{ | ||
| dec: json.NewDecoder(r), | ||
| cur: uuid.Nil, | ||
| } | ||
| return &l, nil | ||
| } | ||
|
|
||
| // Loader is an iterator that returns a series of [Entry]. | ||
| // | ||
| // Users should call [*Loader.Next] until it reports false, then check for | ||
| // errors via [*Loader.Err]. | ||
| type Loader struct { | ||
| err error | ||
| e *Entry | ||
|
|
||
| dec *json.Decoder | ||
| next *Entry | ||
| de diskEntry | ||
| cur uuid.UUID | ||
| } | ||
|
|
||
| // Next reports whether there's an [Entry] to be processed. | ||
| func (l *Loader) Next() bool { | ||
| if l.err != nil { | ||
| return false | ||
| } | ||
|
|
||
| for l.err = l.dec.Decode(&l.de); l.err == nil; l.err = l.dec.Decode(&l.de) { | ||
| id := l.de.Ref | ||
| // If we just hit a new Entry, promote the current one. | ||
| if id != l.cur { | ||
| l.e = l.next | ||
| l.next = &Entry{} | ||
| l.next.Updater = l.de.Updater | ||
| l.next.Fingerprint = l.de.Fingerprint | ||
| l.next.Date = l.de.Date | ||
| } | ||
| switch l.de.Kind { | ||
| case driver.VulnerabilityKind: | ||
| vuln := claircore.Vulnerability{} | ||
| if err := json.Unmarshal(l.de.Vuln.buf, &vuln); err != nil { | ||
| l.err = err | ||
| return false | ||
| } | ||
| l.next.Vuln = append(l.next.Vuln, &vuln) | ||
| case driver.EnrichmentKind: | ||
| en := driver.EnrichmentRecord{} | ||
| if err := json.Unmarshal(l.de.Enrichment.buf, &en); err != nil { | ||
| l.err = err | ||
| return false | ||
| } | ||
| l.next.Enrichment = append(l.next.Enrichment, en) | ||
| } | ||
| // If this was an initial diskEntry, promote the ref. | ||
| if id != l.cur { | ||
| l.cur = id | ||
| // If we have an Entry ready, report that. | ||
| if l.e != nil { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| l.e = l.next | ||
| return true | ||
| } | ||
|
|
||
| // Entry returns the latest loaded [Entry]. | ||
| func (l *Loader) Entry() *Entry { | ||
| return l.e | ||
| } | ||
|
|
||
| // Err is the latest encountered error. | ||
| func (l *Loader) Err() error { | ||
| // Don't report EOF as an error. | ||
| if errors.Is(l.err, io.EOF) { | ||
| return nil | ||
| } | ||
| return l.err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package jsonblob | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "golang.org/x/sync/errgroup" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| "github.com/quay/claircore/test" | ||
| ) | ||
|
|
||
| func TestLoader(t *testing.T) { | ||
| ctx := context.Background() | ||
| a, err := New() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| var want, got struct { | ||
| V []*claircore.Vulnerability | ||
| E []driver.EnrichmentRecord | ||
| } | ||
|
|
||
| want.V = test.GenUniqueVulnerabilities(10, "test") | ||
| ref, err := a.UpdateVulnerabilities(ctx, "test", "", want.V) | ||
| if err != nil { | ||
| t.Error(err) | ||
| } | ||
| t.Logf("ref: %v", ref) | ||
|
|
||
| want.E = test.GenEnrichments(15) | ||
| ref, err = a.UpdateEnrichments(ctx, "test", "", want.E) | ||
| if err != nil { | ||
| t.Error(err) | ||
| } | ||
| t.Logf("ref: %v", ref) | ||
|
|
||
| var buf bytes.Buffer | ||
| defer func() { | ||
| t.Logf("wrote:\n%s", buf.String()) | ||
| }() | ||
| r, w := io.Pipe() | ||
| eg, ctx := errgroup.WithContext(ctx) | ||
| eg.Go(func() error { defer w.Close(); return a.Store(w) }) | ||
| eg.Go(func() error { | ||
| l, err := NewLoader(io.TeeReader(r, &buf)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for l.Next() { | ||
| e := l.Entry() | ||
| if e.Vuln != nil && e.Enrichment != nil { | ||
| t.Error("expecting entry to have either vulnerability or enrichment, got both") | ||
| } | ||
| if e.Vuln != nil { | ||
| got.V = append(got.V, l.Entry().Vuln...) | ||
| } | ||
| if e.Enrichment != nil { | ||
| got.E = append(got.E, l.Entry().Enrichment...) | ||
| } | ||
| } | ||
| if err := l.Err(); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| }) | ||
| if err := eg.Wait(); err != nil { | ||
| t.Error(err) | ||
| } | ||
| if !cmp.Equal(got, want) { | ||
| t.Error(cmp.Diff(got, want)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of other thoughts here - we could either:
Loaderinstead ofStoreUnmarshal()instead of using aLoaderto load the data.