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

lang: funcs: Validates mac address strings #649

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

S-ign
Copy link

@S-ign S-ign commented Mar 9, 2021

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

  • golang code must be formatted according to the standard, please run:
make gofmt		# formats the entire project correctly

or format a single golang file correctly:

gofmt -w yourcode.go
  • please rebase your patch against current git master:
git checkout master
git pull origin master
git checkout your-feature
git rebase master
git push your-remote your-feature
hub pull-request	# or submit with the github web ui
  • after a patch review, please ping @purpleidea so we know to re-review:
# make changes based on reviews...
git add -p		# add new changes
git commit --amend	# combine with existing commit
git push your-remote your-feature -f
# now ping @purpleidea in the github PR since it doesn't notify us automatically

Thanks for contributing to mgmt and welcome to the team!

@S-ign
Copy link
Author

S-ign commented Mar 9, 2021

tests passed

Copy link
Owner

@purpleidea purpleidea left a 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!

lang/funcs/core/net/is_macaddress_func.go Outdated Show resolved Hide resolved
lang/funcs/core/net/is_macaddress_func.go Outdated Show resolved Hide resolved
lang/funcs/core/net/is_macaddress_func.go Outdated Show resolved Hide resolved
lang/funcs/core/net/is_macaddress_func.go Outdated Show resolved Hide resolved
lang/funcs/core/net/is_macaddress_func.go Outdated Show resolved Hide resolved
lang/funcs/core/net/is_macaddress_func.go Outdated Show resolved Hide resolved
output, err := IsMacAddress([]types.Value{
&types.StrValue{V: test.mac},
})
expectedStr := &types.BoolValue{V: test.expected}
Copy link
Owner

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.

Copy link
Author

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.

Copy link
Owner

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?

Copy link
Author

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)
Copy link
Owner

Choose a reason for hiding this comment

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

why %q ??

Copy link
Author

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.

Copy link
Owner

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.

Copy link
Author

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)
Copy link
Owner

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 {
Copy link
Owner

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...

Copy link
Author

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.

Copy link
Owner

@purpleidea purpleidea left a 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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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)
Copy link
Owner

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?

Copy link
Author

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{
Copy link
Owner

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.

Copy link
Author

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.

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