func byteToValueString(b []byte, lastReadIndex int, t FieldDescriptorProto_Type) (string, int) { var val string // All the types of options switch t { case FieldDescriptorProto_TYPE_BOOL: d, _ := proto.DecodeVarint(b[lastReadIndex:]) if int(d) == 1 { val = "true" } else { val = "false" } lastReadIndex += 1 case FieldDescriptorProto_TYPE_UINT32, FieldDescriptorProto_TYPE_INT32, FieldDescriptorProto_TYPE_UINT64, FieldDescriptorProto_TYPE_INT64: d, m := proto.DecodeVarint(b[lastReadIndex:]) lastReadIndex += m //fmt.Printf("m: %v\n", m) val = fmt.Sprintf("%v", d) case FieldDescriptorProto_TYPE_FLOAT, FieldDescriptorProto_TYPE_SFIXED32, FieldDescriptorProto_TYPE_FIXED32: var f float32 binary.Read(bytes.NewBuffer(b[lastReadIndex:]), binary.LittleEndian, &f) lastReadIndex += 1 val = fmt.Sprintf("%v", f) case FieldDescriptorProto_TYPE_DOUBLE, FieldDescriptorProto_TYPE_SFIXED64, FieldDescriptorProto_TYPE_FIXED64: var f float64 binary.Read(bytes.NewBuffer(b[lastReadIndex:]), binary.LittleEndian, &f) lastReadIndex += 1 val = fmt.Sprintf("%v", f) case FieldDescriptorProto_TYPE_STRING: l, n := proto.DecodeVarint(b[lastReadIndex:]) lastReadIndex += n val = `"` + string(b[lastReadIndex:lastReadIndex+int(l)]) + `"` lastReadIndex += int(l) } return val, lastReadIndex }
func getFormattedOptionsFromExtensionMap(extensionMap map[int32]proto.Extension, depth int, fieldOption bool, pathIncludingParent string, startIndex int) []string { var s []string counter := 0 if len(extensionMap) > 0 { commentsIndex := startIndex // Sort extension map for optInd := range extensionMap { // Loop through all imported files for _, curFile := range allFiles { extensions := curFile.GetExtension() // Loop through extensions in the FileDescriptorProto for ext_i, ext := range extensions { if ext.GetNumber() == optInd { bytes, _ := proto.GetRawExtension(extensionMap, optInd) key, n := proto.DecodeVarint(bytes) headerlength := n wt := key & 0x7 var val string var singleOption []string // Enums are special if ext.GetType() == FieldDescriptorProto_TYPE_ENUM { // Loop through enums to find right one for _, myEnum := range curFile.GetEnumType() { if myEnum.GetName() == getLastWordFromPath(ext.GetTypeName(), ".") { for _, enumVal := range myEnum.GetValue() { d, _ := proto.DecodeVarint(bytes[n:]) if uint64(enumVal.GetNumber()) == d { val = enumVal.GetName() } } } } lc := LeadingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1) if len(lc) > 0 { if ext_i == 0 { singleOption = append(singleOption, strings.TrimPrefix(lc, "\n")) } else { singleOption = append(singleOption, lc) } } if !fieldOption { singleOption = append(singleOption, getIndentation(depth+1)) singleOption = append(singleOption, `option (`) } else { if counter >= 1 { singleOption = append(singleOption, ", ") } singleOption = append(singleOption, `(`) } if curFile.GetName() != currentFile.GetName() && len(curFile.GetPackage()) > 0 { singleOption = append(singleOption, curFile.GetPackage()) singleOption = append(singleOption, ".") } singleOption = append(singleOption, ext.GetName()) singleOption = append(singleOption, ")=") singleOption = append(singleOption, val) if !fieldOption { singleOption = append(singleOption, ";\n") } comm := TrailingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1) if len(comm) > 0 { singleOption = append(singleOption, comm) if counter < len(extensionMap)-1 { singleOption = append(singleOption, "\n") } } s = append(s, strings.Join(singleOption, "")) commentsIndex += 1 counter += 1 } else if wt == 2 && ext.GetType() != FieldDescriptorProto_TYPE_STRING { // Messages are special (for method options) for n < len(bytes) { // Grab the payload _, m := proto.DecodeVarint(bytes[n:]) n += m // Grab tag/wiretype //fmt.Printf("%v, %v\n", n, len(bytes)) firstInt, a := proto.DecodeVarint(bytes[n:]) //fmt.Printf("firstint: %v\n", a) n += a wiretype := firstInt & 0x7 var packType FieldDescriptorProto_Type switch wiretype { case 0: packType = FieldDescriptorProto_TYPE_INT32 case 1: packType = FieldDescriptorProto_TYPE_FIXED64 case 2: packType = FieldDescriptorProto_TYPE_STRING case 5: packType = FieldDescriptorProto_TYPE_FIXED32 } packVal, b := byteToValueString(bytes, n, packType) n = b //fmt.Printf("%v\n", packType) //fmt.Printf("%v\n", packVal) n += headerlength if ext.GetType() == FieldDescriptorProto_TYPE_MESSAGE { tagNum := firstInt >> 3 //fmt.Printf("tagnum: %v\n", tagNum) // Loop through messages to find right one myMessage := curFile.GetMessage(getLastWordFromPath(ext.GetTypeName(), ".")) //fmt.Println(myMessage.GetName()) for _, field := range myMessage.GetField() { if uint64(field.GetNumber()) == tagNum { val = `.` + field.GetName() + " = " + packVal break } } } singleOption = append(singleOption, LeadingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1)) if !fieldOption { singleOption = append(singleOption, getIndentation(depth+1)) singleOption = append(singleOption, `option (`) } else { if counter >= 1 { singleOption = append(singleOption, ", ") } singleOption = append(singleOption, `(`) } if curFile.GetName() != currentFile.GetName() && len(curFile.GetPackage()) > 0 { singleOption = append(singleOption, curFile.GetPackage()) singleOption = append(singleOption, ".") } singleOption = append(singleOption, ext.GetName()) singleOption = append(singleOption, ")") singleOption = append(singleOption, val) if !fieldOption { singleOption = append(singleOption, ";\n") } comm := TrailingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1) if len(comm) > 0 { singleOption = append(singleOption, comm) if counter < len(extensionMap) { singleOption = append(singleOption, "\n") } } commentsIndex += 1 counter += 1 s = append(s, strings.Join(singleOption, "")) } } else { val, b := byteToValueString(bytes, n, ext.GetType()) n = b singleOption = append(singleOption, LeadingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1)) if !fieldOption { singleOption = append(singleOption, getIndentation(depth+1)) singleOption = append(singleOption, `option (`) } else { if counter >= 1 { singleOption = append(singleOption, ", ") } singleOption = append(singleOption, `(`) } if curFile.GetName() != currentFile.GetName() && len(curFile.GetPackage()) > 0 { singleOption = append(singleOption, curFile.GetPackage()) singleOption = append(singleOption, ".") } singleOption = append(singleOption, ext.GetName()) singleOption = append(singleOption, ")=") singleOption = append(singleOption, val) if !fieldOption { singleOption = append(singleOption, ";\n") } comm := TrailingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1) if len(comm) > 0 { singleOption = append(singleOption, comm) if counter < len(extensionMap) { singleOption = append(singleOption, "\n") } } commentsIndex += 1 counter += 1 s = append(s, strings.Join(singleOption, "")) } } } } } } return s }