MCPcopy
hub / github.com/wavetermdev/waveterm / ToDBMap

Function ToDBMap

pkg/util/dbutil/dbmappable.go:146–189  ·  view source on GitHub ↗
(v DBMappable, useBytes bool)

Source from the content-addressed store, hash-verified

144}
145
146func ToDBMap(v DBMappable, useBytes bool) map[string]interface{} {
147 if CheckNil(v) {
148 return nil
149 }
150 rv := reflect.ValueOf(v)
151 if rv.Kind() == reflect.Pointer {
152 rv = rv.Elem()
153 }
154 if rv.Kind() != reflect.Struct {
155 panic(fmt.Sprintf("invalid type %T (non-struct) passed to StructToDBMap", v))
156 }
157 rt := rv.Type()
158 m := make(map[string]interface{})
159 numFields := rt.NumField()
160 for i := 0; i < numFields; i++ {
161 field := rt.Field(i)
162 fieldVal := rv.FieldByIndex(field.Index)
163 dbName := field.Tag.Get("dbmap")
164 if dbName == "" {
165 dbName = strings.ToLower(field.Name)
166 }
167 if dbName == "-" {
168 continue
169 }
170 if isByteArrayType(field.Type) {
171 m[dbName] = fieldVal.Interface()
172 } else if field.Type.Kind() == reflect.Slice {
173 if useBytes {
174 m[dbName] = QuickJsonArrBytes(fieldVal.Interface())
175 } else {
176 m[dbName] = QuickJsonArr(fieldVal.Interface())
177 }
178 } else if isStructType(field.Type) || isStringMapType(field.Type) {
179 if useBytes {
180 m[dbName] = QuickJsonBytes(fieldVal.Interface())
181 } else {
182 m[dbName] = QuickJson(fieldVal.Interface())
183 }
184 } else {
185 m[dbName] = fieldVal.Interface()
186 }
187 }
188 return m
189}
190
191func FromDBMap(v DBMappable, m map[string]interface{}) {
192 if CheckNil(v) {

Callers

nothing calls this directly

Calls 10

CheckNilFunction · 0.85
isByteArrayTypeFunction · 0.85
QuickJsonArrBytesFunction · 0.85
QuickJsonArrFunction · 0.85
isStructTypeFunction · 0.85
isStringMapTypeFunction · 0.85
QuickJsonBytesFunction · 0.85
TypeMethod · 0.80
QuickJsonFunction · 0.70
GetMethod · 0.45

Tested by

no test coverage detected