github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/proto/v2/proto_test.go (about) 1 package v2 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/bmizerany/assert" 7 "github.com/gogo/protobuf/proto" 8 "github.com/qiuhoude/go-web/proto/v2/models" 9 "reflect" 10 "sort" 11 "strings" 12 "testing" 13 ) 14 15 // 打印pb文件中所有协议 16 func TestBase(t *testing.T) { 17 extMap := proto.RegisteredExtensions((*models.Base)(nil)) 18 19 var extSlice []*proto.ExtensionDesc 20 for _, v := range extMap { 21 //st := reflect.TypeOf(v.ExtensionType).Elem() 22 //if strings.HasSuffix(st.Name(), "Rs") { 23 // continue 24 //} 25 extSlice = append(extSlice, v) 26 } 27 28 sort.Slice(extSlice, func(i, j int) bool { 29 return extSlice[i].Field < extSlice[j].Field 30 }) 31 for _, v := range extSlice { 32 fmt.Println(v.Field, v.Name) 33 printFiled(reflect.TypeOf(v.ExtensionType).Elem()) 34 //printFiledJson(v) 35 } 36 37 } 38 39 func printFiled(t reflect.Type) { 40 for i := 0; i < t.NumField(); i++ { 41 field := t.Field(i) 42 if !strings.HasPrefix(field.Name, "XXX_") { 43 jsonName := strings.Split(field.Tag.Get("json"), ",")[0] 44 fmt.Println("\t name:", jsonName, " type:", field.Type) 45 } 46 } 47 } 48 49 func printFiledJson(pbDesc *proto.ExtensionDesc) { 50 t := reflect.TypeOf(pbDesc.ExtensionType).Elem() 51 var sb strings.Builder 52 sb.WriteString("{") 53 for i := 0; i < t.NumField(); i++ { 54 field := t.Field(i) 55 if !strings.HasPrefix(field.Name, "XXX_") { 56 jsonName := strings.Split(field.Tag.Get("json"), ",")[0] 57 58 _, _ = fmt.Fprintf(&sb, `"%s"`, jsonName) 59 60 } 61 } 62 sb.WriteString("}") 63 } 64 65 func TestJsonToPb(t *testing.T) { 66 twoIntSlice := make([]*models.TwoInt, 0) 67 for i := 0; i < 2; i++ { 68 twoIntSlice = append(twoIntSlice, &models.TwoInt{ 69 V1: proto.Int32(1), 70 V2: proto.Int32(2), 71 }) 72 } 73 74 oldStruct := &models.GetTreasureRs{ 75 IdStatus: twoIntSlice, 76 Status: proto.Int32(1), 77 Red: proto.Bool(false), 78 } 79 80 dataJ, err := json.Marshal(oldStruct) 81 82 if err != nil { 83 t.Fatal(err) 84 } 85 fmt.Println(string(dataJ)) 86 extMap := proto.RegisteredExtensions((*models.Base)(nil)) 87 extensionDescPrt := extMap[1256] 88 89 newStruct := NewStruct(reflect.TypeOf(extensionDescPrt.ExtensionType)) 90 91 //var fields []reflect.StructField 92 //etype := reflect.TypeOf(extensionDescPrt.ExtensionType).Elem() 93 //for i := 0; i < etype.NumField(); i++ { 94 // fields = append(fields, etype.Field(i)) 95 //} 96 // 97 98 fmt.Printf("newStruct %T %v\n", newStruct, newStruct) 99 100 _ = json.Unmarshal(dataJ, &newStruct) 101 102 fmt.Printf("oldStruct %T %v\n", oldStruct, oldStruct) 103 fmt.Printf("newStruct %T %v\n", newStruct, newStruct) 104 105 } 106 107 // 创建结构体 108 func CreateStruct(fields []reflect.StructField) interface{} { 109 var structType reflect.Type 110 structType = reflect.StructOf(fields) 111 so := reflect.New(structType) 112 return so.Interface() 113 } 114 115 func NewStruct(t reflect.Type) proto.Message { 116 // 只能创建一个map 117 return reflect.New(t.Elem()).Interface().(proto.Message) 118 } 119 120 func TestProto(t *testing.T) { 121 122 cmcPb := &models.CrossMoveCityRq{ 123 MapId: proto.Int32(26), 124 Type: proto.Int32(1), 125 Pos: proto.Int32(11), 126 } 127 _ = cmcPb 128 129 data, err := proto.Marshal(cmcPb) 130 if err != nil { 131 t.Fatal("marshaling error: ", err) 132 } 133 newCmcPb := &models.CrossMoveCityRq{} 134 err = proto.Unmarshal(data, newCmcPb) 135 if err != nil { 136 t.Fatal("unmarshaling error: ", err) 137 } 138 assert.Equal(t, cmcPb.MapId, newCmcPb.MapId) 139 140 descs, err := proto.ExtensionDescs(cmcPb) 141 fmt.Println(descs) 142 143 descV1 := models.E_CrossMoveCityRq_Ext 144 fmt.Println(descV1.Field) 145 146 } 147 148 func Test1(t *testing.T) { 149 l1 := numToList(10000000000001) 150 l2 := numToList(564) 151 152 t.Log(listToNum(addTwoNumbers(l1, l2))) 153 } 154 155 type ListNode struct { 156 Val int 157 Next *ListNode 158 } 159 160 func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { 161 s := listToNum(l1) + listToNum(l2) 162 return numToList(s) 163 } 164 165 func numToList(num int) *ListNode { 166 var rtl *ListNode 167 var nl *ListNode 168 h := &ListNode{Val: 0} 169 s := num 170 171 for ; s != 0; s /= 10 { 172 c := s % 10 173 if rtl == nil { 174 h = &ListNode{Val: c} 175 rtl = h 176 nl = rtl 177 } else { 178 nl = &ListNode{Val: c} 179 rtl.Next = nl 180 rtl = nl 181 } 182 } 183 return h 184 } 185 186 func listToNum(l *ListNode) int { 187 var num int 188 i := 1 189 tp := l 190 for ; tp != nil; tp = tp.Next { 191 num += tp.Val * i 192 i *= 10 193 } 194 return num 195 }