Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc-gen-go:generate the code that the field's descriptor map of all message #1023

Closed
zjj9850 opened this issue Jan 22, 2020 · 1 comment
Closed

Comments

@zjj9850
Copy link

zjj9850 commented Jan 22, 2020

When i use the message's descriptor in golang,but i found the efficiency losing when call descriptor.ForMessage(xx).

I add the code in protoc-gen-go that can generate the descriptor of message when the program init,so the descritpor can be called from memory.the returns is the map structure(the key is the field name and the value is the pointer of the field descriptor)

the code that generated like this:

func GetDbCronJobInfoDescriptorMap() map[string]*protoc_gen_descriptor.FieldDescriptorProto {
 if xxx_descriptor_info_map_DbCronJobInfo == nil {
		initDbCronJobInfoDescriptor()
	}
	return xxx_descriptor_info_map_DbCronJobInfo
}

func initDbCronJobInfoDescriptor() {
	if xxx_descriptor_info_map_DbCronJobInfo == nil {
		xxx_descriptor_info_map_DbCronJobInfo = make(map[string]*protoc_gen_descriptor.FieldDescriptorProto)
		var DbCronJobInfoType *DbCronJobInfo
		_, md := protobuf_descriptor.ForMessage(DbCronJobInfoType)
		for _, fd := range md.Field {
			xxx_descriptor_info_map_DbCronJobInfo[fd.GetName()] = fd
		}
	}
}

var xxx_descriptor_info_map_DbCronJobInfo map[string]*protoc_gen_descriptor.FieldDescriptorProto

func init() {
	proto.RegisterType((*DbCronJobInfo)(nil), "DbCronJobInfo")
	proto.RegisterMapType((map[int32]string)(nil), "DbCronJobInfo.ResultCodeMapEntry")
	initDbCronJobInfoDescriptor()
}
@dsnet
Copy link
Member

dsnet commented Jan 25, 2020

The v1 descriptor package will be deprecated in the next release and there are no plans to optimize it. The replacement for it is native reflection built into messages. See #364

@dsnet dsnet closed this as completed Jan 25, 2020
@golang golang locked and limited conversation to collaborators Jul 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants