mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 15:32:26 +08:00
实现网关服务器不同版本客户端协议代理功能
This commit is contained in:
@@ -11,7 +11,6 @@ func ConvStructToMap(value any) map[string]any {
|
||||
return nil
|
||||
}
|
||||
fieldNum := refType.NumField()
|
||||
result := make(map[string]any)
|
||||
nameList := make([]string, 0)
|
||||
for i := 0; i < fieldNum; i++ {
|
||||
nameList = append(nameList, refType.Field(i).Name)
|
||||
@@ -20,6 +19,7 @@ func ConvStructToMap(value any) map[string]any {
|
||||
if refValue.Kind() == reflect.Ptr {
|
||||
refValue = refValue.Elem()
|
||||
}
|
||||
result := make(map[string]any)
|
||||
for i := 0; i < fieldNum; i++ {
|
||||
result[nameList[i]] = refValue.FieldByName(nameList[i]).Interface()
|
||||
}
|
||||
@@ -77,3 +77,59 @@ func CopyStructField(dst any, src any, fieldName string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func CopyStructSameField(dst any, src any) bool {
|
||||
// dst
|
||||
dstRefType := reflect.TypeOf(dst)
|
||||
if dstRefType.Kind() != reflect.Ptr {
|
||||
return false
|
||||
}
|
||||
dstRefType = dstRefType.Elem()
|
||||
if dstRefType.Kind() != reflect.Struct {
|
||||
return false
|
||||
}
|
||||
dstRefValue := reflect.ValueOf(dst)
|
||||
if dstRefValue.Kind() != reflect.Ptr {
|
||||
return false
|
||||
}
|
||||
dstRefValue = dstRefValue.Elem()
|
||||
// src
|
||||
srcRefType := reflect.TypeOf(src)
|
||||
if srcRefType.Kind() != reflect.Ptr {
|
||||
return false
|
||||
}
|
||||
srcRefType = srcRefType.Elem()
|
||||
if srcRefType.Kind() != reflect.Struct {
|
||||
return false
|
||||
}
|
||||
srcRefValue := reflect.ValueOf(src)
|
||||
if srcRefValue.Kind() != reflect.Ptr {
|
||||
return false
|
||||
}
|
||||
srcRefValue = srcRefValue.Elem()
|
||||
// copy
|
||||
fieldNum := srcRefType.NumField()
|
||||
for i := 0; i < fieldNum; i++ {
|
||||
srcFieldType := srcRefType.Field(i)
|
||||
if !srcFieldType.IsExported() {
|
||||
continue
|
||||
}
|
||||
fieldName := srcFieldType.Name
|
||||
dstFieldType, ok := dstRefType.FieldByName(fieldName)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
srcField := srcRefValue.FieldByName(fieldName)
|
||||
dstField := dstRefValue.FieldByName(fieldName)
|
||||
if srcField.Kind() == reflect.Ptr {
|
||||
dstField.Set(reflect.New(dstFieldType.Type.Elem()))
|
||||
CopyStructSameField(dstField.Interface(), srcField.Interface())
|
||||
continue
|
||||
}
|
||||
if dstField.Type() != reflect.TypeOf(srcField.Interface()) {
|
||||
return false
|
||||
}
|
||||
dstField.Set(reflect.ValueOf(srcField.Interface()))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
49
pkg/reflection/struct_test.go
Normal file
49
pkg/reflection/struct_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type XXX struct {
|
||||
Time int64
|
||||
Date string
|
||||
}
|
||||
|
||||
type YYY struct {
|
||||
Ping uint16
|
||||
}
|
||||
|
||||
type AAA struct {
|
||||
Name string
|
||||
UserId uint32
|
||||
A uint8
|
||||
X *XXX
|
||||
Y YYY
|
||||
}
|
||||
|
||||
type BBB struct {
|
||||
Name string
|
||||
UserId uint32
|
||||
B uint8
|
||||
X *XXX
|
||||
Y YYY
|
||||
}
|
||||
|
||||
func TestCopyStructSameField(t *testing.T) {
|
||||
aaa := &AAA{
|
||||
Name: "flswld",
|
||||
UserId: 100000001,
|
||||
A: 111,
|
||||
X: &XXX{
|
||||
Time: 150405,
|
||||
Date: "2006-01-02",
|
||||
},
|
||||
Y: YYY{
|
||||
Ping: 999,
|
||||
},
|
||||
}
|
||||
bbb := new(BBB)
|
||||
ok := CopyStructSameField(bbb, aaa)
|
||||
fmt.Println(ok)
|
||||
}
|
||||
Reference in New Issue
Block a user