-
-
Notifications
You must be signed in to change notification settings - Fork 315
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
lang: funcs: Validates mac address strings #649
base: master
Are you sure you want to change the base?
Conversation
tests passed |
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.
Hey @S-ign ! Thanks for the patch.
Structurally everything looks good! I left some comments. Make sure to review each one. If there's something you don't understand or agree with, leave a comment instead of updating things in your new patch.
For small patches, I generally recommend fixing things and force-pushing a new version.
For enormous patch sets, it's usually better to add a new commit with the fixes, and then rebase squash right at the end. (We have the privilege of hiding all of our mistakes before we commit them to git master forever!)
As for the commit message, try re-wording it and adding a small sentence or two describing anything unusual or special about this patch. Have a go.
One last tip: Usually the flow most people are used to looks like: 1) hack, hack, hack; 2) commit 3) push
I recommend adding a step 2.5 which involves reading your entire PR as if you were doing a review. This is helpful to both get better at reading code, and to make sure you have everything as you want it.
Thanks again!
output, err := IsMacAddress([]types.Value{ | ||
&types.StrValue{V: test.mac}, | ||
}) | ||
expectedStr := &types.BoolValue{V: test.expected} |
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.
we're expecting a str? be careful of copy+pasta
errors.
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.
ok, so it has to be string, thought I could return other types too.
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.
I didn't understand your reply, can you rephrase?
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.
I see now, I initially changed everything else to return bool but this variable was named expectedStr so it didn't make sense.
I'm going to change my function so that it returns string "true" or "false" instead.
expectedStr := &types.BoolValue{V: test.expected} | ||
|
||
if test.err != nil && err.Error() != test.err.Error() { | ||
t.Errorf("mac %s, expected error: %q, got %q", test.mac, test.err, err) |
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.
why %q ??
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.
not sure actually, I've copied this test.
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.
Never copy code you don't 100% understand ;) Dig into it and make sure it's what you want.
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.
looks like i didn't need a lot of those if statements because I'm not really checking for err since its always nil.
should the function even return err if its always nil?
t.Errorf("mac: %s, expected error: %v, but got nil", test.mac, test.err) | ||
return | ||
} else if test.err == nil && err != nil { | ||
t.Errorf("mac %s, did not expect error but got: %#v", test.mac, err) |
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.
why %#v ?
{"invalid mac invalid delimiter position", "0a--1b2-c3d-4f-56", false, nil}, | ||
} | ||
|
||
for _, test := range macAddressTests { |
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.
add test := test
right here...
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.
I don't understand, I'm replacing
for _, test := range macAddressTests { t.Run(test.name, func(t *testing.T) { output, _ := IsMacAddress([]types.Value{ &types.StrValue{V: test.mac}, })
for:
test := test
wouldn't that get rid of the loop that goes through all the tests? I'm confused about what this does.
I tried it, but it just breaks the test.
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.
Okay. I hope you've been learning from the review process. There's one big change we didn't make yet. I forgot if I delayed telling you on purpose, or if I forgot.
We'd definitely like to re-use existing code from the stdlib wherever possible. First, because that saves time. Second, because that code is probably tested more often. Third, because it is probably more optimized.
Unfortunately for your patch (but not for your learning experience) there's already a mac address parser in the stdlib. It may not be exactly what we want, but we probably want to use it somehow.
So have a look, find it, update this code and anything else, and please push a new version.
Use your newly git skills to combine these commits with the new code you're about to write into a single commit.
Thanks!
|
||
// IsMacAddress takes a single string mac address as input, checks that it | ||
// contains one of the accepted delimiter [":","-","."], checks that all non- | ||
// numarical chars are between a - f, and that the delimiter is separating 2 |
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.
numerical
|
||
// IsMacAddress takes a single string mac address as input, checks that it | ||
// contains one of the accepted delimiter [":","-","."], checks that all non- | ||
// numarical chars are between a - f, and that the delimiter is separating 2 |
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.
nit: 2 -> two.
(numbers less than 10 are usually written as words)
}) | ||
} | ||
|
||
// IsMacAddress takes a single string mac address as input, checks that it |
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.
So this comment is describing the code. And we don't really need to do that for short snippets of code, because well, the code does that. And if we ever changed one of these two, then things would be out of sync.
More importantly, what we want to do, is tell the user and important things. It might be important to tell them which type of macaddresses we validate, and that sort of thing. Talking about the separators is good though.
expectedBool := &types.BoolValue{V: test.expected} | ||
|
||
if err1 := output.Cmp(expectedBool); err1 != nil { | ||
t.Errorf("mac: %s, expected: %s, got: %s", test.mac, expectedBool, output) |
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.
What is %s for? Are we using the right format string everywhere?
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.
%s is for the uninterpreted bytes of the string or slice, maybe i should us %v for the string default value.
for _, test := range macAddressTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test := test | ||
output, _ := IsMacAddress([]types.Value{ |
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.
never ignore errors without a comment saying why or a really good reason.
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.
alright, I'll leave a comment saying that err will always return nil.
2319f4f
to
04f5ba6
Compare
fe2313c
to
271a94e
Compare
b8072b2
to
380004b
Compare
Tips:
please read the style guide before submitting your patch:
docs/style-guide.md
commit message titles must be in the form:
topic: Capitalized message with no trailing period
or:
topic, topic2: Capitalized message with no trailing period
or format a single golang file correctly:
Thanks for contributing to mgmt and welcome to the team!