Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust whitespaces in the generated csharp files #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ interface CsharpObjectTypeExtensions : ExtensionsBase {
|using System.Collections.Generic;
|using System.Linq;
|using System.Text.Json.Serialization;
|using commercetools.Base.CustomAttributes;
|"""
|using commercetools.Base.CustomAttributes;"""
}

public fun ObjectType.objectClassName(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr
| {
| <${type.toProperties()}>
| }
|}
|
""".trimMargin().keepIndentation()
|}""".trimMargin().keepIndentation()


if(type.isADictionaryType())
Expand Down Expand Up @@ -61,7 +59,7 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr
var deprecationAttr = if(this.deprecationAnnotation() == "") "" else this.deprecationAnnotation()+"\n";

return """
|${deprecationAttr}${newKeyword}${typeName}$nullableChar $propName { get; set;}
|${deprecationAttr}${newKeyword}${typeName}$nullableChar $propName { get; set; }
""".trimMargin()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class CsharpObjectTypeRenderer constructor(override val vrapTypeProvider: VrapTy
| <${if(type.isADictionaryType()) "" else type.toProperties()}>
| <${type.renderConstructor(vrapType.simpleClassName)}>
| }
|}
|
""".trimMargin().keepIndentation()
|}""".trimMargin().keepIndentation()

if(type.isADictionaryType())
{
Expand Down Expand Up @@ -59,9 +57,7 @@ class CsharpObjectTypeRenderer constructor(override val vrapTypeProvider: VrapTy
| public partial class ${vrapType.simpleClassName} : Dictionary\<string, ${property.type.toVrapType().simpleName()}\>, I${vrapType.simpleClassName}
| {
| }
|}
|
"""
|}"""
}

private fun ObjectType.toProperties() : String = this.allProperties
Expand All @@ -75,7 +71,7 @@ class CsharpObjectTypeRenderer constructor(override val vrapTypeProvider: VrapTy
var deprecationAttr = if(this.deprecationAnnotation() == "") "" else this.deprecationAnnotation()+"\n";

return """
|${deprecationAttr}public ${typeName}$nullableChar $propName { get; set;}
|${deprecationAttr}public ${typeName}$nullableChar $propName { get; set; }
""".trimMargin()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
|using commercetools.Base.Client;
|using commercetools.Base.Serialization;
|${type.usings()}
|
|namespace ${cPackage}
|{
| public partial class ${type.toRequestName()} : ApiMethod\<${type.toRequestName()}\> {
|
| public partial class ${type.toRequestName()} : ApiMethod\<${type.toRequestName()}\>
| {
| <${type.properties()}>
|
| <${type.constructor()}>
Expand All @@ -68,9 +67,7 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
|
| <${type.formParamMethods()}>
| }
|}
""".trimMargin()
.keepIndentation()
|}""".trimMargin().keepIndentation()

val relativePath = cPackage
.replace(basePackagePrefix, "").replace(".", "/")
Expand Down Expand Up @@ -108,11 +105,12 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
""
}

return """|
|<$pathArgs>
|
|<$body>
""".trimMargin()
return if(body != "")
"""|<$pathArgs>
|
|<$body>""".trimMargin()
Comment on lines +109 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks strange

else
"""|<$pathArgs>""".trimMargin()
}

private fun Method.constructor(): String? {
Expand Down Expand Up @@ -154,7 +152,8 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
constructorAssignments.add("this.RequestUrl = $\"${requestUrl}\";")

return """
|public ${this.toRequestName()}(${constructorArguments.joinToString(separator = ", ")}) {
|public ${this.toRequestName()}(${constructorArguments.joinToString(separator = ", ")})
|{
| <${constructorAssignments.joinToString(separator = "\n")}>
|}
""".trimMargin().keepIndentation().escapeAll()
Expand Down Expand Up @@ -182,7 +181,8 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
private fun Method.queryParamsGetters() : String = this.queryParameters
.filter { it.getAnnotation(PLACEHOLDER_PARAM_ANNOTATION, true) == null }
.map { """
|public List<string> Get${it.fieldName().firstUpperCase()}() {
|public List<string> Get${it.fieldName().firstUpperCase()}()
|{
| return this.GetQueryParam("${it.name}");
|}
""".trimMargin().escapeAll() }
Expand All @@ -191,7 +191,8 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
private fun Method.queryParamsSetters() : String = this.queryParameters
.filter { it.getAnnotation(PLACEHOLDER_PARAM_ANNOTATION, true) == null }
.map { """
|public ${this.toRequestName()} With${it.fieldName().firstUpperCase()}(${it.witherType()} ${it.fieldName()}){
|public ${this.toRequestName()} With${it.fieldName().firstUpperCase()}(${it.witherType()} ${it.fieldName()})
|{
| return this.AddQueryParam("${it.name}", ${it.fieldNameAsString(it.witherType())});
|}
""".trimMargin().escapeAll() }
Expand All @@ -210,8 +211,8 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
"""
|public async Task\<${this.csharpReturnType(vrapTypeProvider)}\> ExecuteAsync()
|{
| var requestMessage = Build();
| return await ApiHttpClient.ExecuteAsync\<${this.csharpReturnType(vrapTypeProvider)}\>(requestMessage);
| var requestMessage = Build();
| return await ApiHttpClient.ExecuteAsync\<${this.csharpReturnType(vrapTypeProvider)}\>(requestMessage);
|}
""".trimMargin()

Expand All @@ -235,12 +236,12 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
|
|public override HttpRequestMessage Build()
|{
| var request = base.Build();
| if ($bodyName != null && $bodyName.Length \> 0)
| {
| request.Content = new StreamContent($bodyName);
| }
| return request;
| var request = base.Build();
| if ($bodyName != null && $bodyName.Length \> 0)
| {
| request.Content = new StreamContent($bodyName);
| }
| return request;
|}
""".trimMargin()
}
Expand All @@ -249,10 +250,9 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
|
|public override HttpRequestMessage Build()
|{
| var request = base.Build();
|
| request.Content = new FormUrlEncodedContent(_formParams);
| return request;
| var request = base.Build();
| request.Content = new FormUrlEncodedContent(_formParams);
| return request;
|}
""".trimMargin()
else
Expand All @@ -261,16 +261,16 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
|
|public override HttpRequestMessage Build()
|{
| var request = base.Build();
| if (SerializerService != null)
| {
| var body = this.SerializerService.Serialize(${bodyName});
| if(!string.IsNullOrEmpty(body))
| {
| request.Content = new StringContent(body, Encoding.UTF8, "application/json");
| }
| }
| return request;
| var request = base.Build();
| if (SerializerService != null)
| {
| var body = this.SerializerService.Serialize(${bodyName});
| if(!string.IsNullOrEmpty(body))
| {
| request.Content = new StringContent(body, Encoding.UTF8, "application/json");
| }
| }
| return request;
|}
""".trimMargin()
}
Expand All @@ -281,12 +281,14 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
private fun Method.formParamMethods() : String =
if (this.bodies != null && !this.bodies.isEmpty() && this.bodies[0].contentMediaType.`is`(MediaType.FORM_DATA)) {
"""
public ${this.toRequestName()} AddFormParam<TValue>(string key, TValue value) {
public ${this.toRequestName()} AddFormParam<TValue>(string key, TValue value)
{
this._formParams.Add(new KeyValuePair<string, string>(key, value.ToString()));
return this;
}

public ${this.toRequestName()} WithFormParam<TValue>(string key, TValue value) {
public ${this.toRequestName()} WithFormParam<TValue>(string key, TValue value)
{
return WithoutFormParam(key).AddFormParam(key, value);
}

Expand All @@ -295,7 +297,8 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
* @param key form parameter name
* @return T
*/
public ${this.toRequestName()} WithoutFormParam(string key) {
public ${this.toRequestName()} WithoutFormParam(string key)
{
this._formParams = this._formParams.FindAll(pair => !pair.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
return this;
}
Expand All @@ -305,28 +308,34 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
* @param formParams list of form parameters
* @return T
*/
public ${this.toRequestName()} WithFormParams(List<KeyValuePair<string, string>> formParams) {
public ${this.toRequestName()} WithFormParams(List<KeyValuePair<string, string>> formParams)
{
this._formParams = formParams;
return this;
}

public List<KeyValuePair<string, string>> GetFormParams() {
public List<KeyValuePair<string, string>> GetFormParams()
{
return this._formParams.ToList();
}

public List<string> GetFormParam(string key) {
public List<string> GetFormParam(string key)
{
return this._formParams.FindAll(pair => pair.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)).Select(pair => pair.Value).ToList();
}

public List<string> GetFormParamUriStrings() {
public List<string> GetFormParamUriStrings()
{
return this._formParams.Select(ToUriString).ToList();
}

public string GetFormParamUriString() {
public string GetFormParamUriString()
{
return string.Join('&', this._formParams.Select(ToUriString));
}

private static string ToUriString(KeyValuePair<string, string> entry) {
private static string ToUriString(KeyValuePair<string, string> entry)
{
return entry.Key + "=" + WebUtility.UrlEncode(entry.Value);
}

Expand Down Expand Up @@ -370,7 +379,8 @@ class CsharpHttpRequestRenderer constructor(override val vrapTypeProvider: VrapT
val parameters = "string " + StringCaseFormat.LOWER_CAMEL_CASE.apply(placeholder.value) + ", ${it.witherType()} " + paramName.value

return """
|public ${this.toRequestName()} With$methodName($parameters){
|public ${this.toRequestName()} With$methodName($parameters)
|{
| return this.AddQueryParam($value, ${paramName.value});
|}
""".trimMargin().escapeAll()
Expand Down