func UpdateProtoDescriptorFromOPConfig()

in src/go/configgenerator/filtergen/grpc_transcoder.go [213:266]


func UpdateProtoDescriptorFromOPConfig(serviceConfig *confpb.Service, opts options.ConfigGeneratorOptions, descriptorBytes []byte) ([]byte, error) {
	ruleMap := make(map[string]*ahpb.HttpRule)
	for _, rule := range serviceConfig.GetHttp().GetRules() {
		ruleMap[rule.GetSelector()] = rule
	}
	apiMap := GetAPINamesSetFromOPConfig(serviceConfig, opts)

	fds := &descpb.FileDescriptorSet{}
	if err := protov2.Unmarshal(descriptorBytes, fds); err != nil {
		glog.Error("failed to unmarshal protodescriptor, error: ", err)
		return nil, fmt.Errorf("failed to unmarshal proto descriptor, error: %v", err)
	}

	for _, file := range fds.GetFile() {
		for _, service := range file.GetService() {
			apiName := fmt.Sprintf("%s.%s", file.GetPackage(), service.GetName())

			// Only modify the API in the serviceInfo.ApiNames.
			// These are the ones to enable grpc transcoding.
			if _, ok := apiMap[apiName]; !ok {
				continue
			}

			for _, method := range service.GetMethod() {
				sel := fmt.Sprintf("%s.%s", apiName, method.GetName())
				if constRule, ok := ruleMap[sel]; ok {
					// Clone rule so modifications do not affect input service config.
					rule := proto.Clone(constRule).(*ahpb.HttpRule)
					glog.Info("Set http.rule: #+v", rule)
					if method.GetOptions() == nil {
						method.Options = &descpb.MethodOptions{}
					}
					protov2.SetExtension(method.GetOptions(), ahpb.E_Http, rule)
				}

				// If an http rule is specified for a rpc endpoint then the rpc's default http binding will be
				// disabled according to the logic in the envoy's json transcoder filter. To still enable
				// the default http binding, which is the designed behavior, the default http binding needs to be
				// added to the http rule's additional bindings.
				if httpRule := protov2.GetExtension(method.GetOptions(), ahpb.E_Http).(*ahpb.HttpRule); httpRule != nil {
					defaultPath := fmt.Sprintf("/%s/%s", apiName, method.GetName())
					PreserveDefaultHttpBinding(httpRule, defaultPath)
				}
			}
		}
	}

	newData, err := protov2.Marshal(fds)
	if err != nil {
		glog.Error("failed to marshal proto descriptor, error: ", err)
		return nil, fmt.Errorf("failed to marshal proto descriptor, error: %v", err)
	}
	return newData, nil
}