- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 89
Add support for Postgres inet type #413
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
          
     Open
      
      
            rausnitz
  wants to merge
  2
  commits into
  vapor:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
centaurlabs:inet-support
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
  
    
      
          
            66 changes: 66 additions & 0 deletions
          
          66 
        
  Sources/PostgresNIO/New/Data/PostgresINET+PostgresCodable.swift
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| public struct PostgresINET { | ||
| public let ipFamily: UInt8 | ||
| public let netmaskLength: UInt8 | ||
| public let isCIDR: Bool | ||
| public let addressLength: UInt8 | ||
| public let ipAddress: [UInt8] | ||
| } | ||
|  | ||
| extension PostgresINET: PostgresDecodable { | ||
| public init<JSONDecoder: PostgresJSONDecoder>(from byteBuffer: inout ByteBuffer, type: PostgresDataType, format: PostgresFormat, context: PostgresDecodingContext<JSONDecoder>) throws { | ||
| // IP family | ||
| guard let ipFamily: UInt8 = byteBuffer.readInteger(as: UInt8.self) else { | ||
| throw PostgresDecodingError.Code.failure | ||
| } | ||
|  | ||
| // netmask length in bits | ||
| guard let netmaskLength: UInt8 = byteBuffer.readInteger(as: UInt8.self) else { | ||
| throw PostgresDecodingError.Code.failure | ||
| } | ||
|  | ||
| // whether it is a CIDR | ||
| let isCIDR: Bool | ||
| switch byteBuffer.readInteger(as: UInt8.self) { | ||
| case .some(0): | ||
| isCIDR = false | ||
| case .some(1): | ||
| isCIDR = true | ||
| default: | ||
| throw PostgresDecodingError.Code.failure | ||
| } | ||
|  | ||
| // address length in bytes | ||
| guard let addressLength: UInt8 = byteBuffer.readInteger(as: UInt8.self), | ||
| addressLength * 8 == netmaskLength, | ||
| let ipAddress: [UInt8] = byteBuffer.readBytes(length: Int(addressLength)) | ||
| else { | ||
| throw PostgresDecodingError.Code.failure | ||
| } | ||
|  | ||
| self.init( | ||
| ipFamily: ipFamily, | ||
| netmaskLength: netmaskLength, | ||
| isCIDR: isCIDR, | ||
| addressLength: addressLength, | ||
| ipAddress: ipAddress | ||
| ) | ||
| } | ||
| } | ||
|  | ||
| extension PostgresINET: PostgresEncodable & PostgresNonThrowingEncodable { | ||
| public static var psqlType: PostgresDataType { return .inet } | ||
| public static var psqlFormat: PostgresFormat { .binary } | ||
| public func encode<JSONEncoder: PostgresJSONEncoder>(into byteBuffer: inout ByteBuffer, context: PostgresEncodingContext<JSONEncoder>) { | ||
| byteBuffer.writeInteger(self.ipFamily, as: UInt8.self) | ||
| byteBuffer.writeInteger(self.netmaskLength, as: UInt8.self) | ||
| byteBuffer.writeInteger(self.isCIDR ? 1 : 0, as: UInt8.self) | ||
| byteBuffer.writeInteger(self.addressLength, as: UInt8.self) | ||
| byteBuffer.writeBytes(self.ipAddress) | ||
| } | ||
| } | ||
|  | ||
| extension PostgresINET: PostgresArrayDecodable {} | ||
|  | ||
| extension PostgresINET: PostgresArrayEncodable { | ||
| public static var psqlArrayType: PostgresDataType { return .inetArray } | ||
| } | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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 think this is too close to the on the wire representation. In an ideal case this would be closer to the semantical meaning:
Since there are no currency types for this in Swift, we need to have our own minimal type for this.
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.
@fabianfett Without an explicit
isCIDRproperty, would it be safe to infer that an instance uses CIDR notation based on whether the IPv4 network mask is 32 (or IPv6 network mask is 128)?