ParseBool parses and returns the boolean value represented by the provided string, or an error if parsing is unsuccessful. See https://github.com/postgres/postgres/blob/90627cf98a8e7d0531789391fd798c9bfcc3bc1a/src/backend/utils/adt/bool.c#L36
(s string)
| 306 | // string, or an error if parsing is unsuccessful. |
| 307 | // See https://github.com/postgres/postgres/blob/90627cf98a8e7d0531789391fd798c9bfcc3bc1a/src/backend/utils/adt/bool.c#L36 |
| 308 | func ParseBool(s string) (bool, error) { |
| 309 | s = strings.TrimSpace(s) |
| 310 | if len(s) >= 1 { |
| 311 | switch s[0] { |
| 312 | case 't', 'T': |
| 313 | if isCaseInsensitivePrefix(s, "true") { |
| 314 | return true, nil |
| 315 | } |
| 316 | case 'f', 'F': |
| 317 | if isCaseInsensitivePrefix(s, "false") { |
| 318 | return false, nil |
| 319 | } |
| 320 | case 'y', 'Y': |
| 321 | if isCaseInsensitivePrefix(s, "yes") { |
| 322 | return true, nil |
| 323 | } |
| 324 | case 'n', 'N': |
| 325 | if isCaseInsensitivePrefix(s, "no") { |
| 326 | return false, nil |
| 327 | } |
| 328 | case '1': |
| 329 | if s == "1" { |
| 330 | return true, nil |
| 331 | } |
| 332 | case '0': |
| 333 | if s == "0" { |
| 334 | return false, nil |
| 335 | } |
| 336 | case 'o', 'O': |
| 337 | // Just 'o' is ambiguous between 'on' and 'off'. |
| 338 | if len(s) > 1 { |
| 339 | if isCaseInsensitivePrefix(s, "on") { |
| 340 | return true, nil |
| 341 | } |
| 342 | if isCaseInsensitivePrefix(s, "off") { |
| 343 | return false, nil |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | return false, MakeParseError(s, types.Bool, pgerror.New(pgcode.InvalidTextRepresentation, "invalid bool value")) |
| 349 | } |
| 350 | |
| 351 | // ParseDBool parses and returns the *DBool Datum value represented by the provided |
| 352 | // string, or an error if parsing is unsuccessful. |
no test coverage detected
searching dependent graphs…