func()

in dev/tools/proto-to-mapper/main.go [247:355]


func (v *visitor) writeTypes(out io.Writer, msg protoreflect.MessageDescriptor) {
	goType := protoNameForType(msg)

	{
		fmt.Fprintf(out, "// +kcc:proto=%s\n", msg.FullName())
		fmt.Fprintf(out, "type %s struct {\n", goType)
		for i := 0; i < msg.Fields().Len(); i++ {
			field := msg.Fields().Get(i)
			sourceLocations := msg.ParentFile().SourceLocations().ByDescriptor(field)

			goFieldName := strings.Title(field.JSONName())
			jsonName := field.JSONName()
			goType := ""

			if field.IsMap() {
				entryMsg := field.Message()
				keyKind := entryMsg.Fields().ByName("key").Kind()
				valueKind := entryMsg.Fields().ByName("value").Kind()
				if keyKind == protoreflect.StringKind && valueKind == protoreflect.StringKind {
					goType = "map[string]string"
				} else if keyKind == protoreflect.StringKind && valueKind == protoreflect.Int64Kind {
					goType = "map[string]int64"
				} else {
					fmt.Fprintf(out, "\t// TODO: map type %v %v for field %v\n", keyKind, valueKind, jsonName)
					continue
				}
			} else {
				switch field.Kind() {
				case protoreflect.MessageKind:
					goType = protoNameForType(field.Message())

				case protoreflect.EnumKind:
					goType = "string" //string(field.Enum().Name())

				case protoreflect.StringKind:
					goType = "string"

				case protoreflect.Int32Kind:
					goType = "int32"

				case protoreflect.Int64Kind:
					goType = "int64"

				case protoreflect.Uint32Kind:
					goType = "uint32"

				case protoreflect.Uint64Kind:
					goType = "uint64"

				case protoreflect.Fixed64Kind:
					goType = "uint64"

				case protoreflect.BoolKind:
					goType = "bool"

				case protoreflect.DoubleKind:
					goType = "float64"

				case protoreflect.FloatKind:
					goType = "float32"

				case protoreflect.BytesKind:
					goType = "[]byte"

				default:
					klog.Fatalf("unhandled kind %q for field %v", field.Kind(), field)
				}

				if field.Cardinality() == protoreflect.Repeated {
					goType = "[]" + goType
				} else {
					goType = "*" + goType
				}
			}

			// Blank line between fields for readability
			if i != 0 {
				fmt.Fprintf(out, "\n")
			}

			if sourceLocations.LeadingComments != "" {
				comment := strings.TrimSpace(sourceLocations.LeadingComments)
				for _, line := range strings.Split(comment, "\n") {
					if strings.TrimSpace(line) == "" {
						fmt.Fprintf(out, "    //\n")
					} else {
						fmt.Fprintf(out, "    // %s\n", line)
					}
				}
			}

			fmt.Fprintf(out, "    %s %s `json:\"%s,omitempty\"`\n",
				goFieldName,
				goType,
				jsonName,
			)
		}
		fmt.Fprintf(out, "}\n")
	}

	for i := 0; i < msg.Messages().Len(); i++ {
		m := msg.Messages().Get(i)
		if m.IsMapEntry() {
			continue
		}
		v.writeTypes(out, m)
	}

}