com.reprezen.genflow.rapidml.csharp.generators.RepreZenClassGenerator Maven / Gradle / Ivy
package com.reprezen.genflow.rapidml.csharp.generators;
import com.google.common.base.Objects;
import com.reprezen.genflow.api.template.IGenTemplateContext;
import com.reprezen.genflow.rapidml.csharp.Config;
import com.reprezen.genflow.rapidml.csharp.helpers.FileHelper;
import com.reprezen.genflow.rapidml.csharp.helpers.SpecialFile;
import java.nio.file.Path;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
@SuppressWarnings("all")
public class RepreZenClassGenerator {
private final Config config;
@Extension
private final FileHelper fileHelper;
public RepreZenClassGenerator(final IGenTemplateContext context, final Config config) {
this.config = config;
this.fileHelper = FileHelper.of(SpecialFile.REPREZEN, context, config);
}
public Path generate() {
return this.fileHelper.writeFile(this.getContent(), this.fileHelper.getCsharpFileName("RepreZen"));
}
public CharSequence getContent() {
StringConcatenation _builder = new StringConcatenation();
_builder.newLine();
CharSequence _configClass = this.getConfigClass();
_builder.append(_configClass);
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("[AttributeUsage(AttributeTargets.Class, Inherited = false)]");
_builder.newLine();
_builder.append("public class DeserializeFromAttribute : Attribute");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("private Type target;");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public DeserializeFromAttribute(Type target)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.target = target;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Type Target { get { return target; } }");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("public class RepreZenContractResolver : IContractResolver");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("private static Dictionary targetTypeMap = new Dictionary();");
_builder.newLine();
_builder.append(" ");
_builder.append("private static Type deserializeFrom = typeof(DeserializeFromAttribute);");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public static void ScanForTargets()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("targetTypeMap.Clear();");
_builder.newLine();
_builder.append(" ");
_builder.append("var types = AppDomain.CurrentDomain.GetAssemblies()");
_builder.newLine();
_builder.append(" ");
_builder.append(".SelectMany(a => a.GetTypes())");
_builder.newLine();
_builder.append(" ");
_builder.append(".Where(t => t.GetCustomAttributes(deserializeFrom, false).Length > 0);");
_builder.newLine();
_builder.append(" ");
_builder.append("foreach (Type type in types)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("DeserializeFromAttribute attr = type.GetCustomAttributes(deserializeFrom, false)");
_builder.newLine();
_builder.append(" ");
_builder.append(".First() as DeserializeFromAttribute;");
_builder.newLine();
_builder.append(" ");
_builder.append("targetTypeMap[attr.Target] = type;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("private IContractResolver chainedResolver { get; set; }");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public RepreZenContractResolver(IContractResolver chainedResolver)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.chainedResolver = chainedResolver;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public JsonContract ResolveContract(Type type)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("if (targetTypeMap.ContainsKey(type))");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("type = targetTypeMap[type];");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("return chainedResolver.ResolveContract(type);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
{
Config.Framework _framework = this.config.getFramework();
boolean _equals = Objects.equal(_framework, Config.Framework.ASP_DOTNET_CORE_2_0_MVC);
if (_equals) {
CharSequence _responderClasses = this.getResponderClasses();
_builder.append(_responderClasses);
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
CharSequence _resultClasses = this.getResultClasses();
_builder.append(_resultClasses, "\t");
_builder.newLineIfNotEmpty();
}
}
_builder.newLine();
return _builder;
}
public CharSequence getConfigClass() {
CharSequence _switchResult = null;
Config.Framework _framework = this.config.getFramework();
if (_framework != null) {
switch (_framework) {
case ASP_DOTNET_CORE_2_0_MVC:
StringConcatenation _builder = new StringConcatenation();
_builder.append("public static class RepreZenConfiguration");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("public static IMvcBuilder ConfigureRepreZen(this IMvcBuilder builder)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("RepreZenContractResolver.ScanForTargets();");
_builder.newLine();
_builder.append(" ");
_builder.append("builder.AddJsonOptions(opt =>");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("var resolver = opt.SerializerSettings.ContractResolver;");
_builder.newLine();
_builder.append(" ");
_builder.append("if (resolver != null)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("var repreZenResolver = new RepreZenContractResolver(resolver);");
_builder.newLine();
_builder.append(" ");
_builder.append("opt.SerializerSettings.ContractResolver = repreZenResolver;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("});");
_builder.newLine();
_builder.append(" ");
_builder.append("return builder;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_switchResult = _builder;
break;
case ASP_DOTNET_WEBAPI_2:
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("public class RepreZenConfiguration ");
_builder_1.newLine();
_builder_1.append("{");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("public static void configure(HttpConfiguration config)");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("{");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("config.Formatters.Remove(config.Formatters.XmlFormatter);");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("var currentResolver = config.Formatters.JsonFormatter.SerializerSettings.ContractResolver;");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("RepreZenContractResolver.ScanForTargets();");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("var repreZenResolver = new RepreZenContractResolver(currentResolver);");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings {");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("ContractResolver = repreZenResolver");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("};");
_builder_1.newLine();
_builder_1.append("\t ");
_builder_1.append("config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_switchResult = _builder_1;
break;
default:
break;
}
}
return _switchResult;
}
public CharSequence getResponderClasses() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("public interface IResponder");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("bool IsNormal();");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("void RespondWith(IActionResult result);");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("public class Responder : IResponder");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("private Result result;");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public void RespondWith(NormalType value)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.result = new Result(value);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public void RespondWith(IActionResult result)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.result = new Result(result);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public bool IsNormal()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return result != null && result.IsNormal();");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Result GetResult()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return result;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("public class VoidResponder : IResponder");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("private VoidResult result = new VoidResult();");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public void RespondWith(IActionResult result)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.result = new VoidResult(result);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public bool IsNormal()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return result.IsNormal();");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public VoidResult GetResult()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return result;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder;
}
public CharSequence getResultClasses() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("public class Result : IActionResult");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("private IActionResult wrapped;");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Result(NormalType value) {");
_builder.newLine();
_builder.append(" ");
_builder.append("this.wrapped = new OkObjectResult(value);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Result(IActionResult wrapped)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.wrapped = wrapped;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public bool IsNormal()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return wrapped is OkObjectResult && ((OkObjectResult)wrapped).Value is NormalType;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public NormalType GetValue()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return (NormalType) ((OkObjectResult)wrapped).Value;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Task ExecuteResultAsync(ActionContext context)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return wrapped.ExecuteResultAsync(context);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("public class VoidResult : IActionResult");
_builder.newLine();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("private IActionResult wrapped;");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public VoidResult()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.wrapped = new OkResult();");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.newLine();
_builder.append(" ");
_builder.append("public VoidResult(IActionResult wrapped)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("this.wrapped = wrapped;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public bool IsNormal()");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return wrapped is OkResult;");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append(" ");
_builder.append("public Task ExecuteResultAsync(ActionContext context)");
_builder.newLine();
_builder.append(" ");
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("return wrapped.ExecuteResultAsync(context);");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder;
}
}