Make Message fields in Immutable protos nullable 1/2

This commit is contained in:
Eric Harding committed 2018-04-09 15:59:04 -04:00
1 parent 386b5f9b6b
commit 84dca4a714
2 files changed
+39 -7

No files matched your search

+10 -3
View File
@@ -123,13 +123,14 @@ def className(input):
# return name,is_complex,is_nullable
class TypeModel():
def __init__(self, mytype, islist=False, nullable=None, cpptype=None, immutabletype=None, keytype=None, natural_key=False):
def __init__(self, mytype, islist=False, nullable=None, cpptype=None, isMessage=False, immutabletype=None, keytype=None, natural_key=False):
self.cstype = mytype
self.cpptype = cpptype or mytype
self.nullable_type = nullable or mytype
self.natural_key = natural_key
self.keytype = keytype
self.isdict = bool(keytype)
self.ismessage = isMessage
self.islist = islist
self.immutable = immutabletype or mytype
@@ -159,19 +160,25 @@ def to_type_model(field, msg):
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]
map_key = to_type_model([f for f in t.field if f.name == 'key'][0], msg)
map_value = to_type_model([f for f in t.field if f.name == 'value'][0], msg)
itype = map_value.cstype+immutable_postfix
return TypeModel(
map_value.cstype,
cpptype=map_value.cpptype,
isMessage=True,
keytype=map_key.cstype,
immutabletype=map_value.cstype+immutable_postfix)
return TypeModel(full_name, islist, cpptype=short_name, immutabletype=full_name+immutable_postfix)
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)
else:
assert(not "Failed to get type for type id: %s " % field.type)
@@ -11,7 +11,7 @@ namespace {{package}} {
partial class {{name}} : bts.api.IImmutableMessage {
MessageDescriptor bts.api.IImmutableMessage.Descriptor => {{name}}.Descriptor;
}
partial struct {{name}}Immutable : bts.api.IImmutableMessage {
public partial struct {{name}}Immutable : bts.api.IImmutableMessage {
readonly {{name}} _impl;
public static MessageDescriptor Descriptor => {{name}}.Descriptor;
@@ -20,9 +20,11 @@ namespace {{package}} {
public void WriteTo(CodedOutputStream output) => _impl.WriteTo(output);
public {{name}}Immutable({{name}} src) {
if (src == null) throw new NullReferenceException("{name}");
_impl = src.Clone();
}
private {{name}}Immutable({{name}} src, bool clone) {
if (src == null) throw new NullReferenceException("{name}");
_impl = clone ? src.Clone() : src;
}
@@ -58,6 +60,7 @@ namespace {{package}} {
return !(left == right);
}
//
// Immutable field wrappers
{{#each field}}
{{#if type.isdict}}
@@ -67,7 +70,21 @@ namespace {{package}} {
// Can remove the cast if immutable==cstype
public IEnumerable<{{type.immutable}}> {{Name}} => _impl.{{Name}}.Select(t => ({{type.immutable}})t);
{{else}}
public {{type.immutable}} {{Name}} => ({{type.immutable}})_impl.{{Name}};
{{#if type.ismessage}}
public {{type.immutable}} {{Name}} {
get {
if (_impl.{{Name}} == null) throw new NullReferenceException("{{Name}}");
return ({{type.immutable}})_impl.{{Name}};
}
}
public bool Has{{Name}} {
get {
return _impl.{{Name}} != null;
}
}
{{else}}
public {{type.immutable}} {{Name}} => ({{type.immutable}})_impl.{{Name}};
{{/if}}
{{/if}}
{{/if}}
@@ -89,7 +106,11 @@ namespace {{package}} {
// Note: Should we generate an IEnumerable<immutabletype> here?
IEnumerable<{{type.cstype}}> {{name}}=null
{{else}}
{{{type.nullable_type}}} {{name}}=null
{{#if type.ismessage}}
{{{type.cstype}}} {{name}}=null
{{else}}
{{{type.nullable_type}}} {{name}}=null
{{/if}}
{{/if}}
{{/if}}
{{#unless @last}},{{/unless}}
@@ -113,7 +134,11 @@ namespace {{package}} {
{{#if type.islist}}
temp.{{Name}}.AddRange({{name}} ?? _impl.{{Name}});
{{else}}
temp.{{Name}} = {{name}} ?? _impl.{{Name}};
{{#if type.ismessage}}
temp.{{Name}} = {{name}} ?? _impl.{{Name}};
{{else}}
temp.{{Name}} = {{name}} ?? _impl.{{Name}};
{{/if}}
{{/if}}
{{/if}}
{{/each}}