Skip to content

Commit 6bf7342

Browse files
committed
Angular
- custom http client use parameters removed - generic return type with different types fixed - rename parameter names when name is a reserved keyword (only for new and function) ASP.NET - ignore types on action templates/routes Core - differ generic and not generic types like Test<T> and Test
1 parent e005834 commit 6bf7342

File tree

12 files changed

+60
-66
lines changed

12 files changed

+60
-66
lines changed

Angular/Commands/AngularHttpClientMethodOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public class AngularHttpClientMethodOptions
66
public bool NotGeneric { get; set; }
77
public bool ReturnGeneric { get; set; } = true;
88
public bool ParameterGeneric { get; set; }
9-
public bool UseParameters { get; set; }
109
public bool ReturnsAny { get; set; }
1110
}
1211
}

Angular/Fluent/AngularHttpClientSyntax.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@ public IAngularHttpClientMethodSyntax ParameterGeneric()
3535
return this;
3636
}
3737

38-
public IAngularHttpClientMethodSyntax UseParameters()
39-
{
40-
this.options.UseParameters = true;
41-
return this;
42-
}
43-
4438
public IAngularHttpClientMethodSyntax ReturnsAny()
4539
{
4640
this.options.ReturnsAny = true;
4741
return this;
4842
}
4943
}
50-
}
44+
}

Angular/Fluent/IAngularHttpClientMethodSyntax.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public interface IAngularHttpClientMethodSyntax
66
IAngularHttpClientMethodSyntax NotGeneric();
77
IAngularHttpClientMethodSyntax ReturnGeneric();
88
IAngularHttpClientMethodSyntax ParameterGeneric();
9-
IAngularHttpClientMethodSyntax UseParameters();
109
IAngularHttpClientMethodSyntax ReturnsAny();
1110
}
12-
}
11+
}

Angular/Writers/AngularServiceWriter.cs

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public virtual void Write(AngularWriteConfiguration configuration, List<ITransfe
100100
this.MapType(controllerLanguage, configurationLanguage, action.ReturnType);
101101
}
102102
TypeTemplate returnType = action.ReturnType.ToTemplate();
103-
TypeTemplate returnModelType = isEnumerable ? action.ReturnType.Generics.First().Type.ToTemplate() : returnType;
104-
ModelTransferObject returnModel = transferObjects.OfType<ModelTransferObject>().FirstOrDefault(x => x.Name == returnModelType.Name && x.Namespace == returnModelType.Namespace);
103+
TypeTransferObject returnModelType = isEnumerable ? action.ReturnType.Generics.First().Type : action.ReturnType;
104+
ModelTransferObject returnModel = returnModelType as ModelTransferObject ?? transferObjects.OfType<ModelTransferObject>().FirstOrDefault(x => x.Equals(returnModelType));
105105
this.AddUsing(action.ReturnType, classTemplate, configuration, relativeModelPath);
106106
MethodTemplate methodTemplate = classTemplate.AddMethod(action.Name, Code.Generic("Observable", returnType))
107107
.FormatName(configuration);
@@ -114,7 +114,8 @@ public virtual void Write(AngularWriteConfiguration configuration, List<ITransfe
114114
this.MapType(controllerLanguage, configurationLanguage, parameter.Type);
115115
}
116116
this.AddUsing(parameter.Type, classTemplate, configuration, relativeModelPath);
117-
ParameterTemplate parameterTemplate = methodTemplate.AddParameter(parameter.Type.ToTemplate(), parameter.Name).FormatName(configuration);
117+
string parameterName = this.GetAllowedName(configuration.Language, parameter.Name);
118+
ParameterTemplate parameterTemplate = methodTemplate.AddParameter(parameter.Type.ToTemplate(), parameterName).FormatName(configuration);
118119
if (parameter.IsOptional)
119120
{
120121
parameterTemplate.Optional();
@@ -211,21 +212,11 @@ public virtual void Write(AngularWriteConfiguration configuration, List<ITransfe
211212
isUrlApiVersion = true;
212213
uri = uri.Replace(apiVersionKey, actionVersion);
213214
}
214-
if (!actionTypeOptions[action.Type]?.UseParameters ?? false)
215+
foreach (HttpServiceActionParameterTransferObject parameter in inlineParameters)
215216
{
216-
foreach (HttpServiceActionParameterTransferObject parameter in inlineParameters)
217-
{
218-
string[] chunks = uri.Split(new[] { $"{{{parameter.Name}}}" }, StringSplitOptions.RemoveEmptyEntries);
219-
parameterUrl = parameterUrl.Append(Code.String(chunks[0])).Append(Code.Local(parameter.Name));
220-
uri = chunks.Length == 1 ? string.Empty : chunks[1];
221-
}
222-
}
223-
else
224-
{
225-
foreach (HttpServiceActionParameterTransferObject parameter in inlineParameters)
226-
{
227-
uri = uri.Replace($"{{{parameter.Name}}}", string.Empty).Replace($"{{{parameter.Name}}}", string.Empty);
228-
}
217+
string[] chunks = uri.Split(new[] { $"{{{parameter.Name}}}" }, StringSplitOptions.RemoveEmptyEntries);
218+
parameterUrl = parameterUrl.Append(Code.String(chunks[0])).Append(Code.Local(mapping[parameter]));
219+
uri = chunks.Length == 1 ? string.Empty : chunks[1];
229220
}
230221
if (!string.IsNullOrEmpty(uri))
231222
{
@@ -237,55 +228,48 @@ public virtual void Write(AngularWriteConfiguration configuration, List<ITransfe
237228
isFirst = false;
238229
parameterUrl = parameterUrl.Append(Code.String($"?api-version={actionVersion}"));
239230
}
240-
if (!actionTypeOptions[action.Type]?.UseParameters ?? false)
231+
foreach (HttpServiceActionParameterTransferObject parameter in urlDirectParameters)
241232
{
242-
foreach (HttpServiceActionParameterTransferObject parameter in urlDirectParameters)
233+
if (isFirst)
243234
{
244-
if (isFirst)
245-
{
246-
isFirst = false;
247-
parameterUrl = parameterUrl.Append(Code.String("?")).Append(Code.Local(parameter.Name));
248-
}
249-
else
250-
{
251-
parameterUrl = parameterUrl.Append(Code.String("&")).Append(Code.Local(parameter.Name));
252-
}
235+
isFirst = false;
236+
parameterUrl = parameterUrl.Append(Code.String("?")).Append(Code.Local(parameter.Name));
237+
}
238+
else
239+
{
240+
parameterUrl = parameterUrl.Append(Code.String("&")).Append(Code.Local(parameter.Name));
241+
}
242+
}
243+
foreach (HttpServiceActionParameterTransferObject parameter in urlParameters)
244+
{
245+
parameterUrl = parameterUrl.Append(isFirst ? Code.String($"?{parameter.Name}=") : Code.String($"&{parameter.Name}="));
246+
if (parameter.FromQuery && parameter.Type.Name == "Array")
247+
{
248+
appendConvertAnyMethod = true;
249+
parameterUrl = parameterUrl.Append(Code.This().Method("convertAny", Code.Local(mapping[parameter].Name + "Join")));
253250
}
254-
foreach (HttpServiceActionParameterTransferObject parameter in urlParameters)
251+
else
255252
{
256-
string name = mapping[parameter].Name;
257-
parameterUrl = parameterUrl.Append(isFirst ? Code.String($"?{parameter.Name}=") : Code.String($"&{parameter.Name}="));
258-
if (parameter.FromQuery && parameter.Type.Name == "Array")
253+
if (parameter.Type.IgnoreNullable().Name == "Date")
259254
{
260-
appendConvertAnyMethod = true;
261-
parameterUrl = parameterUrl.Append(Code.This().Method("convertAny", Code.Local(name + "Join")));
255+
appendConvertFromDateMethod = true;
256+
parameterUrl = parameterUrl.Append(Code.This().Method("convertFromDate", Code.Local(mapping[parameter])));
262257
}
263258
else
264259
{
265-
if (parameter.Type.IgnoreNullable().Name == "Date")
266-
{
267-
appendConvertFromDateMethod = true;
268-
parameterUrl = parameterUrl.Append(Code.This().Method("convertFromDate", Code.Local(name)));
269-
}
270-
else
271-
{
272-
appendConvertAnyMethod = true;
273-
parameterUrl = parameterUrl.Append(Code.This().Method("convertAny", Code.Local(name)));
274-
}
260+
appendConvertAnyMethod = true;
261+
parameterUrl = parameterUrl.Append(Code.This().Method("convertAny", Code.Local(mapping[parameter])));
275262
}
276-
isFirst = false;
277263
}
264+
isFirst = false;
278265
}
279266
ChainedCodeFragment executeAction = Code.This().Field(httpField);
280267
List<ICodeFragment> parameters = new() { parameterUrl };
281268
if (action.Parameters.Any(x => x.FromBody))
282269
{
283270
parameters.Add(Code.Local(action.Parameters.Single(x => x.FromBody).Name));
284271
}
285-
if (actionTypeOptions[action.Type]?.UseParameters ?? false)
286-
{
287-
parameters.AddRange(inlineParameters.Concat(urlDirectParameters).Concat(urlParameters).Select(parameter => Code.Local(parameter.Name)));
288-
}
272+
parameters.AddRange(inlineParameters.Concat(urlDirectParameters).Concat(urlParameters).Select(parameter => Code.Local(parameter.Name)));
289273
if (actionTypeOptions[action.Type]?.HasHttpOptions ?? false)
290274
{
291275
parameters.Add(Code.Local("httpOptions"));
@@ -649,5 +633,10 @@ private void AppendConvertToDateMethod(ClassTemplate classTemplate)
649633
)
650634
));
651635
}
636+
637+
private string GetAllowedName(ILanguage language, string name)
638+
{
639+
return language.ReservedKeywords.ContainsKey(name) ? language.ReservedKeywords[name] : name;
640+
}
652641
}
653642
}

AspDotNet/Readers/AspDotNetControllerReader.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Text.RegularExpressions;
67
using KY.Core;
78
using KY.Generator.AspDotNet.Configurations;
89
using KY.Generator.Extensions;
@@ -122,10 +123,14 @@ public virtual void Read(AspDotNetReadConfiguration configuration, List<ITransfe
122123
}
123124
foreach (KeyValuePair<HttpServiceActionTypeTransferObject, string> actionType in actionTypes)
124125
{
125-
HttpServiceActionTransferObject action = new HttpServiceActionTransferObject();
126+
HttpServiceActionTransferObject action = new();
126127
action.Name = actionTypes.Count == 1 ? method.Name : $"{actionType.Key}{method.Name.FirstCharToUpper()}";
127128
action.ReturnType = this.modelReader.Read(returnType, transferObjects, configuration.Controller);
128129
action.Route = actionType.Value ?? fallbackRoute;
130+
if (action.Route?.Contains(":") ?? false)
131+
{
132+
action.Route = Regex.Replace(action.Route, "({[^:]*)(:[^}]+)(})", "$1$3") ;
133+
}
129134
action.Type = actionType.Key;
130135
action.Version = methodVersions.LastOrDefault();
131136
action.FixCasingWithMapping = returnEntryType.GetCustomAttribute<GenerateFixCasingWithMappingAttribute>() != null || typeAttributes.Any(x => x is GenerateFixCasingWithMappingAttribute);
@@ -138,7 +143,7 @@ public virtual void Read(AspDotNetReadConfiguration configuration, List<ITransfe
138143
continue;
139144
}
140145
string fullRoute = $"{controller.Route}/{action.Route}";
141-
HttpServiceActionParameterTransferObject actionParameter = new HttpServiceActionParameterTransferObject();
146+
HttpServiceActionParameterTransferObject actionParameter = new();
142147
actionParameter.Name = parameter.Name;
143148
actionParameter.Type = this.modelReader.Read(parameter.ParameterType, transferObjects, configuration.Controller);
144149
actionParameter.FromBody = this.IsFromBodyParameter(parameter, action.Type);

Core/Languages/BaseLanguage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public abstract class BaseLanguage : Codeable, IFormattableLanguage, IMappableLa
2121
public LanguageFormatting Formatting { get; }
2222
public abstract string Name { get; }
2323
public abstract bool ImportFromSystem { get; }
24+
public Dictionary<string, string> ReservedKeywords { get; } = new();
2425
public virtual string NamespaceKeyword => "namespace";
2526
public virtual string ClassScope => "public";
2627
public virtual string PartialKeyword => "partial";
@@ -39,6 +40,8 @@ protected BaseLanguage()
3940
this.Formatting.EndBlock = "}";
4041
this.Formatting.StartBlockInNewLine = true;
4142

43+
this.ReservedKeywords.Add("new", "newValue");
44+
4245
this.progressedChainedCodeFragments = new List<ChainedCodeFragment>();
4346
this.lastFragments = new List<ICodeFragment>();
4447

Core/Languages/EmptyLanguage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class EmptyLanguage : IMappableLanguage
99
{
1010
public virtual string Name => "Empty";
1111
public bool ImportFromSystem => true;
12+
public Dictionary<string, string> ReservedKeywords { get; } = new();
1213
public object Key { get; } = new object();
1314

1415
public virtual void Write(FileTemplate file, IOutput output)
@@ -26,4 +27,4 @@ public virtual void Write<T>(IEnumerable<T> code, IOutputCache output) where T :
2627
throw new NotImplementedException($"The method {nameof(Write)} for type {code.GetType().Name} is not implemented in {this.Name}.");
2728
}
2829
}
29-
}
30+
}

Core/Languages/ILanguage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ public interface ILanguage
88
{
99
string Name { get; }
1010
bool ImportFromSystem { get; }
11+
Dictionary<string, string> ReservedKeywords { get; }
1112

1213
void Write(FileTemplate file, IOutput output);
1314
void Write(ICodeFragment code, IOutputCache output);
1415
void Write<T>(IEnumerable<T> code, IOutputCache output) where T : ICodeFragment;
1516
}
16-
}
17+
}

Core/Transfer/ModelTransferObject.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class ModelTransferObject : TypeTransferObject
1010
public virtual bool IsEnum { get; set; }
1111
public virtual bool IsInterface { get; set; }
1212
public virtual bool IsAbstract { get; set; }
13-
public virtual bool IsGeneric { get; set; }
1413
public virtual Dictionary<string, int> EnumValues { get; set; }
1514
public virtual ModelTransferObject BasedOn { get; set; }
1615
public virtual ILanguage Language { get; set; }

Core/Transfer/TypeTransferObject.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class TypeTransferObject : ITransferObject
1313
public virtual string Namespace { get; set; }
1414
public virtual bool FromSystem { get; set; }
1515
public virtual bool IsNullable { get; set; }
16+
public virtual bool IsGeneric { get; set; }
1617
public virtual List<GenericAliasTransferObject> Generics { get; }
1718
public virtual TypeTransferObject Original { get; set; }
1819
public virtual ICodeFragment Default { get; set; }
@@ -30,13 +31,14 @@ public TypeTransferObject(TypeTransferObject type)
3031
this.Namespace = type.Namespace;
3132
this.FromSystem = type.FromSystem;
3233
this.IsNullable = type.IsNullable;
34+
this.IsGeneric = type.IsGeneric;
3335
this.Generics = type.Generics.ToList();
3436
this.HasUsing = type.HasUsing;
3537
}
3638

3739
public bool Equals(TypeTransferObject type)
3840
{
39-
return (this.Name == type.Name || this.OriginalName == type.Name || this.Name == type.OriginalName) && this.Namespace == type.Namespace;
41+
return (this.Name == type.Name || this.OriginalName == type.Name || this.Name == type.OriginalName) && this.Namespace == type.Namespace && this.IsGeneric == type.IsGeneric;
4042
}
4143

4244
public TypeTransferObject Clone()

0 commit comments

Comments
 (0)