Example #1
0
func getFieldNumber(descriptorSet *descriptor.FileDescriptorSet, rootPkg string, rootMsg string, msg *descriptor.DescriptorProto, fieldNum int32) *descriptor.FieldDescriptorProto {
	for _, f := range msg.GetField() {
		if f.GetNumber() == fieldNum {
			return f
		}
	}
	if !msg.IsExtendable() {
		return nil
	}
	extendee := "." + rootPkg + "." + rootMsg
	for _, file := range descriptorSet.GetFile() {
		for _, ext := range file.GetExtension() {
			if strings.Map(dotToUnderscore, file.GetPackage()) == strings.Map(dotToUnderscore, rootPkg) {
				if !(ext.GetExtendee() == rootMsg || ext.GetExtendee() == extendee) {
					continue
				}
			} else {
				if ext.GetExtendee() != extendee {
					continue
				}
			}
			if ext.GetNumber() == fieldNum {
				return ext
			}
		}
	}
	return nil
}