From 45f51d4fa6311d7621d2fd29424906a92777d544 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Sun, 3 Oct 2021 10:07:12 -0700 Subject: [PATCH] types/opt: implement Bool.Scan Signed-off-by: David Crawshaw --- types/opt/bool.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/types/opt/bool.go b/types/opt/bool.go index 3cdf08d27..3b094a175 100644 --- a/types/opt/bool.go +++ b/types/opt/bool.go @@ -29,6 +29,32 @@ func (b Bool) Get() (v bool, ok bool) { return v, err == nil } +// Scan implements database/sql.Scanner. +func (b *Bool) Scan(src interface{}) error { + if src == nil { + *b = "" + return nil + } + switch src := src.(type) { + case bool: + if src { + *b = "true" + } else { + *b = "false" + } + return nil + case int64: + if src == 0 { + *b = "false" + } else { + *b = "true" + } + return nil + default: + return fmt.Errorf("opt.Bool.Scan: invalid type %T: %v", src, src) + } +} + // EqualBool reports whether b is equal to v. // If b is empty or not a valid bool, it reports false. func (b Bool) EqualBool(v bool) bool {