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

Complete lab9 #489

Merged
merged 2 commits into from
Aug 10, 2019
Merged

Complete lab9 #489

merged 2 commits into from
Aug 10, 2019

Conversation

n0npax
Copy link
Collaborator

@n0npax n0npax commented Jun 24, 2019

Lab 9 - JSON input

Fixes #495

Review of colleague's PR #474

Changes proposed in this PR:

Implement features requested in spec.

@n0npax n0npax force-pushed the lab09 branch 3 times, most recently from 177be32 to ad22907 Compare June 25, 2019 11:16
@n0npax n0npax changed the title [WIP] Complete lab Complete lab9 Jun 25, 2019
@n0npax n0npax force-pushed the lab09 branch 2 times, most recently from 6a5fe7c to 672b90e Compare June 25, 2019 11:22
@anz-bank anz-bank deleted a comment from golangcibot Jun 25, 2019
@anz-bank anz-bank deleted a comment from golangcibot Jun 25, 2019
@n0npax n0npax force-pushed the lab09 branch 3 times, most recently from d8fd441 to f074643 Compare June 25, 2019 12:32
@n0npax n0npax added the lab9 label Jun 25, 2019
@codecov
Copy link

codecov bot commented Jun 25, 2019

Codecov Report

Merging #489 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #489    +/-   ##
======================================
  Coverage     100%   100%            
======================================
  Files         157    161     +4     
  Lines        2806   2926   +120     
======================================
+ Hits         2806   2926   +120
Impacted Files Coverage Δ
09_json/n0npax/pkg/puppy/errors.go 100% <100%> (ø)
09_json/n0npax/pkg/puppy/store/memStore.go 100% <100%> (ø)
09_json/n0npax/pkg/puppy/store/syncStore.go 100% <100%> (ø)
09_json/n0npax/cmd/puppy-server/main.go 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 216ef6f...9e0ce5b. Read the comment docs.

.golangci.yml Outdated Show resolved Hide resolved
@n0npax n0npax force-pushed the lab09 branch 2 times, most recently from 8a8e963 to b1fda19 Compare June 30, 2019 02:58
@willshen8
Copy link
Collaborator

willshen8 commented Jul 27, 2019

One more thing. Marcin. Please fix up your commit message. It should be describing what you are committing. I got caught doing the same thing once like you :) Cheers!

willshen8
willshen8 previously approved these changes Jul 27, 2019
Copy link
Contributor

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

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

Let's resolve discussions around monkey patching and error passing in lab10 PR first please.
I've also updated this PR description.

09_json/n0npax/cmd/puppy-server/main.go Outdated Show resolved Hide resolved
09_json/n0npax/cmd/puppy-server/main.go Outdated Show resolved Hide resolved
Copy lab08
Apply some new patches from lab07
Add command line support
Add example data in json file
Copy link
Contributor

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

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

I couple more comments on variable naming / function signatures / structure, but nothing too big.
I'm happy for you to merge as is or fix and ping me personally and I'll approve right away. Up to you :)


var (
logFatalf = log.Fatal
parser = parseArgs
Copy link
Contributor

Choose a reason for hiding this comment

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

i like this!

Copy link
Collaborator

Choose a reason for hiding this comment

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

TIL

return nil
}

func parseArgs(args []string) (config, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

func createStorer(c *config) error {
switch c.sType {
case "sync":
c.storer = store.NewSyncStore()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be nicer if this function didn't modify it's input but return the Storer:

func createStorer(storeStr string) (puppy.Storer, error) { //...

To me config should as much as possible only hold read-only/immutable config info (which storer is not).
Also, where all relevant information is just one (or two) fields of the config I find it nicer to pass those few fields (keep the interface smaller).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed in lab10

09_json/n0npax/cmd/puppy-server/main.go Show resolved Hide resolved
var storeType string
var puppyFile *os.File
kingpin.Flag("data", "path to file with puppies data").Short('d').FileVar(&puppyFile)
kingpin.Flag("store", "Store type").Short('s').Default("map").EnumVar(&storeType, "map", "sync")
Copy link
Contributor

Choose a reason for hiding this comment

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

cool i didn't know about this... will remember.

@n0npax n0npax merged commit 058675e into anz-bank:master Aug 10, 2019
@n0npax
Copy link
Collaborator Author

n0npax commented Aug 10, 2019

Thanks for the review. I'll merge this as is and implement changes in lab10

@n0npax n0npax deleted the lab09 branch August 10, 2019 08:50
Copy link
Collaborator

@alextmz alextmz left a comment

Choose a reason for hiding this comment

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

Nice code and some good ideas here. Had to really scratch the bottom of the nit-bag to find stuff to write about.


var (
logFatalf = log.Fatal
parser = parseArgs
Copy link
Collaborator

Choose a reason for hiding this comment

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

TIL


Review coverage with

go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: should had specified usage and parameters :-)

Copy link
Collaborator Author

@n0npax n0npax Nov 7, 2019

Choose a reason for hiding this comment

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

not sure if I get it. for sure it's refactored in lab11

logFatalf(err)
}
logFatalf(runPuppyServer(&config))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

That's clean!

kingpin.Flag("data", "path to file with puppies data").Short('d').FileVar(&puppyFile)
kingpin.Flag("store", "Store type").Short('s').Default("map").EnumVar(&storeType, "map", "sync")
_, err := kingpin.CommandLine.Parse(args)
return config{puppyFile, storeType, nil}, err
Copy link
Collaborator

Choose a reason for hiding this comment

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

That's a much better way than I did

}
var puppies []puppy.Puppy
if err = json.Unmarshal(b, &puppies); err != nil {
return nil, errors.New(string(b))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, here you return the read file contents as an error, but then you lose the context of whatever is returned by the Unmarshal error - might make whoever reads the error message confused, getting just a dump without saying what is wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch. I have no idea why I didn't just return nil, err

// CreatePuppy creates puppy
func (m *MemStore) CreatePuppy(p *puppy.Puppy) (int, error) {
if p.Value < 0 {
return -1, puppy.Errorf(puppy.ErrCodeInvalidInput, "Puppy value have to be positive number")
Copy link
Collaborator

Choose a reason for hiding this comment

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

"Puppy value has to be equal or bigger than zero", as zero itself is nor positive nor negative (and I'm really running out of nits here)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

?? This is returning error if p.Value is lower than 0. 0 < 0 -> False so we're jumping to line 26. If p.Value is equal or larger than 0 I'm not throwing error? Am I missing sth?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

0 is positive as far as i remember.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a nit about the message itself - zero is neither negative nor positive, it's zero. So saying that value has to be positive is not strictly true, because the smallest possible value needs to be zero. So, the message must say value has to be bigger or equal than zero or sth else that includes the possibility of a zero value. Nit, really... :)

}

// UpdatePuppy updates puppy
func (m *MemStore) UpdatePuppy(id int, p *puppy.Puppy) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd skip passing the ID separately, as Puppy already has one. If you're updating, the ID shouldn't change, so the ID in Puppy should be enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is holly war :)

Example PUT in K8S (https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.14/#volumemount-v1-core)
PUT /apis/batch/v1beta1/namespaces/{namespace}/cronjobs/{name}

We could extract {name} from payload, but it's provided explicitly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

:) Indeed can be a matter of taste. I agree with using it explicitly in case of a PUT, but just doesn't feel right in the case of internal language functions. But it's taste anyway :)

}

// DeletePuppy deletes puppy
func (m *MemStore) DeletePuppy(id int) (bool, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Feels like that both bool and error returning a bit too much, isn't just error enough?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes it is. It's fixed in lab10 or lab11

return puppy.Puppy{
Breed: "Type A",
Colour: "Grey",
Value: rand.Intn(100) + 100,
Copy link
Collaborator

Choose a reason for hiding this comment

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

good one randomizing the value

// Puppy contains information about single puppy
type Puppy struct {
ID int `json:"id"`
Value int `json:"value"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Value could be 1,99... (float) 😆

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

design assumption :)

@alextmz alextmz mentioned this pull request Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lab09
5 participants