Skip to content

Commit c0d9fee

Browse files
committed
Reflection
- fix multiple output of one model
1 parent 883c096 commit c0d9fee

File tree

6 files changed

+67
-45
lines changed

6 files changed

+67
-45
lines changed

Common/Main.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class Main
1212
public static bool Run(string[] args)
1313
{
1414
#if DEBUG
15-
if (args[0] != "statistics")
15+
if (args.Length > 0 && args[0] != "statistics")
1616
{
1717
System.Diagnostics.Debugger.Launch();
1818
}

Common/Transfer/GenericModelTransferObject.cs

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public override string Comment
117117
set => this.Template.Comment = value;
118118
}
119119

120+
public override Type Type
121+
{
122+
get => this.Template.Type;
123+
set => this.Template.Type = value;
124+
}
125+
120126
public GenericModelTransferObject(ModelTransferObject template)
121127
{
122128
this.Template = template is GenericModelTransferObject genericTemplate ? genericTemplate.Template : template;

Common/Transfer/ModelTransferObject.cs

+12-23
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,25 @@
77
namespace KY.Generator.Transfer
88
{
99
[DebuggerDisplay("ModelTransferObject {Namespace,nq}.{Name,nq}")]
10-
public class ModelTransferObject : TypeTransferObject, ICloneable
10+
public class ModelTransferObject : TypeTransferObject
1111
{
1212
public virtual bool IsEnum { get; set; }
1313
public virtual bool IsAbstract { get; set; }
1414
public virtual Dictionary<string, int> EnumValues { get; set; }
1515
public virtual ModelTransferObject BasedOn { get; set; }
16+
17+
[NotCloneable]
1618
public virtual ILanguage Language { get; set; }
17-
public virtual List<TypeTransferObject> Interfaces { get; }
18-
public virtual List<FieldTransferObject> Constants { get; }
19-
public virtual List<FieldTransferObject> Fields { get; }
20-
public virtual List<PropertyTransferObject> Properties { get; }
21-
public virtual List<MethodTransferObject> Methods { get; }
22-
public virtual List<string> Usings { get; }
23-
public virtual string Comment { get; set; }
2419

25-
public ModelTransferObject()
26-
{
27-
this.Interfaces = new List<TypeTransferObject>();
28-
this.Constants = new List<FieldTransferObject>();
29-
this.Fields = new List<FieldTransferObject>();
30-
this.Properties = new List<PropertyTransferObject>();
31-
this.Usings = new List<string>();
32-
this.Methods = new List<MethodTransferObject>();
33-
}
20+
public virtual List<TypeTransferObject> Interfaces { get; } = new();
21+
public virtual List<FieldTransferObject> Constants { get; } = new();
22+
public virtual List<FieldTransferObject> Fields { get; } = new();
23+
public virtual List<PropertyTransferObject> Properties { get; } = new();
24+
public virtual List<MethodTransferObject> Methods { get; } = new();
25+
public virtual List<string> Usings { get; } = new();
26+
public virtual string Comment { get; set; }
3427

35-
object ICloneable.Clone()
36-
{
37-
ModelTransferObject clone = this.Clone(false, nameof(this.Language));
38-
clone.Language = this.Language;
39-
return clone;
40-
}
28+
[NotCloneable]
29+
public virtual Type Type { get; set; }
4130
}
4231
}

Fluent/Commands/FluentCommand.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public override IGeneratorCommandResult Run()
5656
{
5757
main.Execute();
5858
}
59-
List<ITransferObject> transferObjects = this.resolver.Get<IEnvironment>().TransferObjects;
59+
IEnvironment environment = this.resolver.Get<IEnvironment>();
6060
foreach (IFluentInternalSyntax syntax in main.Syntaxes)
6161
{
6262
IGeneratorCommandResult commandResult = syntax.Run();
6363
if (!commandResult.Success)
6464
{
6565
return commandResult;
6666
}
67-
transferObjects.AddIfNotExists(syntax.Resolver.Get<List<ITransferObject>>());
67+
environment.TransferObjects.AddIfNotExists(syntax.Resolver.Get<List<ITransferObject>>());
6868
}
6969
}
7070
return this.Success();

Reflection/Readers/ReflectionModelReader.cs

+44-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ReflectionModelReader(Options options, List<ITransferObject> transferObje
2525

2626
public ModelTransferObject Read(Type type, IOptions caller = null)
2727
{
28-
ModelTransferObject model = new() { Language = ReflectionLanguage.Instance };
28+
ModelTransferObject model = new() { Language = ReflectionLanguage.Instance, Type = type };
2929
model.Name = model.OriginalName = type.Name;
3030
model.Namespace = type.Namespace;
3131
model.IsNullable = !type.IsValueType;
@@ -34,17 +34,15 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
3434
model.FromSystem = type.Namespace != null && type.Namespace.StartsWith("System");
3535

3636
IOptions typeOptions = this.options.Get(type, caller);
37-
3837
if (model.IsGeneric)
3938
{
4039
model.Name = model.OriginalName = type.Name.Split('`').First();
4140
model = new GenericModelTransferObject(model);
4241
}
43-
ModelTransferObject existingModel = this.transferObjects.Concat(this.environment.TransferObjects)
44-
.OfType<ModelTransferObject>()
45-
.FirstOrDefault(entry => entry.Equals(model));
42+
ModelTransferObject existingModel = this.transferObjects.OfType<ModelTransferObject>().FirstOrDefault(entry => entry.Equals(model));
4643
if (existingModel != null)
4744
{
45+
// TODO: Replace complete if with cached model reading after cloning of TransferModels is fixed
4846
if (model.IsGeneric)
4947
{
5048
GenericModelTransferObject genericModel = new(existingModel);
@@ -53,20 +51,14 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
5351
this.ApplyGenericTemplate(type, genericModel);
5452
this.transferObjects.Add(existingModel);
5553
}
56-
else
57-
{
58-
if (!this.transferObjects.Contains(existingModel))
59-
{
60-
existingModel = existingModel.Clone();
61-
this.transferObjects.Add(existingModel);
62-
}
63-
if (!this.options.Contains(existingModel))
64-
{
65-
this.options.Set(existingModel, typeOptions);
66-
}
67-
}
6854
return existingModel;
6955
}
56+
// TODO: Uncomment cached model reading after cloning of TransferModels is fixed
57+
// existingModel = this.environment.TransferObjects.OfType<ModelTransferObject>().FirstOrDefault(entry => entry.Equals(model));
58+
// if (existingModel != null)
59+
// {
60+
// return this.ReadExisting(existingModel, caller);
61+
// }
7062
this.options.Set(model, typeOptions);
7163
if (typeOptions.Ignore)
7264
{
@@ -102,6 +94,41 @@ public ModelTransferObject Read(Type type, IOptions caller = null)
10294
return model;
10395
}
10496

97+
private ModelTransferObject ReadExisting(ModelTransferObject model, IOptions caller)
98+
{
99+
IOptions typeOptions = this.options.Get(model.Type, caller);
100+
if (model.IsGeneric)
101+
{
102+
GenericModelTransferObject genericModel = new(model);
103+
model = genericModel;
104+
if (model.Type != null)
105+
{
106+
this.ApplyGenericTemplate(model.Type, genericModel);
107+
}
108+
}
109+
else
110+
{
111+
model = model.Clone();
112+
}
113+
if (!this.options.Contains(model))
114+
{
115+
this.options.Set(model, typeOptions);
116+
}
117+
this.transferObjects.Add(model);
118+
this.ReadExistingMembers(model, typeOptions);
119+
return model;
120+
}
121+
122+
private void ReadExistingMembers(ModelTransferObject model, IOptions caller)
123+
{
124+
model.Interfaces.OfType<ModelTransferObject>()
125+
.Concat(model.Generics.Select(x => x.Type))
126+
.Concat(model.Constants.Select(x => x.Type))
127+
.Concat(model.Fields.Select(x => x.Type))
128+
.Concat(model.Properties.Select(x => x.Type))
129+
.OfType<ModelTransferObject>().ForEach(x => this.ReadExisting(x, caller));
130+
}
131+
105132
private void ReadArray(Type type, ModelTransferObject model)
106133
{
107134
// Logger.Trace($"Reflection read array {type.Name} ({type.Namespace})");

Tests/Formatting/Generator/Output/WithoutPrefix/my-class-with-interface.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable */
22
// tslint:disable
33

4-
import { IInterface } from "./interface.interface";
4+
import { Interface } from "./interface";
55

6-
export class MyClassWithInterface implements IInterface {
6+
export class MyClassWithInterface implements Interface {
77
public constructor(init?: Partial<MyClassWithInterface>) {
88
Object.assign(this, init);
99
}

0 commit comments

Comments
 (0)