feat: add more manage features

This commit is contained in:
Samuel N Cui
2023-10-05 02:11:37 +08:00
parent c105763e3d
commit 973cbb91c2
20 changed files with 424 additions and 111 deletions
+1 -29
View File
@@ -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)
}