postgres-nio
postgres-nio copied to clipboard
Refactor PostgresData to Swift type conversion
PostgresData currently offers "fuzzy" conversion to Swift types via its initializer and properties. For example:
let data: PostgresData = ...
print(data.string) // String?
The string property checks to see if the data is any of the Postgres data types that are convertible to a string. This includes most everything like .text, .uuid, .float8, .numeric, etc.
This fuzzy conversion gives a better developer experience but makes building type or performance sensitive code less obvious. For example, if you want to require a BIGINT value, you may use PostgresData.int64. However, BIGINT is only one of several data types that may return successfully for this property. There may be a hidden conversion between types.
To improve this, I suggest separating the PostgresData conversion API into precise and fuzzy counterparts. The precise API would replace the fuzzy API as properties on PostgresData. However, these properties would be renamed to better align with the PostgresDataType they relate to.
let data: PostgresData = ...
print(data.text) // String?
print(data.varchar) // String?
print(data.int8) // Int (or Int64 on 32-bit)
print(data.int4) // Int32 (or Int on 32-bit)
print(data.float4) // Float?
print(data.float8) // Double?
The fuzzy API would be implemented as initializers on the Swift type in question. This is similar to how ByteBuffer's new APIs work.
let data: PostgresData = ...
let string = String(postgresData: data) // String?
let int = Int(postgresData: data) // Int?
let double = Double(postgresData: data) // Double?
@gwynne I think we can close this one, as we have a completely new way for encode and decode. WDYT?
PostgresDecodable solves exactly this problem and PostgresData is on its way out.