コード例 #1
0
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
}
コード例 #2
0
func getFormattedOptionsFromExtensionMap(extensionMap map[int32]proto.Extension, depth int, fieldOption bool, pathIncludingParent string) string {
	var s []string
	counter := 0
	if len(extensionMap) > 0 {
		commentsIndex := 0
		for optInd := range extensionMap {
			// Loop through all imported files
			for _, curFile := range allFiles {
				extensions := curFile.GetExtension()

				// Loop through extensions in the FileDescriptorProto
				for _, ext := range extensions {
					if ext.GetNumber() == optInd {
						bytes, _ := proto.GetRawExtension(extensionMap, optInd)
						key, n := proto.DecodeVarint(bytes)

						wt := key & 0x7

						var val 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()
										}
									}
								}
							}

							s = append(s, LeadingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1))

							if !fieldOption {
								s = append(s, getIndentation(depth+1))
								s = append(s, `option (`)
							} else {
								if counter >= 1 {
									s = append(s, ", ")
								}
								s = append(s, `(`)
							}

							if curFile.GetName() != currentFile.GetName() && len(curFile.GetPackage()) > 0 {
								s = append(s, curFile.GetPackage())
								s = append(s, ".")
							}
							s = append(s, ext.GetName())
							s = append(s, ") = ")
							s = append(s, val)

							if !fieldOption {
								s = append(s, ";\n")
							}
							comm := TrailingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1)
							if len(comm) > 0 {
								s = append(s, comm)
								if counter < len(extensionMap)-1 {
									s = append(s, "\n")
								}
							}

							commentsIndex += 1
							counter += 1

						} else if wt == 2 && ext.GetType() != FieldDescriptorProto_TYPE_STRING { // Messages are special (for method options)
							payload, m := proto.DecodeVarint(bytes[n:])
							n += m
							//fmt.Printf("payload: %v\n", payload)
							for ind := uint64(0); ind < payload-1; ind += 1 {
								firstInt, a := proto.DecodeVarint(bytes[n:])
								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", packVal)

								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
										}
									}
								}

								s = append(s, LeadingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1))

								if !fieldOption {
									s = append(s, getIndentation(depth+1))
									s = append(s, `option (`)
								} else {
									if counter >= 1 {
										s = append(s, ", ")
									}
									s = append(s, `(`)
								}

								if curFile.GetName() != currentFile.GetName() && len(curFile.GetPackage()) > 0 {
									s = append(s, curFile.GetPackage())
									s = append(s, ".")
								}
								s = append(s, ext.GetName())
								s = append(s, ")")
								s = append(s, val)
								if !fieldOption {
									s = append(s, ";\n")
								}
								comm := TrailingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1)
								if len(comm) > 0 {
									s = append(s, comm)
									if counter < len(extensionMap) {
										s = append(s, "\n")
									}
								}
								commentsIndex += 1
								counter += 1

							}

						} else {
							val, _ = byteToValueString(bytes, n, ext.GetType())

							s = append(s, LeadingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1))

							if !fieldOption {
								s = append(s, getIndentation(depth+1))
								s = append(s, `option (`)
							} else {
								if counter >= 1 {
									s = append(s, ", ")
								}
								s = append(s, `(`)
							}

							if curFile.GetName() != currentFile.GetName() && len(curFile.GetPackage()) > 0 {
								s = append(s, curFile.GetPackage())
								s = append(s, ".")
							}
							s = append(s, ext.GetName())
							s = append(s, ") = ")
							s = append(s, val)

							if !fieldOption {
								s = append(s, ";\n")
							}
							comm := TrailingComments(fmt.Sprintf("%s,%d,%d", pathIncludingParent, 999, commentsIndex), depth+1)
							if len(comm) > 0 {
								s = append(s, comm)
								if counter < len(extensionMap) {
									s = append(s, "\n")
								}
							}
							commentsIndex += 1
							counter += 1
						}

					}
				}
			}
		}
	}

	return strings.Join(s, "")
}