Skip to content

review-me - #2

Draft
NickGowdy wants to merge 84 commits into
review-mefrom
main
Draft

NickGowdy wants to merge 84 commits into
review-mefrom
main

Conversation

@NickGowdy

Copy link
Copy Markdown
Owner

No description provided.

Comment thread client/lorawan.go
return nil, err
}

if err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This error handler doesn't seem to be checking anything new.

Comment thread client/lorawan.go
}

// LoraWAN used to communicate to LoRaWAN external system
type LoraWAN struct {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this client package/struct encapsulate more information about the API endpoints and their request/response?

For example, rather than having the LoraWan.DoPost method, what if we had something that looked more like:

func (l *LoraWAN) OnboardSensor(req  OnboardSensorRequest) (*OnboardSensorResponse, error)

This would mean that the specifics of marshalling/unmarshalling the request/response would be contained within this package and wouldn't be leaking out into the processor package. If this endpoint were to change slightly, an engineer would have to make changes in both this package and in the processor package.

Comment thread client/lorawan.go

// DoPost sends data via POST (HTTP) request
func (l *LoraWAN) DoPost(body io.Reader, ctx context.Context) (resp *http.Response, err error) {
fullUrl := l.baseURL + endpoint

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It might be safer to use something like url.JoinPath.

Comment thread processor/code_processor.go Outdated
}
}

func registerDevice(loraWAN client.LoraWAN, ctx context.Context) (*device.Device, error) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's more usual in Go for context.Context to be the first argument - most Go engineers would find this arrangement very unnatural.

Comment thread processor/code_processor.go Outdated
}
}

func registerDevice(loraWAN client.LoraWAN, ctx context.Context) (*device.Device, error) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using an interface here for the client is a really nice choice and enables easier testing, however, it often yields better results to define interfaces where they are used, not where they are implemented.

Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values. The implementing package should return concrete (usually pointer or struct) types: that way, new methods can be added to implementations without requiring extensive refactoring.

https://github.com/golang/go/wiki/CodeReviewComments#interfaces

Comment thread main.go Outdated
if err := godotenv.Load(".env"); err != nil {
panic("error loading.env file")
}
godotenv.Load(".env")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This line seems to do the same as the above line.

Comment thread main.go
godotenv.Load(".env")
baseurl := os.Getenv("BASE_URL")

maxConcurrentJobs, err := strconv.Atoi(os.Getenv("MAX_CONCURRENT_JOBS"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This set of env var loading/parsing gets in the way of reading what main() is doing, it could make sense to extract these out into another function that returns a struct containing all the configuration values.

Comment thread main.go
codeProcessor := &processor.CodeProcessor{
CodeRegistrationLimit: 1,
MaxConcurrentJobs: maxConcurrentJobs,
LoraWAN: *loraWAN,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dependency injection of the client here is nice 👍

Comment thread device/device_test.go
}

hasChar := false
for _, char := range allowedChars {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This set of assertions for each character is really verbose and repetitive, could you loop over each character in the string instead ?

Comment thread main.go Outdated
}(count)

// Spawn workers
for job := 0; job < codeRegistrationLimit; job++ {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It would be neater if CodeProcessor had a .Run() method that span up the worker GoRoutines. This method could accept a context.Context to handle shutdown, or CodeProcessor could have a method called Stop().

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