Skip to content

Commit be655eb

Browse files
committed
Annotations
- custom Produce* attributes moved back to legacy namespace to differ between .NET original types Core/Reflection - generic type handling improved Sqlite - temporary ignore not mappable columns
1 parent d464bb4 commit be655eb

File tree

10 files changed

+44
-38
lines changed

10 files changed

+44
-38
lines changed

Annotations/Legacy/ProducesAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace KY.Generator
3+
namespace KY.Generator.Legacy
44
{
55
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
66
public class ProducesAttribute : Attribute

Annotations/Legacy/ProducesResponseTypeAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace KY.Generator
3+
namespace KY.Generator.Legacy
44
{
55
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
66
public class ProducesResponseTypeAttribute : Attribute

AspDotNet/Readers/AspDotNetControllerReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ private Dictionary<HttpServiceActionTypeTransferObject, string> GetActionTypes(L
192192
case "RouteAttribute":
193193
case "ProducesAttribute":
194194
case "ProducesResponseTypeAttribute":
195-
case "ApiVersion":
196-
case "EnableCors":
195+
case "ApiVersionAttribute":
196+
case "EnableCorsAttribute":
197197
case nameof(GenerateIgnoreGenericAttribute):
198198
// Ignore these attributes
199199
return null;

Core/Transfer/Extensions/TypeTransferObjectExtension.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public static void FromType(this TypeTransferObject transferObject, Type type)
4444
Type argument = type.GetElementType() ?? typeof(object);
4545
transferObject.Generics.Add(new GenericAliasTransferObject { Type = argument.ToTransferObject() });
4646
}
47+
else if (type.ContainsGenericParameters)
48+
{
49+
transferObject.Name = type.Name;
50+
transferObject.HasUsing = false;
51+
}
4752
else
4853
{
4954
transferObject.Name = type.Name;

Core/Transfer/ModelTransferObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ public ModelTransferObject()
3232
this.Methods = new List<MethodTransferObject>();
3333
}
3434
}
35-
}
35+
}

Core/Transfer/TypeTransferObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class TypeTransferObject : ITransferObject
1616
public TypeTransferObject Original { get; set; }
1717
public ICodeFragment Default { get; set; }
1818
public string FullName => $"{this.Namespace}.{this.Name}";
19+
public bool HasUsing { get; set; } = true;
1920

2021
public TypeTransferObject()
2122
{
@@ -29,6 +30,7 @@ public TypeTransferObject(TypeTransferObject type)
2930
this.FromSystem = type.FromSystem;
3031
this.IsNullable = type.IsNullable;
3132
this.Generics = type.Generics.ToList();
33+
this.HasUsing = type.HasUsing;
3234
}
3335

3436
public bool Equals(TypeTransferObject type)

Core/Transfer/Writers/TransferWriter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ protected virtual void AddConstants(ModelTransferObject model, ClassTemplate cla
3131
{
3232
fieldTemplate.DefaultValue = Code.Number((int)constant.Default);
3333
}
34-
else if (type == typeof(long))
34+
else if (type == typeof(long))
3535
{
3636
fieldTemplate.DefaultValue = Code.Number((long)constant.Default);
3737
}
38-
else if (type == typeof(short))
38+
else if (type == typeof(short))
3939
{
4040
fieldTemplate.DefaultValue = Code.Number((short)constant.Default);
4141
}
42-
else if (type == typeof(uint))
42+
else if (type == typeof(uint))
4343
{
4444
fieldTemplate.DefaultValue = Code.Number((uint)constant.Default);
4545
}
46-
else if (type == typeof(ulong))
46+
else if (type == typeof(ulong))
4747
{
4848
fieldTemplate.DefaultValue = Code.Number((ulong)constant.Default);
4949
}
50-
else if (type == typeof(ushort))
50+
else if (type == typeof(ushort))
5151
{
5252
fieldTemplate.DefaultValue = Code.Number((ushort)constant.Default);
5353
}
@@ -152,7 +152,7 @@ protected virtual void AddUsing(TypeTransferObject type, ClassTemplate classTemp
152152
{
153153
return;
154154
}
155-
if ((!type.FromSystem || type.FromSystem && configuration.Language.ImportFromSystem) && !string.IsNullOrEmpty(type.Namespace) && classTemplate.Namespace.Name != type.Namespace)
155+
if ((!type.FromSystem || type.FromSystem && configuration.Language.ImportFromSystem) && type.HasUsing && !string.IsNullOrEmpty(type.Namespace) && classTemplate.Namespace.Name != type.Namespace)
156156
{
157157
string fileName = Formatter.FormatFile(type.Name, configuration, true);
158158
classTemplate.AddUsing(type.Namespace, type.Name, $"{relativeModelPath.Replace("\\", "/").TrimEnd('/')}/{fileName}");

Core/Writers/DefaultTypeWriter.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ public DefaultTypeWriter(string nullableFormatter = "{0}?")
1414

1515
public void Write(TypeTemplate template, IOutputCache output)
1616
{
17-
if (template.IsNullable)
18-
{
19-
output.Add(string.Format(this.NullableFormatter, template.Name));
20-
}
21-
else
22-
{
2317
output.Add(template.Name);
24-
}
2518
}
2619

2720
public void Write(GenericTypeTemplate template, IOutputCache output)
@@ -32,4 +25,4 @@ public void Write(GenericTypeTemplate template, IOutputCache output)
3225
.Add(">");
3326
}
3427
}
35-
}
28+
}

Reflection/Readers/ReflectionModelReader.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public ModelTransferObject Read(Type type, List<ITransferObject> transferObjects
4040
transferObjects.Add(model);
4141
this.ReadEnum(type, model);
4242
}
43+
else if (type.ContainsGenericParameters)
44+
{
45+
model.HasUsing = false;
46+
}
4347
else if (!isFromSystem)
4448
{
4549
transferObjects.Add(model);
@@ -97,20 +101,28 @@ private void ReadClass(Type type, ModelTransferObject model, List<ITransferObjec
97101
{
98102
model.BasedOn = this.Read(type.BaseType, transferObjects);
99103
}
100-
Dictionary<Type, string> genericMapping = new Dictionary<Type, string>();
101104
if (type.IsGenericType)
102105
{
106+
Type genericType = type.GetGenericTypeDefinition();
103107
model.IsGeneric = true;
104108
model.Generics.Clear();
105-
foreach (Type argument in type.GenericTypeArguments)
109+
if (genericType is TypeInfo typeInfo)
110+
{
111+
for (int index = 0; index < typeInfo.GenericTypeParameters.Length; index++)
112+
{
113+
string alias = typeInfo.GenericTypeParameters[index].Name;
114+
Type argument = type.GenericTypeArguments[index];
115+
model.Generics.Add(new GenericAliasTransferObject
116+
{
117+
Alias = alias,
118+
Type = this.Read(argument, transferObjects)
119+
});
120+
}
121+
type = genericType;
122+
}
123+
else
106124
{
107-
string alias = genericMapping.Count > 1 ? $"T{genericMapping.Count}" : "T";
108-
genericMapping.Add(argument, alias);
109-
model.Generics.Add(new GenericAliasTransferObject
110-
{
111-
Alias = alias,
112-
Type = this.Read(argument, transferObjects)
113-
});
125+
throw new InvalidOperationException("Internal Error l2sl3: Type is not a TypeInfo");
114126
}
115127
}
116128

@@ -135,9 +147,7 @@ private void ReadClass(Type type, ModelTransferObject model, List<ITransferObjec
135147
FieldTransferObject fieldTransferObject = new FieldTransferObject
136148
{
137149
Name = field.Name,
138-
Type = genericMapping.ContainsKey(field.FieldType)
139-
? new TypeTransferObject { Name = genericMapping[field.FieldType] }
140-
: this.Read(field.FieldType, transferObjects),
150+
Type = this.Read(field.FieldType, transferObjects),
141151
Default = field.GetValue(null)
142152
};
143153
model.Constants.Add(fieldTransferObject);
@@ -154,9 +164,7 @@ private void ReadClass(Type type, ModelTransferObject model, List<ITransferObjec
154164
FieldTransferObject fieldTransferObject = new FieldTransferObject
155165
{
156166
Name = field.Name,
157-
Type = genericMapping.ContainsKey(field.FieldType)
158-
? new TypeTransferObject { Name = genericMapping[field.FieldType] }
159-
: this.Read(field.FieldType, transferObjects)
167+
Type = this.Read(field.FieldType, transferObjects)
160168
};
161169
model.Fields.Add(fieldTransferObject);
162170
}
@@ -171,9 +179,7 @@ private void ReadClass(Type type, ModelTransferObject model, List<ITransferObjec
171179
PropertyTransferObject propertyTransferObject = new PropertyTransferObject
172180
{
173181
Name = property.Name,
174-
Type = genericMapping.ContainsKey(property.PropertyType)
175-
? new TypeTransferObject { Name = genericMapping[property.PropertyType] }
176-
: this.Read(property.PropertyType, transferObjects),
182+
Type = this.Read(property.PropertyType, transferObjects),
177183
Attributes = property.GetCustomAttributes().ToTransferObjects().ToList()
178184
};
179185
model.Properties.Add(propertyTransferObject);

Sqlite/Writers/SqliteRepositoryWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private void WriteDropTable(ClassTemplate classTemplate, FieldTemplate connectio
118118

119119
private void WriteGet(ClassTemplate classTemplate, FieldTemplate connectionField, SqliteModelTransferObject model, SqliteWriteRepositoryCommandParameters parameters)
120120
{
121-
List<SqlitePropertyTransferObject> columns = model.Properties.ToList();
121+
List<SqlitePropertyTransferObject> columns = model.Properties.Where(x => x.Type.Original != null).ToList();
122122
if (columns.Count == 0)
123123
{
124124
return;

0 commit comments

Comments
 (0)