mirror of
https://github.com/samuelncui/yatm.git
synced 2026-09-20 06:54:15 +00:00
feat: add more manage features
This commit is contained in:
+1
-29
@@ -3,30 +3,14 @@ package entity
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
|
||||
"github.com/modern-go/reflect2"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
typeMap sync.Map
|
||||
)
|
||||
|
||||
// Scan implement database/sql.Scanner
|
||||
func Scan(dst proto.Message, src interface{}) error {
|
||||
cacheKey := reflect2.RTypeOf(dst)
|
||||
typ, has := loadType(cacheKey)
|
||||
if !has {
|
||||
ptrType := reflect.TypeOf(dst)
|
||||
if ptrType.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("scan dst is not an ptr, has= %T", dst)
|
||||
}
|
||||
|
||||
typ = reflect2.Type2(ptrType.Elem())
|
||||
storeType(cacheKey, typ)
|
||||
}
|
||||
typ := reflect2.TypeOf(dst).(reflect2.PtrType).Elem()
|
||||
typ.Set(dst, typ.New())
|
||||
|
||||
var buf []byte
|
||||
@@ -59,15 +43,3 @@ func Value(src proto.Message) (driver.Value, error) {
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func loadType(key uintptr) (reflect2.Type, bool) {
|
||||
i, has := typeMap.Load(key)
|
||||
if !has {
|
||||
return nil, false
|
||||
}
|
||||
return i.(reflect2.Type), true
|
||||
}
|
||||
|
||||
func storeType(key uintptr, typ reflect2.Type) {
|
||||
typeMap.Store(key, typ)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user