Skip to content
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

Misc. fixes #852

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Misc. fixes #852

wants to merge 9 commits into from

Conversation

bobg
Copy link

@bobg bobg commented Nov 27, 2024

Are contributions like this welcome?

This PR contains numerous fixes to problems identified by staticcheck, plus a few other style, correctness, and best-practice items.

Several unused functions, variables, and struct fields are removed. In some places, imports are rearranged according to goimports with -local github.com/bluesky-social/indigo. Some unnecessary import aliases are removed.

Where it's not self-explanatory, I've annotated the different types of change inline to describe what's going on.

@@ -54,9 +54,7 @@ func (s *PLCServer) GetDocument(ctx context.Context, didstr string) (*did.Docume
return &doc, nil
}

func (s *PLCServer) FlushCacheFor(did string) {
return
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant return.

"github.com/bluesky-social/indigo/atproto/data"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/lexicon"
"github.com/bluesky-social/indigo/atproto/syntax"

"github.com/bobg/errors"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A humble suggestion. Please see https://github.com/bobg/errors#readme

@@ -29,12 +28,12 @@ func runValidateRecord(cctx *cli.Context) error {
cat := lexicon.NewBaseCatalog()
err := cat.LoadDirectory(p)
if err != nil {
return err
return errors.Wrap(err, "in LoadDirectory")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially when a function has multiple error return sites, it's useful to wrap them to know which site did the returning.

}

body, err := data.UnmarshalJSON(respBytes)
if err != nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error was not being checked.

for _, f := range flags {
v = append(v, f)
}
v, _ := s.Data[key]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If key is not in s.Data, then v will be []string(nil), which is suitable for using directly in the append that follows. All the other stuff is extraneous.

// TODO: do we still just do this if it errors?
c.complete <- job.act.Uid
// TODO: plumb a context object into this function and use select{} to multiplex its Done channel with c.repoSync
for job := range c.repoSync {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More straightforward than a for { select { ... } } with a single case.

"github.com/multiformats/go-multihash"
mh "github.com/multiformats/go-multihash"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate import.

Comment on lines +10 to +14
ctxAuth struct{}
ctxAuthScope struct{}
ctxDID struct{}
ctxToken struct{}
ctxUser struct{}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use distinct types as keys for context.WithValue, not strings, to avoid collisions with other packages doing the same thing.

This file encapsulates setting and getting of the particular context values needed by this package.


user, ok := c.Get("user").(*gojwt.Token)
if !ok {
token := getToken(ctx)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs double-checking. The original code was confusing the Token types from two different jwt modules, and possibly confusing users with tokens.

}
defer resp.Body.Close()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required after any http.Get (and a few other operations).

@bobg bobg marked this pull request as ready for review November 27, 2024 23:35
}
defer resp.Body.Close()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mustn't forget this.

return err
var (
oplog []map[string]any
dec = json.NewDecoder(resp.Body)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to read the whole response body into memory first - stream it straight to the JSON decoder.

@bobg bobg mentioned this pull request Dec 1, 2024
@@ -347,7 +342,7 @@ func (bgs *BGS) StartWithListener(listen net.Listener) error {
sendHeader = false
}

bgs.log.Warn("HANDLER ERROR: (%s) %s", ctx.Path(), err)
bgs.log.Warn("HANDLER ERROR", "path", ctx.Path(), "err", err)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Structured (not formatted-string) logging, here and in a couple of other spots below too.

@bnewbold bnewbold self-requested a review December 5, 2024 18:47
@bnewbold
Copy link
Collaborator

bnewbold commented Dec 5, 2024

Thanks! I think what will probably happen here is that i'll cherry-pick a subset of these when we get a chance. Keeping small/discrete commits is helpful. We probably won't pull in new dependencies (eg, your errors package). I should really get staticcheck wired up to our regular linting.

@bobg
Copy link
Author

bobg commented Dec 5, 2024

Makes sense, thanks @bnewbold. I'm interested and able to help - please let me know whether and how you'd like me to. (E.g., I could split this PR into multiple smaller ones - by directory, say, or by type-of-change.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants