support map types in DBMappable

This commit is contained in:
sawka
2023-03-28 00:23:31 -07:00
parent 1dbdb3d50b
commit e674333621
+6 -2
View File
@@ -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()