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

Support for inet data type. #7

Merged
merged 1 commit into from Feb 13, 2017

Conversation

rikvdkleij
Copy link
Contributor

Implemented Encoder and Decoder for inet PostgreSQL data type.

Created Inet type in Data.hs which has constructors for IPv4 and IPv6, with or without netmask.

Added roundtrip test for Inet in Main.hs of Tasty test.

@rikvdkleij
Copy link
Contributor Author

@nikita-volkov
When will you review this PR?

@nikita-volkov nikita-volkov merged commit 7693cbb into nikita-volkov:master Feb 13, 2017
@nikita-volkov
Copy link
Owner

It's released. Thanks for the contribution!

@rikvdkleij
Copy link
Contributor Author

It's released. Thanks for the contribution!

My pleasure. So you don't have review comments about my PR code 😄 I'm asking this because I want to learn from reviews.

I will also create PR for hasql to support inet.

return $ inetIPv6FromBytes af netmask isCidr ipSize ip

{-# INLINABLE inetIPv4FromBytes #-}
inetIPv4FromBytes:: Word8 -> Word8 -> Word8 -> Int8 -> Data.IPv4 -> Data.Inet
Copy link
Owner

Choose a reason for hiding this comment

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

Better move this into the where clause of inet for two reasons:

  1. This function only exists for inet
  2. The API is always cleaner, when all the top-level functions operate on the same level of abstraction

Same applies to inetIPv6FromBytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, thanks, I will change that.

I also noticed that I defined in Data module type Netmask = Word8 but I do not use this in encoder or decoder code itself. It's more documentation but I will change that also so Netmask is used.

@nikita-volkov
Copy link
Owner

nikita-volkov commented Mar 11, 2017

I've reviewed your according "hasql" PR. Thanks for your patience.

At first I didn't pay much attention to this PR, because "postgesql-binary" doesn't have a wide audience and the cost of API-breaking changes is negligible. It's kinda like an internal library. However none of that is the case of the "hasql" library, from which in your PR you reexported the datatypes you defined here. That made me pay closer attention to them and I discovered flaws.

First, there was an obvious redundancy in the Inet definition, so I redefined it like this:

data Inet =
  Inet !IP !Word8

data IP =
  IPv4IP !IPv4 |
  IPv6IP !IPv6

I didn't find any point in encoding the netmask as optional, so I made it permanent.

It's idiomatic to give the public types identity, which is why I redefined the IP types like this:

data IPv4 =
  IPv4 !Word8 !Word8 !Word8 !Word8

data IPv6 =
  IPv6 !Word16 !Word16 !Word16 !Word16 !Word16 !Word16 !Word16 !Word16

Then I thought, what is 4x Word8 if not Word32? Why waste space then? So:

newtype IPv4 =
  IPv4 Word32

data IPv6 =
  IPv6 !Word64 !Word64

Finally, I've almost pushed those changes, but then I've discovered the "network-ip" package, which focuses on exactly our use case and does everything exactly the same way, while providing a lot of extras for the application of those types. So I suggest to migrate to that package. Are you willing to do the update?

@rikvdkleij
Copy link
Contributor Author

rikvdkleij commented Mar 11, 2017

Yes, I will do the update. I already considered to use package for these data types, see nikita-volkov/hasql#68 (comment)

I didn't find any point in encoding the netmask as optional, so I made it permanent.

But I would still like to make distinction between subnet and host. I think in most cases you will store host address and do not want be bothered with netmask. It's your library so let me know if you still do not want to mark netmask as optional.

@nikita-volkov
Copy link
Owner

I believe that making the netmask optional is overthinking this. Besides, if we migrate to the "network-ip" package, then we only get the NetAddr IP for the Inet type and there's no way of specifying it without the netmask anyway.

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.

None yet

2 participants