This commit is contained in:
Leufolium 2024-07-17 17:12:37 +08:00
parent 8fc078e3c9
commit 7d22149037
2 changed files with 18 additions and 1 deletions

View File

@ -2319,7 +2319,7 @@ func (m *Mongo) CreateStreamer(ctx *gin.Context, streamer *dbstruct.Streamer) er
func (m *Mongo) UpdateStreamer(ctx *gin.Context, streamer *dbstruct.Streamer) error {
col := m.getColStreamer()
set := util.EntityToM(streamer)
set := util.DeepEntityToM(streamer)
set["ut"] = time.Now().Unix()
filter := qmgo.M{
"mid": util.DerefInt64(streamer.Mid),

View File

@ -35,6 +35,23 @@ func StringToMd5(s string) string {
// 将实体类结构体指针转为bson.M采用反射
func EntityToM(p any) qmgo.M {
set := qmgo.M{}
pType := reflect.TypeOf(p).Elem()
pVal := reflect.ValueOf(p).Elem()
for i := 0; i < pType.NumField(); i++ {
name := pType.Field(i).Tag.Get("bson")
if name != "" {
field := pVal.Field(i)
if name != "_id" && !field.IsNil() {
value := field.Elem().Interface()
set[name] = value
}
}
}
return set
}
func DeepEntityToM(p any) qmgo.M {
set := qmgo.M{}
pType := reflect.TypeOf(p).Elem()
pVal := reflect.ValueOf(p).Elem()