-
Notifications
You must be signed in to change notification settings - Fork 24
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
fixes for cidr equality, validity and consistency #535
Conversation
see also: #534 |
What if we left equality as it was but then added a method |
One of the concrete problems I'm running in to, is that a problem with cidr#normalize: Cidr[A] is that you'd never know before you if the cidr you have is already normalized or still needs to be, leading to potentially calling .normalize over and over. In that sense, I like the idea of having would you like some drafts of various options? |
15a1214
to
7cf0c6b
Compare
Okay, why don't we pursue the I'm concerned about overriding |
main...martijnhoekstra:strict-cidr is a potential minimal Strict version. It could have its own fromString too. I'd need to kick the tires on whether this would work for me in practice. |
98d87fa
to
9c1f114
Compare
9c1f114
to
c0dd8ed
Compare
Looks good to me! |
* This means the address will never have any bits set outside the prefix. For example, a range | ||
* such as 192.168.0.1/31 is not allowed. | ||
*/ | ||
final class Strict[+A <: IpAddress] private (override val prefix: A, override val prefixBits: Int) |
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: can't these be passed along as ctor args?
final class Strict[+A <: IpAddress] private (override val prefix: A, override val prefixBits: Int) | |
final class Strict[+A <: IpAddress] private (prefix: A, prefixBits: Int) |
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.
for the prefix bits, yes, that's just plain better.
For prefix, if it's an override val, it overrides def prefix: A = address.transform(_.masked(Ipv4Address.mask(prefixBits)), _.masked(Ipv6Address.mask(prefixBits)))
with the val, which adds a field but potentially saves a bit of useless work. I guess I'd rather take the extra field? What do you think?
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.
@armanbilge what do you think?
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.
@martijnhoekstra sorry, I missed this.
Instead of adding an extra redundant field, can't we just do override def prefix = address
?
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.
caeb0ab
to
83000eb
Compare
83000eb
to
24b1fb8
Compare
the root of the problem: 172.168.0.1/30 isn't a valid cidr range, because 172.168.0.1 isn't a valid prefix for a /30, but is accepted by both Cidr.fromString and Cidr.apply, which now cause cidr#toString to output invalid cidr ranges.
This MR changes CIDR equality and printing:
equality:
ip"172.168.0.1" / 30 != ip"172.168.0.0" / 30
, even though it matches the same addressessip"172.168.0.1" / 30 == ip"172.168.0.0" / 30
, even though the base address is differentprinting:
(ip"172.168.0.1" / 30).toString == "172.168.0.1/30"
even though that isn't a valid CIDR range(ip"172.168.0.1" / 30).toString == "172.168.0.0/30"
even though that doesn't contain the base addressmisc:
(adress0 / 0).address == adress0
still holdscidr1 == cidr2
no longer impliescidr1.address == cidr2.address
cidr1 == cidr2
no longer impliescidr1.productIterator.toList == cidr2.productIterator.toList
This is not the only way to fix the given problems. The other obvious option is at construction (
Cidr.apply
) time, set address to prefix, and deprecate address for prefix. This is a bigger breaking change: it will break users that assume(addr / 0).address == addr
. It also kills the (somewhat?) useful ability to have this datatype carry at the same time an arbitrary IP address and some cidr range the address is part of. passing around some machines ip / 24 (or something) does come up a fair bit in practice, and comes "for free" in the current implementation.A third option is to provide separate between strict and lenient constructors