diff --git a/lib/src/Data/RepresentationType.dart b/lib/src/Data/RepresentationType.dart index 9e5eb6b..204cfcf 100644 --- a/lib/src/Data/RepresentationType.dart +++ b/lib/src/Data/RepresentationType.dart @@ -167,7 +167,7 @@ class RepresentationType { return Warehouse.getTemplateByClassId(guid!, TemplateType.Record) ?.definedType; else if (identifier == RepresentationTypeIdentifier.TypedResource) - return Warehouse.getTemplateByClassId(guid!, TemplateType.Unspecified) + return Warehouse.getTemplateByClassId(guid!, TemplateType.Resource) ?.definedType; else if (identifier == RepresentationTypeIdentifier.Enum) return Warehouse.getTemplateByClassId(guid!, TemplateType.Enum) diff --git a/lib/src/Proxy/TemplateGenerator.dart b/lib/src/Proxy/TemplateGenerator.dart index 1e0d4f0..bfd059a 100644 --- a/lib/src/Proxy/TemplateGenerator.dart +++ b/lib/src/Proxy/TemplateGenerator.dart @@ -147,8 +147,7 @@ class TemplateGenerator { name = _translateClassName(templates .singleWhere((x) => x.classId == representationType.guid && - (x.type == TemplateType.Resource || - x.type == TemplateType.Wrapper)) + (x.type == TemplateType.Resource)) .className); } else if (representationType.identifier == RepresentationTypeIdentifier.TypedRecord) { @@ -345,8 +344,7 @@ class TemplateGenerator { var defineCreators = templates.map((tmp) { // creator var className = _translateClassName(tmp.className); - if (tmp.type == TemplateType.Resource || - tmp.type == TemplateType.Wrapper) { + if (tmp.type == TemplateType.Resource) { return "Warehouse.defineType<${className}>(() => ${className}(), RepresentationType(RepresentationTypeIdentifier.TypedResource, false, Guid.parse('${tmp.classId.toString()}')));\r\n"; } else if (tmp.type == TemplateType.Record) { return "Warehouse.defineType<${className}>(() => ${className}(), RepresentationType(RepresentationTypeIdentifier.TypedRecord, false, Guid.parse('${tmp.classId.toString()}')));\r\n"; @@ -429,8 +427,7 @@ class TemplateGenerator { parentName = _translateClassName(templates .singleWhere((x) => (x.classId == template.parentId) && - (x.type == TemplateType.Resource || - x.type == TemplateType.Wrapper)) + (x.type == TemplateType.Resource)) .className); rt.writeln("class ${className} extends ${parentName} {"); } else { diff --git a/lib/src/Resource/Template/TemplateType.dart b/lib/src/Resource/Template/TemplateType.dart index 3bd51c6..ee77035 100644 --- a/lib/src/Resource/Template/TemplateType.dart +++ b/lib/src/Resource/Template/TemplateType.dart @@ -1,7 +1,5 @@ enum TemplateType { - Unspecified, Resource, Record, - Wrapper, Enum } diff --git a/lib/src/Resource/Template/TypeTemplate.dart b/lib/src/Resource/Template/TypeTemplate.dart index 10a5658..907a216 100644 --- a/lib/src/Resource/Template/TypeTemplate.dart +++ b/lib/src/Resource/Template/TypeTemplate.dart @@ -23,7 +23,11 @@ import 'ConstantTemplate.dart'; import 'TemplateType.dart'; class TypeTemplate { - late Guid _classId; + late bool _isWrapper; + + bool get isWrapper => _isWrapper; + + late Guid _classId; Guid? _parentId = null; String? _annotation; @@ -125,24 +129,18 @@ class TypeTemplate { var instance = Warehouse.createInstance(type); - TemplateDescriber describer; - - if (instance is DistributedResource) { - _templateType = TemplateType.Wrapper; - describer = instance.template; - } else if (instance is IResource) { + if (instance is IResource) { _templateType = TemplateType.Resource; - describer = instance.template; } else if (instance is IRecord) { _templateType = TemplateType.Record; - describer = instance.template; } else if (instance is IEnum) { _templateType = TemplateType.Enum; - describer = instance.template; } else throw new Exception( "Type must implement IResource, IRecord, IEnum or a subtype of DistributedResource."); + _isWrapper = (instance is DistributedResource); + // if (instance is IRecord) // _templateType = TemplateType.Record; // else if (instance is IResource) @@ -150,6 +148,8 @@ class TypeTemplate { // else // throw new Exception("Type is neither a resource nor a record."); + TemplateDescriber describer = instance.template; + _definedType = type; _className = describer.nameSpace; @@ -199,53 +199,56 @@ class TypeTemplate { } } - if (describer.functions != null) { - var funcs = describer.functions as List; + if (_templateType == TemplateType.Resource) { + if (describer.functions != null) { + var funcs = describer.functions as List; - for (var i = 0; i < funcs.length; i++) { - var fi = funcs[i]; + for (var i = 0; i < funcs.length; i++) { + var fi = funcs[i]; - List args = fi.args - .asMap() - .entries - .map((arg) => ArgumentTemplate( - arg.value.name, - RepresentationType.fromType(arg.value.type) ?? - RepresentationType.Dynamic, - arg.value.optional, - arg.key)) - .toList(); + List args = fi.args + .asMap() + .entries + .map((arg) => ArgumentTemplate( + arg.value.name, + RepresentationType.fromType(arg.value.type) ?? + RepresentationType.Dynamic, + arg.value.optional, + arg.key)) + .toList(); - var ft = FunctionTemplate( - this, - i, - fi.name, - false, - fi.isStatic, - args, - RepresentationType.fromType(fi.returnType) ?? - RepresentationType.Void, - fi.annotation); + var ft = FunctionTemplate( + this, + i, + fi.name, + false, + fi.isStatic, + args, + RepresentationType.fromType(fi.returnType) ?? + RepresentationType.Void, + fi.annotation); - functions.add(ft); + functions.add(ft); + } } - } - if (describer.events != null) { - var evts = describer.events as List; - for (var i = 0; i < evts.length; i++) { - var ei = evts[i]; + if (describer.events != null) { + var evts = describer.events as List; + for (var i = 0; i < evts.length; i++) { + var ei = evts[i]; - var et = new EventTemplate( - this, - i, - ei.name, - false, - RepresentationType.fromType(ei.type) ?? RepresentationType.Dynamic, - ei.annotation, - ei.listenable); + var et = new EventTemplate( + this, + i, + ei.name, + false, + RepresentationType.fromType(ei.type) ?? + RepresentationType.Dynamic, + ei.annotation, + ei.listenable); - events.add(et); + events.add(et); + } } } diff --git a/lib/src/Resource/Warehouse.dart b/lib/src/Resource/Warehouse.dart index 2e2362a..9939b28 100644 --- a/lib/src/Resource/Warehouse.dart +++ b/lib/src/Resource/Warehouse.dart @@ -22,7 +22,6 @@ SOFTWARE. */ - import '../Data/IntType.dart'; import '../Data/TransmissionType.dart'; @@ -66,10 +65,8 @@ class Warehouse { static KeyList> _initTemplates() { var rt = new KeyList>(); - rt.add(TemplateType.Unspecified, new KeyList()); rt.add(TemplateType.Resource, new KeyList()); rt.add(TemplateType.Record, new KeyList()); - rt.add(TemplateType.Wrapper, new KeyList()); rt.add(TemplateType.Enum, new KeyList()); return rt; @@ -561,6 +558,9 @@ class Warehouse { /// /// Resource template. static void putTemplate(TypeTemplate template) { + if (_templates[template.type]?.containsKey(template.classId) ?? false) + throw Exception("Template with same class Id already exists."); + _templates[template.type]?[template.classId] = template; } @@ -588,18 +588,18 @@ class Warehouse { /// Class Id. /// Resource template. static TypeTemplate? getTemplateByClassId(Guid classId, - [TemplateType templateType = TemplateType.Unspecified]) { - if (templateType == TemplateType.Unspecified) { - // look in resources + [TemplateType? templateType = null]) { + if (templateType == null) { + // look into resources var template = _templates[TemplateType.Resource]?[classId]; if (template != null) return template; - // look in records + // look into records template = _templates[TemplateType.Record]?[classId]; if (template != null) return template; - // look in wrappers - template = _templates[TemplateType.Wrapper]?[classId]; + // look into enums + template = _templates[TemplateType.Enum]?[classId]; return template; } else { return _templates[templateType]?[classId]; @@ -612,22 +612,22 @@ class Warehouse { /// Class name. /// Resource template. static TypeTemplate? getTemplateByClassName(String className, - [TemplateType templateType = TemplateType.Unspecified]) { - if (templateType == TemplateType.Unspecified) { - // look in resources + [TemplateType? templateType = null]) { + if (templateType == null) { + // look into resources var template = _templates[TemplateType.Resource] ?.values .firstWhere((x) => x.className == className); if (template != null) return template; - // look in records + // look into records template = _templates[TemplateType.Record] ?.values .firstWhere((x) => x.className == className); if (template != null) return template; - // look in wrappers - template = _templates[TemplateType.Wrapper] + // look into wrappers + template = _templates[TemplateType.Enum] ?.values .firstWhere((x) => x.className == className); return template;