From 9bea06bd0d9badcccbf004fcdda894ae00a68ba9 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 9 Oct 2017 14:49:45 -0400 Subject: [PATCH] spark-197 - plugin.py security_key generation picks the wrong key --- .vscode/launch.json | 17 +++++++++++ src/lib/api/service_compiler/plugin.py | 41 +++++++++++++++++--------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f198012d5..66f59625d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,23 @@ { "version": "0.2.0", "configurations": [ + { + "name": "Python", + "type": "python", + "request": "launch", + "stopOnEntry": false, + "pythonPath": "${config:python.pythonPath}", + "program": "${file}", + "cwd": "${workspaceRoot}", + "env": {}, + "envFile": "${workspaceRoot}/.env", + "args": [ "DEBUG" ], + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, { "name": "WSL Launch", "type": "cppdbg", diff --git a/src/lib/api/service_compiler/plugin.py b/src/lib/api/service_compiler/plugin.py index 41d6078fc..cc6360676 100755 --- a/src/lib/api/service_compiler/plugin.py +++ b/src/lib/api/service_compiler/plugin.py @@ -45,6 +45,7 @@ def type_name_cpp(name_with_ns): # Map protobuf raw types to intermediate type tree # MethodModel = namedtuple("MethodModel", "name comment server_streaming has_complex_key key_type key_field output_type_full output_type_cpp output_type input_type_full input_type_cpp input_type") +KeyData = namedtuple("KeyData", "type field") def transform_method(m, keydata): return MethodModel( name=m.name, @@ -87,25 +88,23 @@ def transform_message(i): return r ServiceModel = namedtuple("ServiceModel", "name comment method") -def transform_service(service, keydata): +KeyModel = namedtuple("KeyModel", "type field") +DefaultKeyModel = KeyModel("int", "id") +def transform_service(service, keymap): return ServiceModel( service.name, option(service.options, apioptions_pb2.service_description, ""), - [transform_method(m, keydata) for m in service.method]) + [transform_method(m, keymap.get(m.output_type, DefaultKeyModel)) for m in service.method]) FileModel = namedtuple("FileModel", "name package namespace_csharp message_type service enum_type") -KeyModel = namedtuple("KeyModel", "type field") -def transform_file(p): +def transform_file(p, keymap): message_types = [transform_message(m) for m in p.message_type] - # Note: this keydata thing is wrong... this will find the first key in the file not the key for the service - keydata = next((KeyModel(f.type,f.name) for m in message_types for f in m.field if f.is_key), None) - keydata = keydata or next((KeyModel(f.type,f.name) for m in message_types for f in m.field if f.name == 'id'), KeyModel("int","id")) return FileModel( name = p.name, package = p.package, namespace_csharp = p.package, message_type = message_types, - service = [transform_service(s, keydata) for s in p.service], + service = [transform_service(s, keymap) for s in p.service], enum_type = [transform_enum(e) for e in p.enum_type] ) @@ -229,14 +228,28 @@ def generate_code(request, response): 'type': lambda this,item: item.split('.')[-1:][0], 'camel': lambda _,f: camel_case(f), } - # for proto_file in request.proto_file: - # todo: for all files on the input - # get a list of their messages and Key fields - # pass this as a dictionary into transform_file - # pass + keymap = {} + # Generate a map of keys for all types across files + for proto_file in request.proto_file: + for msg in proto_file.message_type: + # check eachfield + found_key = False + namekey = ".{}.{}".format(proto_file.package,msg.name) + for field in msg.field: + if option(field.options, apioptions_pb2.key, False): + found_key = True + keymap[namekey] = KeyData(to_type_model(field, msg), field.name) + break + # If no field was annotated as a key but there's a field named "id" use that + if not found_key: + for field in msg.field: + if field.name == "id": + keymap[namekey] = KeyData(to_type_model(field, msg), field.name) + break + for proto_file in request.proto_file: if proto_file.name in request.file_to_generate: - proto_file = transform_file(proto_file) + proto_file = transform_file(proto_file, keymap) # Path on response.file name is interpreted differently by the c# and c++ code gen filename = ('strip_out_path' in params) and os.path.split(proto_file.name)[1] or proto_file.name source_name = os.path.splitext(filename)[0]