From 623fce6e8ff4bec5137e62bb654f571ff8fb1e11 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 1 Oct 2023 13:16:49 -0400 Subject: [PATCH] build: Enable nullable in proto generated cs --- src/client/spark_api/MarketData/SecurityMD.cs | 2 +- .../spark_api/MarketData/SyntheticTuple.cs | 2 +- src/client/spark_api_server/BookDumper.cs | 2 +- src/client/spark_api_server/SecurityDumper.cs | 2 +- src/lib/api/service_compiler/plugin.py | 32 ++++++++----------- .../templates/c#/immutable.pb.cs | 25 +++++++-------- 6 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/client/spark_api/MarketData/SecurityMD.cs b/src/client/spark_api/MarketData/SecurityMD.cs index 3807dd154..90c3ac9f9 100644 --- a/src/client/spark_api/MarketData/SecurityMD.cs +++ b/src/client/spark_api/MarketData/SecurityMD.cs @@ -52,7 +52,7 @@ namespace mbts { security.Equals(other.security); public override string ToString() => - security.ToString(); + security.ToString()!; public static bool operator ==(SecurityMD left, SecurityMD right) => Equals(left, right); diff --git a/src/client/spark_api/MarketData/SyntheticTuple.cs b/src/client/spark_api/MarketData/SyntheticTuple.cs index 9acc80682..fc2a90a51 100644 --- a/src/client/spark_api/MarketData/SyntheticTuple.cs +++ b/src/client/spark_api/MarketData/SyntheticTuple.cs @@ -43,7 +43,7 @@ namespace bts { security.Id.GetHashCode(); public override string ToString() => - user_name == null ? security.ToString() : $"{user_name}:{security}"; + user_name == null ? security.ToString()! : $"{user_name}:{security}"; public static bool operator ==(SyntheticSecurityTuple a, SyntheticSecurityTuple b) => a.Equals(b); diff --git a/src/client/spark_api_server/BookDumper.cs b/src/client/spark_api_server/BookDumper.cs index 6fab3aeca..482d7eaeb 100644 --- a/src/client/spark_api_server/BookDumper.cs +++ b/src/client/spark_api_server/BookDumper.cs @@ -29,7 +29,7 @@ namespace spark_api_server { var md_source = new MDSource(TimeSpan.FromSeconds(0.5)); var md_client = new MarketDataClient(md_source); var name_set = opts.securities.empty_if_null().ToHashSet(); - var books_src = md_client.books(s => name_set.Contains(s.ToString())) + var books_src = md_client.books(s => name_set.Contains(s.ToString()!)) .Cache().ToObservable(); var sub = books_src.Subscribe(on_books); await books_src.LastOrDefaultAsync(); diff --git a/src/client/spark_api_server/SecurityDumper.cs b/src/client/spark_api_server/SecurityDumper.cs index 6a9c45187..0ac4d2a10 100644 --- a/src/client/spark_api_server/SecurityDumper.cs +++ b/src/client/spark_api_server/SecurityDumper.cs @@ -47,7 +47,7 @@ namespace spark_api_server { string fmt(SecurityImmutable sec) { if (!opts.verbose) { - return sec.ToString(); + return sec.ToString()!; } var fmt_settings = Google.Protobuf.JsonFormatter.Settings.Default; var formatter = new Google.Protobuf.JsonFormatter(fmt_settings); diff --git a/src/lib/api/service_compiler/plugin.py b/src/lib/api/service_compiler/plugin.py index 300a9bcd3..b05711a8e 100755 --- a/src/lib/api/service_compiler/plugin.py +++ b/src/lib/api/service_compiler/plugin.py @@ -135,14 +135,13 @@ def transform_file(p, keymap, imports): def className(input): return input.split('.')[-1:][0] -# return name,is_complex,is_nullable +# return name,is_complex class TypeModel(): - def __init__(self, mytype, islist=False, nullable=None, cpptype=None, jstype=None, fstype=None, isMessage=False, immutabletype=None, keytype=None, natural_key=False): + def __init__(self, mytype, islist=False, cpptype=None, jstype=None, fstype=None, isMessage=False, immutabletype=None, keytype=None, natural_key=False): self.cstype = mytype self.cpptype = cpptype or mytype self.jstype = jstype or mytype self.fstype = fstype or mytype - self.nullable_type = nullable or mytype self.natural_key = natural_key self.keytype = keytype self.isdict = bool(keytype) @@ -153,17 +152,17 @@ 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?", jstype='number', natural_key=True) - elif field.type == types.TYPE_INT64: return TypeModel("long", islist, "long?", jstype='number', fstype='int64') - 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', fstype='int64') - elif field.type == types.TYPE_UINT32: return TypeModel("uint", islist, "uint?", "unsigned", jstype='number', fstype='uint32', natural_key=True) - elif field.type == types.TYPE_UINT64: return TypeModel("ulong", islist, "ulong?", "unsigned long", jstype='number', fstype='uint64', 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', fstype='float32') + if field.type == types.TYPE_INT32: return TypeModel("int", islist, jstype='number', natural_key=True) + elif field.type == types.TYPE_INT64: return TypeModel("long", islist, jstype='number', fstype='int64') + elif field.type == types.TYPE_SINT32: return TypeModel("int", islist, jstype='number', natural_key=True) + elif field.type == types.TYPE_SINT64: return TypeModel("long", islist, jstype='number', fstype='int64') + elif field.type == types.TYPE_UINT32: return TypeModel("uint", islist, "unsigned", jstype='number', fstype='uint32', natural_key=True) + elif field.type == types.TYPE_UINT64: return TypeModel("ulong", islist, "unsigned long", jstype='number', fstype='uint64', natural_key=True) + elif field.type == types.TYPE_DOUBLE: return TypeModel("double", islist, jstype='number') + elif field.type == types.TYPE_FLOAT: return TypeModel("float", islist, jstype='number', fstype='float32') 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", jstype='Uint8Array', fstype='Fable.Core.JS.Uint8Array') - elif field.type == types.TYPE_BOOL: return TypeModel("bool", islist, "bool?", jstype='boolean', natural_key=True) + elif field.type == types.TYPE_BOOL: return TypeModel("bool", islist, 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('.') @@ -172,16 +171,14 @@ def to_type_model(field, msg): if short_name in nested_enums: full_name = "{0}.Types.{1}".format(msg.name, short_name) fs_name = "{0}.{1}".format(msg.name, short_name) - return TypeModel(full_name, islist, nullable=full_name+'?', cpptype=short_name, jstype='number', fstype=fs_name, natural_key=True) + return TypeModel(full_name, islist, cpptype=short_name, jstype='number', fstype=fs_name, 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('.') short_name = full_name.split('.')[-1:][0] immutable_postfix = "Immutable" - nullable_postfix = "?" if full_name.startswith('google'): immutable_postfix = "" - nullable_postfix = "" full_name = short_name if short_name in map_types: t = map_types[short_name] @@ -195,10 +192,9 @@ def to_type_model(field, msg): fstype=map_value.fstype, isMessage=True, keytype=map_key, - immutabletype=itype, - nullable=itype+nullable_postfix) + immutabletype=itype) itype = full_name+immutable_postfix - return TypeModel(full_name, islist, cpptype=short_name, jstype=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) else: assert(not "Failed to get type for type id: %s " % field.type) diff --git a/src/lib/api/service_compiler/templates/c#/immutable.pb.cs b/src/lib/api/service_compiler/templates/c#/immutable.pb.cs index bf2bd0854..e8a997f06 100644 --- a/src/lib/api/service_compiler/templates/c#/immutable.pb.cs +++ b/src/lib/api/service_compiler/templates/c#/immutable.pb.cs @@ -1,4 +1,5 @@ #region Generated code +#nullable enable namespace {{package}} { using Spark.Api; @@ -62,7 +63,7 @@ namespace {{package}} { public bool Equals({{name}}Immutable other) => _impl.Equals(other._impl); - public override bool Equals(object obj) => + public override bool Equals(object? obj) => obj is {{name}}Immutable other && Equals(other); public bool Equals({{name}}Immutable? other) { @@ -79,8 +80,9 @@ namespace {{package}} { public override int GetHashCode() => _impl.GetHashCode(); - public override string ToString() { - bool didrun = false; string result = null; + public override string? ToString() { + bool didrun = false; + string? result = null; ToStringOverride(ref didrun, ref result); if(didrun) return result; return _impl.ToString(); @@ -89,7 +91,7 @@ namespace {{package}} { partial void EqualsOverride( ref bool didrun, ref bool result, {{name}}Immutable other); - partial void ToStringOverride(ref bool didrun, ref string result); + partial void ToStringOverride(ref bool didrun, ref string? result); // Implicit conversion operator public static implicit operator Nullable<{{name}}Immutable>({{name}} other) => @@ -113,12 +115,12 @@ namespace {{package}} { {{#if type.islist}} // Can remove the cast if immutable==cstype public IEnumerable<{{type.immutable}}> {{Name}} => - _impl.{{Name}}.Select(t => ({{type.immutable}})t); + _impl.{{Name}}.Select(t => ({{type.immutable}})t!); {{else}} {{#if type.ismessage}} public {{type.immutable}} {{Name}} => _impl.{{Name}} is null ? throw new NullReferenceException("{{Name}}") : - ({{type.immutable}})_impl.{{Name}}; + ({{type.immutable}})_impl.{{Name}}!; public bool Has{{Name}} => _impl.{{Name}} != null; @@ -141,15 +143,12 @@ namespace {{package}} { // Arguments {{#each field}} {{#if type.isdict}} - IReadOnlyDictionary<{{type.keytype.cstype}},{{type.cstype}}> {{name}} = null, - IDictionary<{{type.keytype.cstype}},{{type.cstype}}> {{name}}_mut = null{{#unless @last}},{{/unless}} + IReadOnlyDictionary<{{type.keytype.cstype}},{{type.cstype}}>? {{name}} = null, + IDictionary<{{type.keytype.cstype}},{{type.cstype}}>? {{name}}_mut = null{{#unless @last}},{{/unless}} {{else}}{{#if type.islist}} - IEnumerable<{{type.cstype}}> {{name}} = null{{#unless @last}},{{/unless}} - {{else}}{{#if type.ismessage}} - {{{type.cstype}}} {{name}} = null{{#unless @last}},{{/unless}} + IEnumerable<{{type.cstype}}>? {{name}} = null{{#unless @last}},{{/unless}} {{else}} - {{{type.nullable_type}}} {{name}} = null{{#unless @last}},{{/unless}} - {{/if}} + {{{type.cstype}}}? {{name}} = null{{#unless @last}},{{/unless}} {{/if}} {{/if}} {{/each}}