From 01f8c17b38050604112f688006b259a60df6a58a Mon Sep 17 00:00:00 2001 From: "Tristan M." <54208010+Oudwins@users.noreply.github.com> Date: Wed, 21 Aug 2024 20:22:45 +0200 Subject: [PATCH] fix: bool coercer (#14) --- conf/Coercers.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/conf/Coercers.go b/conf/Coercers.go index 3c27464..b450b9c 100644 --- a/conf/Coercers.go +++ b/conf/Coercers.go @@ -20,11 +20,9 @@ var Coercers = struct { Slice CoercerFunc }{ Bool: func(data any) (any, error) { - if b, ok := data.(bool); ok { - return b, nil - } - switch v := data.(type) { + case bool: + return v, nil case string: // There are cases where frontend libraries use "on" as the bool data // think about toggles. Hence, let's try this first. @@ -39,9 +37,17 @@ var Coercers = struct { } return boolVal, nil } + case int: + if v == 0 { + return false, nil + } else if v == 1 { + return true, nil + } default: return nil, fmt.Errorf("input data is an unsupported type to coerce to bool: %v", data) } + + return nil, fmt.Errorf("input data is an unsupported type to coerce to bool: %v", data) }, String: func(data any) (any, error) { switch v := data.(type) {