The optional boolean should support all the types of strings postgres can return
gossmer opened this issue · 2 comments
ie. should be this:
func optionalBool() throws -> Bool? {
guard let rawValue = rawValue?.lowercased() else { return nil }
// https://www.postgresql.org/docs/11/datatype-boolean.html
switch rawValue {
case "t", "true", "yes", "on", "1" : return true
case "f", "false", "no", "off", "0" : return false
default: throw conversionError(Bool.self)
}
}
The use case for the methods that convert PostgresValue instances to Swift types is to convert values returned from the Postgres server.
For boolean values, the Postgres server returns either 't
' for 'f
' (the so-called "output format"). So that is what optionalBool
supports.
Yes, Postgres accepts, as input, a variety of other strings as boolean values (the "input format"), including the ones you mentioned, as well as as whitespace and case variants (e.g. ' F
') and prefixes (e.g. 'fal
'). But, for output, Postgres returns only 't
' and 'f
'.
This is also the case for other data types, notably date/time values. For example, Postgres recognizes, as input, 'January 8, 1999
', but returns as output, '1999-01-08
'. The optionalDate
method of PostgresValue expects the latter, since the underlying use case is processing values returned from Postgres.
Further, the input formats accepted by Postgres are not always well specified. For example, see the first paragraph of Section 8.5.1.
In PostgresClientKit, for supplying input values to Postgres, use the Swift types that conform to the PostgresValueConvertible protocol. For example:
true
false
PostgresDate(year: 1999, month: 1, day: 8)
and allow PostgresClientKit to handle the conversion to an input format allowed by Postgres.
If you would like me to reopen this, just let me know by posting a comment.