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

Add ParseFromIP convenience function #19

Merged
merged 1 commit into from
May 27, 2022
Merged

Add ParseFromIP convenience function #19

merged 1 commit into from
May 27, 2022

Conversation

janik-cloudflare
Copy link
Contributor

This is similar to ParseFromIPAddr, but parses a net.IP instead of
an net.IPNet. The mask size is 32 for IPv4, or 128 for IPv6.


Just a small function that I thought might be worth upstreaming. Thank you all for this module!

@coveralls
Copy link

coveralls commented May 26, 2022

Pull Request Test Coverage Report for Build 2383458277

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 13 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-5.9%) to 75.691%

Changes Missing Coverage Covered Lines Changed/Added Lines %
net.go 0 13 0.0%
Totals Coverage Status
Change from base Build 2064315933: -5.9%
Covered Lines: 137
Relevant Lines: 181

💛 - Coveralls

@aaronfuj aaronfuj self-requested a review May 26, 2022 20:13
@aaronfuj
Copy link

Thanks for the PR, looks good! Can you add in a couple test cases to net_test.go to exercise both the IPv4 and IPv6 paths? They could be as simple as the existing tests, but it would still be good to have the coverage

e.g. something like the following

patricia/net_test.go

Lines 84 to 98 in 1ac5d9e

_, ipr, _ := net.ParseCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/16")
v4IP, v6IP, err = ParseFromIPAddr(ipr)
assert.NoError(t, err)
assert.Nil(t, v4IP)
assert.NotNil(t, v6IP)
assert.Equal(t, uint(16), v6IP.Length)
assert.Equal(t, uint64(0x2001000000000000), v6IP.Left)
assert.Equal(t, uint64(0x0), v6IP.Right)
_, ipr, _ = net.ParseCIDR("127.0.0.1/10")
v4IP, v6IP, err = ParseFromIPAddr(ipr)
assert.NoError(t, err)
assert.NotNil(t, v4IP)
assert.Equal(t, uint(10), v4IP.Length)
assert.Nil(t, v6IP)

@aaronfuj aaronfuj self-assigned this May 26, 2022
This is similar to `ParseFromIPAddr`, but parses a `net.IP` instead of
an `net.IPNet`. The mask size is 32 for IPv4, or 128 for IPv6.
@janik-cloudflare
Copy link
Contributor Author

Thanks for the quick review @aaronfuj, added some simple tests (similar to ParseIPFromAddr)

@aaronfuj
Copy link

Thanks for the quick review @aaronfuj, added some simple tests (similar to ParseIPFromAddr)

No problem, thanks for the PR!

Copy link

@aaronfuj aaronfuj left a comment

Choose a reason for hiding this comment

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

lgtm!

@aaronfuj aaronfuj merged commit dc67e04 into kentik:main May 27, 2022
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.

3 participants