SPARK-1000 Add template for typescript generation.
This commit is contained in:
1 parent
9bd6c53642
commit
fc5ebd2cb0
4 files changed
+89
-27
No files matched your search
@@ -104,15 +104,16 @@ def transform_service(service, keymap):
|
||||
option(service.options, apioptions_pb2.service_description, ""),
|
||||
[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")
|
||||
def transform_file(p, keymap):
|
||||
FileModel = namedtuple("FileModel", "name package namespace_csharp message_type service enum_type imports")
|
||||
def transform_file(p, keymap, imports):
|
||||
return FileModel(
|
||||
name = p.name,
|
||||
package = p.package,
|
||||
namespace_csharp = p.package,
|
||||
message_type = [transform_message(m) for m in p.message_type],
|
||||
service = [transform_service(s, keymap) for s in p.service],
|
||||
enum_type = [transform_enum(e) for e in p.enum_type]
|
||||
enum_type = [transform_enum(e) for e in p.enum_type],
|
||||
imports = imports
|
||||
)
|
||||
|
||||
#
|
||||
@@ -123,9 +124,10 @@ def className(input):
|
||||
|
||||
# return name,is_complex,is_nullable
|
||||
class TypeModel():
|
||||
def __init__(self, mytype, islist=False, nullable=None, cpptype=None, isMessage=False, immutabletype=None, keytype=None, natural_key=False):
|
||||
def __init__(self, mytype, islist=False, nullable=None, cpptype=None, jstype=None, isMessage=False, immutabletype=None, keytype=None, natural_key=False):
|
||||
self.cstype = mytype
|
||||
self.cpptype = cpptype or mytype
|
||||
self.jstype = jstype or mytype
|
||||
self.nullable_type = nullable or mytype
|
||||
self.natural_key = natural_key
|
||||
self.keytype = keytype
|
||||
@@ -137,24 +139,24 @@ class TypeModel():
|
||||
def to_type_model(field, msg):
|
||||
types = FieldDescriptorProto
|
||||
islist = field.label == FieldDescriptorProto.LABEL_REPEATED
|
||||
if field.type == types.TYPE_INT32: return TypeModel("int", islist, "int?", natural_key=True)
|
||||
elif field.type == types.TYPE_INT64: return TypeModel("long", islist, "long?")
|
||||
elif field.type == types.TYPE_SINT32: return TypeModel("int", islist, "int?", natural_key=True)
|
||||
elif field.type == types.TYPE_SINT64: return TypeModel("long", islist, "long?")
|
||||
elif field.type == types.TYPE_UINT32: return TypeModel("uint", islist, "uint?", "unsigned", natural_key=True)
|
||||
elif field.type == types.TYPE_UINT64: return TypeModel("ulong", islist, "ulong?", "unsigned long", natural_key=True)
|
||||
elif field.type == types.TYPE_DOUBLE: return TypeModel("double", islist, "double?")
|
||||
elif field.type == types.TYPE_FLOAT: return TypeModel("float", islist, "float?")
|
||||
if field.type == types.TYPE_INT32: return TypeModel("int", islist, "int?", jstype='number', natural_key=True)
|
||||
elif field.type == types.TYPE_INT64: return TypeModel("long", islist, "long?", jstype='number')
|
||||
elif field.type == types.TYPE_SINT32: return TypeModel("int", islist, "int?", jstype='number', natural_key=True)
|
||||
elif field.type == types.TYPE_SINT64: return TypeModel("long", islist, "long?", jstype='number')
|
||||
elif field.type == types.TYPE_UINT32: return TypeModel("uint", islist, "uint?", "unsigned", jstype='number', natural_key=True)
|
||||
elif field.type == types.TYPE_UINT64: return TypeModel("ulong", islist, "ulong?", "unsigned long", jstype='number', natural_key=True)
|
||||
elif field.type == types.TYPE_DOUBLE: return TypeModel("double", islist, "double?", jstype='number')
|
||||
elif field.type == types.TYPE_FLOAT: return TypeModel("float", islist, "float?", jstype='number')
|
||||
elif field.type == types.TYPE_STRING: return TypeModel("string", islist, cpptype="std::string")
|
||||
elif field.type == types.TYPE_BYTES: return TypeModel("ByteString", islist, cpptype="std::string")
|
||||
elif field.type == types.TYPE_BOOL: return TypeModel("bool", islist, "bool?", natural_key=True)
|
||||
elif field.type == types.TYPE_BYTES: return TypeModel("ByteString", islist, cpptype="std::string", jstype='Uint8Array')
|
||||
elif field.type == types.TYPE_BOOL: return TypeModel("bool", islist, "bool?", jstype='boolean', natural_key=True)
|
||||
elif field.type == types.TYPE_ENUM:
|
||||
nested_enums = [x.name for x in msg.enum_type]
|
||||
full_name = field.type_name.lstrip('.')
|
||||
short_name = full_name.split('.')[-1:][0]
|
||||
if short_name in nested_enums:
|
||||
full_name = "{0}.Types.{1}".format(msg.name, short_name)
|
||||
return TypeModel(full_name, islist, nullable=full_name+'?', cpptype=short_name, natural_key=True)
|
||||
return TypeModel(full_name, islist, nullable=full_name+'?', cpptype=short_name, jstype='number', natural_key=True)
|
||||
elif field.type == types.TYPE_MESSAGE:
|
||||
map_types = dict([(x.name, x) for x in msg.nested_type if x.options.map_entry == True])
|
||||
full_name = field.type_name.lstrip('.')
|
||||
@@ -173,12 +175,13 @@ def to_type_model(field, msg):
|
||||
return TypeModel(
|
||||
map_value.cstype,
|
||||
cpptype=map_value.cpptype,
|
||||
jstype=map_value.jstype,
|
||||
isMessage=True,
|
||||
keytype=map_key.cstype,
|
||||
keytype=map_key,
|
||||
immutabletype=itype,
|
||||
nullable=itype+nullable_postfix)
|
||||
itype = full_name+immutable_postfix
|
||||
return TypeModel(full_name, islist, cpptype=short_name, isMessage=True, immutabletype=itype, nullable=itype+nullable_postfix)
|
||||
return TypeModel(full_name, islist, cpptype=short_name, jstype=short_name, isMessage=True, immutabletype=itype, nullable=itype+nullable_postfix)
|
||||
else:
|
||||
assert(not "Failed to get type for type id: %s " % field.type)
|
||||
|
||||
@@ -227,11 +230,13 @@ def generate_code(request, response):
|
||||
if not "lang" in params:
|
||||
response.error = "You must provide a language for the service generator plugin"
|
||||
return
|
||||
lang = params["lang"]
|
||||
if lang == "cpp": generate_insertions(params, request, response)
|
||||
langs = params["lang"].split('+')
|
||||
if 'cpp' in langs: generate_insertions(params, request, response)
|
||||
if not 'no_services' in params:
|
||||
current_proto_file = None
|
||||
templates = [(f, loadTemplate(lang, f)) for f in os.listdir("templates/" + lang)]
|
||||
templates = []
|
||||
for lang in langs:
|
||||
templates += [(f, loadTemplate(lang, f)) for f in os.listdir("templates/" + lang)]
|
||||
helpers = { 'upper': lambda this,item: item.upper(),
|
||||
'lower': lambda this,item: item.lower(),
|
||||
'cap': lambda this,item: item[0].upper() + item[1:],
|
||||
@@ -259,10 +264,14 @@ def generate_code(request, response):
|
||||
if field.name == "id":
|
||||
keymap[namekey] = KeyData(to_type_model(field, msg), field.name)
|
||||
break
|
||||
imports = []
|
||||
for proto_file in request.proto_file:
|
||||
if not proto_file.name in request.file_to_generate:
|
||||
imports.append(transform_file(proto_file, keymap, []))
|
||||
|
||||
for proto_file in request.proto_file:
|
||||
if proto_file.name in request.file_to_generate:
|
||||
proto_file = transform_file(proto_file, keymap)
|
||||
proto_file = transform_file(proto_file, keymap, imports)
|
||||
# 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]
|
||||
@@ -291,4 +300,3 @@ if __name__ == "__main__":
|
||||
output = response.SerializeToString()
|
||||
sys.stdout.write(output)
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace {{package}} {
|
||||
// Immutable field wrappers
|
||||
{{#each field}}
|
||||
{{#if type.isdict}}
|
||||
public IReadOnlyDictionary<{{type.keytype}},{{type.cstype}}> {{Name}} => _impl.{{Name}};
|
||||
public IReadOnlyDictionary<{{type.keytype.cstype}},{{type.cstype}}> {{Name}} => _impl.{{Name}};
|
||||
{{else}}
|
||||
{{#if type.islist}}
|
||||
// Can remove the cast if immutable==cstype
|
||||
@@ -99,8 +99,8 @@ namespace {{package}} {
|
||||
// Arguments
|
||||
{{#each field}}
|
||||
{{#if type.isdict}}
|
||||
IReadOnlyDictionary<{{type.keytype}},{{type.cstype}}> {{name}}=null,
|
||||
IDictionary<{{type.keytype}},{{type.cstype}}> {{name}}_mut=null
|
||||
IReadOnlyDictionary<{{type.keytype.cstype}},{{type.cstype}}> {{name}}=null,
|
||||
IDictionary<{{type.keytype.cstype}},{{type.cstype}}> {{name}}_mut=null
|
||||
{{else}}
|
||||
{{#if type.islist}}
|
||||
// Note: Should we generate an IEnumerable<immutabletype> here?
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Generated code -- You probably should not edit
|
||||
import {root} from 'proto'
|
||||
import {ServiceContext} from 'ServiceContext'
|
||||
import { assertValid } from 'bts_assert'
|
||||
|
||||
{{#each imports}}
|
||||
// {{name}}
|
||||
{{/each}}
|
||||
|
||||
{{#each message_type}}
|
||||
export interface {{name}} {
|
||||
{{#each field}}
|
||||
{{#if type.isdict}}
|
||||
readonly {{name}} : Map<{{type.keytype.jstype}},{{type.jstype}}>
|
||||
{{else}}
|
||||
{{#if type.islist}}
|
||||
readonly {{name}} : {{type.jstype}}[]
|
||||
{{else}}
|
||||
readonly {{name}} : {{type.jstype}}
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
{{/each}}
|
||||
}
|
||||
const {{name}}Proto = root.lookupType("{{../package}}.{{name}}")
|
||||
{{/each}}
|
||||
|
||||
|
||||
{{#each service}}
|
||||
//{{comment}}
|
||||
export class {{name}} {
|
||||
private context : ServiceContext
|
||||
private qualifier : string
|
||||
constructor(sc : ServiceContext, qualifier?:string) {
|
||||
this.context = sc
|
||||
this.qualifier = qualifier||""
|
||||
}
|
||||
|
||||
{{#each method}}
|
||||
{{#if server_streaming}}
|
||||
// todo: {{name}}
|
||||
{{else}}
|
||||
// {{comment}}
|
||||
async {{name}}(request : {{input_type}}) : Promise<{{output_type}}> {
|
||||
const r = {{input_type}}Proto.create(request)
|
||||
assertValid({{input_type}}Proto, r)
|
||||
const buffer = {{input_type}}Proto.encode(r).finish()
|
||||
const result = await this.context.call("{{../name}}", this.qualifier, {{@index}}, buffer)
|
||||
return {{output_type}}Proto.decode(result)
|
||||
}
|
||||
{{/if}}
|
||||
{{/each}}
|
||||
}
|
||||
|
||||
{{/each}}
|
||||
@@ -96,7 +96,7 @@ function(Service_Compiler_Generate_Cpp SRCS HDRS)
|
||||
-I ${CMAKE_SOURCE_DIR}/src
|
||||
${ABS_FIL}
|
||||
|
||||
DEPENDS ${ABS_FIL} ${TEMPLATES}
|
||||
DEPENDS ${ABS_FIL} ${TEMPLATES} ${CMAKE_CURRENT_SOURCE_DIR}/api/service_compiler/plugin.py
|
||||
|
||||
COMMENT "Running C++ service compiler on ${FIL}"
|
||||
VERBATIM )
|
||||
@@ -120,7 +120,7 @@ function(Service_Compiler_Generate_Cpp SRCS HDRS)
|
||||
-I ${CMAKE_SOURCE_DIR}/src
|
||||
${ABS_FIL}
|
||||
|
||||
DEPENDS ${ABS_FIL} ${TEMPLATES}
|
||||
DEPENDS ${ABS_FIL} ${TEMPLATES} ${CMAKE_CURRENT_SOURCE_DIR}/api/service_compiler/plugin.py
|
||||
|
||||
COMMENT "Running C++ service compiler on ${FIL}"
|
||||
VERBATIM )
|
||||
|
||||
Reference in new issue
Block a user