From e674333621f19d316848a1b7f3572ddc9dacef8c Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 28 Mar 2023 00:23:31 -0700 Subject: [PATCH] support map types in DBMappable --- pkg/dbutil/map.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/dbutil/map.go b/pkg/dbutil/map.go index c4fd61a1..59bde8f6 100644 --- a/pkg/dbutil/map.go +++ b/pkg/dbutil/map.go @@ -104,6 +104,10 @@ func isByteArrayType(t reflect.Type) bool { return t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Uint8 } +func isStringMapType(t reflect.Type) bool { + return t.Kind() == reflect.Map && t.Key().Kind() == reflect.String +} + func ToDBMap(v DBMappable, useBytes bool) map[string]interface{} { if v == nil { return nil @@ -136,7 +140,7 @@ func ToDBMap(v DBMappable, useBytes bool) map[string]interface{} { } else { m[dbName] = QuickJsonArr(fieldVal.Interface()) } - } else if isStructType(field.Type) { + } else if isStructType(field.Type) || isStringMapType(field.Type) { if useBytes { m[dbName] = QuickJsonBytes(fieldVal.Interface()) } else { @@ -177,7 +181,7 @@ func FromDBMap(v DBMappable, m map[string]interface{}) { QuickSetBytes(barrVal.(*[]byte), m, dbName) } else if field.Type.Kind() == reflect.Slice { QuickSetJsonArr(fieldVal.Addr().Interface(), m, dbName) - } else if isStructType(field.Type) { + } else if isStructType(field.Type) || isStringMapType(field.Type) { QuickSetJson(fieldVal.Addr().Interface(), m, dbName) } else if field.Type.Kind() == reflect.String { strVal := fieldVal.Addr().Interface()