add orm function
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
@@ -45,7 +45,6 @@ type Message struct {
|
||||
ServerConn Client
|
||||
TransferMsg
|
||||
Time time.Time
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
type WaitMsg struct {
|
||||
@@ -475,3 +474,29 @@ func MustToMsgVal(val interface{}) MsgVal {
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func (m MsgVal) Orm(stu interface{}) error {
|
||||
inf, err := m.ToInterface()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t := reflect.TypeOf(stu)
|
||||
if t.Kind() != reflect.Ptr {
|
||||
return errors.New("interface not writable(pointer wanted)")
|
||||
}
|
||||
if !reflect.ValueOf(stu).Elem().CanSet() {
|
||||
return errors.New("interface not writable")
|
||||
}
|
||||
it := reflect.TypeOf(inf)
|
||||
if t.Elem().Kind() != it.Kind() {
|
||||
return fmt.Errorf("interface{} kind is %v,not %v", t.Elem().Kind(), it.Kind())
|
||||
}
|
||||
if t.Elem().Name() != it.Name() {
|
||||
return fmt.Errorf("interface{} name is %v,not %v", t.Elem().Name(), it.Name())
|
||||
}
|
||||
if t.Elem().String() != it.String() {
|
||||
return fmt.Errorf("interface{} string is %v,not %v", t.Elem().String(), it.String())
|
||||
}
|
||||
reflect.ValueOf(stu).Elem().Set(reflect.ValueOf(inf))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user