From 9f9022b8e14dd2f65ca0f08536cb1d18cd715907 Mon Sep 17 00:00:00 2001 From: xgfone Date: Thu, 18 Jul 2024 18:26:30 +0800 Subject: [PATCH] add the try functions to bind rows to map --- dml_select_bind_map.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/dml_select_bind_map.go b/dml_select_bind_map.go index a6f1adb..1f13f5b 100644 --- a/dml_select_bind_map.go +++ b/dml_select_bind_map.go @@ -14,6 +14,36 @@ package sqlx +// TryBindToMapKV is the same as BindToMapKV, but calls it only if err==nil. +func TryBindToMapKV[M ~map[K]V, K comparable, V any](rows Rows, err error, initcap int) (m M, e error) { + if err == nil { + m, err = BindToMapKV[M](rows, initcap) + } + + e = err + return +} + +// TryBindToMapBool is the same as BindToMapBool, but calls it only if err==nil. +func TryBindToMapBool[M ~map[K]bool, K comparable](rows Rows, err error, initcap int) (m M, e error) { + if err == nil { + m, err = BindToMapBool[M](rows, initcap) + } + + e = err + return +} + +// TryBindToMapEmptyStruct is the same as BindToMapEmptyStruct, but calls it only if err==nil. +func TryBindToMapEmptyStruct[M ~map[K]struct{}, K comparable](rows Rows, err error, initcap int) (m M, e error) { + if err == nil { + m, err = BindToMapEmptyStruct[M](rows, initcap) + } + + e = err + return +} + // BindToMapKV scans two columns as key and value, and inserts them into m. // // NOTICE: If rows.Rows is nil, do nothing. Or, it will close the rows.