public static void AddDependentModels(ref CombinedClass combined) { var missingClasses = new List <TypescriptModelClass>(); foreach (var cls in combined.TypescriptModelClassInfoList) { foreach (var prop in cls.ModelType.GetProperties()) { if (prop.Name == "ProfileImage") { if (prop.PropertyType.Name == "") { continue; } } Console.WriteLine($"Processing Property : {cls.ModelType.Name + "." + prop.Name}"); if (cls.ModelType.Name == prop.PropertyType.Name) { continue; } if (prop.PropertyType.IsGenericType && (prop.PropertyType.GetGenericArguments()[0].Name == cls.ModelType.Name)) { continue; } ProcessDependentModelSubProperty(cls.ModelType, ref combined, prop, ref missingClasses); } } combined.TypescriptModelClassInfoList.AddRange(missingClasses); }
public string AssignContainer([FromBody] String jsonContainer) { try { CombinedClass combinedClass = JsonConvert.DeserializeObject <CombinedClass>(jsonContainer); combinedClass.EventMessages.TripId = combinedClass.Containers.Id; db.EventMessages.Add(combinedClass.EventMessages); Containers old = db.Containers.Where(x => x.Id == combinedClass.Containers.Id).FirstOrDefault(); if (old != null) { combinedClass.Containers = old; combinedClass.Containers.DriverId = combinedClass.EventMessages.DriverId; combinedClass.Containers.MovingTo = combinedClass.EventMessages.MovingTo; combinedClass.Containers.LastStatus = combinedClass.EventMessages.Status; combinedClass.Containers.Opprettetav = combinedClass.Containers.Endretav = combinedClass.EventMessages.DriverId.ToString(); combinedClass.Containers.Opprettet = combinedClass.Containers.Endret = DateTime.Now; db.Entry(old).CurrentValues.SetValues(combinedClass.Containers); db.SaveChanges(); } } catch (Exception ex) { return(ex.InnerException.ToString()); } return("success"); }
public static (string, Type) ProcessGenericVariable(CombinedClass combined, Type returnType) { string rName; var args = returnType.GenericTypeArguments; if (args.Length != 1) { throw new NotImplementedException("This scenario is not implemented"); } var innerType = ""; rName = args[0].Name; if (args[0].IsGenericType) { var innerArg = args[0].GenericTypeArguments; if (innerArg.Length != 1) { throw new NotImplementedException("This scenario is not implemented"); } innerType = innerArg[0].Name ?? ""; if (!combined.TypescriptModelClassInfoList.Any(x => x.Name == innerType)) { combined.TypescriptModelClassInfoList.Add(new TypescriptModelClass { Name = innerType, ModelType = innerArg[0], CanIgnore = CanIgnoreReturnType(innerArg[0]) }); } rName = args[0].GetGenericTypeDefinition().Name; rName = GetGenericInnerType(rName); } else if (args[0].IsArray) { if (args[0].Name == "") { throw new Exception("This will never be caught"); } } if (!innerType.IsNullOrEmpty()) { rName = rName + "<" + innerType + ">"; } return(rName, args[0]);
public static CombinedClass GetParsedStructure(Assembly assembly) { var combined = new CombinedClass(); foreach (var aClass in assembly.GetTypes()) { if (!typeof(Microsoft.AspNetCore.Mvc.Controller).IsAssignableFrom(aClass)) { continue; } var cItem = new TClass { ClassName = aClass.Name }; combined.FunctionInfoList.Add(cItem); var methodInfos = aClass.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public); if (methodInfos.Length == 0) { continue; } foreach (var method in methodInfos) { MethodCallType callType; var methodParams = method.GetParameters(); var returnType = method.ReturnType; if (method.GetCustomAttribute(typeof(HttpPostAttribute)) != null) { if (!DoesMethodParamsOneOrNoBody(methodParams)) { throw new NotImplementedException($"This Post scenario is not implemented for {aClass.Name} - {method.Name}"); } callType = MethodCallType.Post; } else if (method.GetCustomAttribute(typeof(HttpGetAttribute)) != null) { callType = MethodCallType.Get; if (DoesMethodParamsAnyBody(methodParams)) { throw new NotImplementedException($"This get scenario is not implemented for {aClass.Name} - {method.Name}"); } } else if (method.GetCustomAttribute(typeof(HttpPutAttribute)) != null) { if (!DoesMethodParamsOneOrNoBody(methodParams)) { throw new NotImplementedException($"This put scenario is not implemented for {aClass.Name} - {method.Name}"); } callType = MethodCallType.Put; } else if (method.GetCustomAttribute(typeof(HttpDeleteAttribute)) != null) { if (DoesMethodParamsAnyBody(methodParams)) { throw new NotImplementedException($"This delete scenario is not implemented for {aClass.Name} - {method.Name}"); } callType = MethodCallType.Delete; } else { throw new NotImplementedException("This scenario is not implemented"); } var rName = returnType.Name; var underlysingReturnType = returnType; if (returnType.BaseType == typeof(System.Threading.Tasks.Task)) { (rName, underlysingReturnType) = CodeGenUtils.ProcessGenericVariable(combined, returnType); } else { if (returnType.IsGenericType) { (rName, underlysingReturnType) = CodeGenUtils.ProcessGenericVariable(combined, returnType); } } var mItem = new TMethod { MethodName = method.Name, CallType = callType, OutputParameterType = rName, CanIgnore = CodeGenUtils.CanIgnoreReturnType(underlysingReturnType) }; cItem.Methods.Add(mItem); //var inputParam = ""; foreach (var param in methodParams) { if (!combined.TypescriptModelClassInfoList.Any(x => x.Name == param.ParameterType.Name)) { combined.TypescriptModelClassInfoList.Add(new TypescriptModelClass { Name = param.ParameterType.Name, ModelType = param.ParameterType, CanIgnore = CanIgnoreReturnType(param.ParameterType) }); } mItem.InputParameters.Add(new InputParam { Key = param.ParameterType.Name, Value = param.Name, TypeFullName = param.ParameterType.FullName, InputType = GetInputType(param) }); } } } return(combined); }
public static void ProcessDependentModelSubProperty(Type modelType, ref CombinedClass combined, PropertyInfo prop, ref List <TypescriptModelClass> missingClasses) { Console.WriteLine($"Inspecting : Model = {modelType.Name} , Prop Reflected - {prop.ReflectedType.FullName} , Prop Name - {prop.Name} , Prop Type - {prop.PropertyType.FullName}"); if (modelType.Name == "WordClauseListModel") { if (prop.PropertyType.IsGenericType) { if (prop.PropertyType.Name == "") { return; } } } if (prop.PropertyType.IsGenericType) { if (prop.PropertyType.Name == "List`1") { var innerType = prop.PropertyType.GetGenericArguments()[0]; if (innerType.Name == "") { return; } } } /* * if (modelType.Name == prop.PropertyType.Name) * { * return; * } * * if (prop.PropertyType.IsGenericType) * { * var innerTypeName = GetGenericInnerType(prop.PropertyType.Name); * if (innerTypeName == modelType.Name) * return; * * if (prop.PropertyType.GetGenericArguments().Count == 1) * { * var innerType = prop.PropertyType.GetGenericArguments()[0]; * * if (IsBasicType(innerType.Name)) * { * return; * } * } * } * else * { * if (IsBasicType(prop.PropertyType.Name)) * { * return; * } * } */ if (IsBasicType(prop.PropertyType.Name)) { return; } if (missingClasses.Any(x => x.Name == prop.PropertyType.Name) || combined.TypescriptModelClassInfoList.Any(x => x.Name == prop.PropertyType.Name)) { foreach (var subprop in prop.PropertyType.GetProperties()) { if (modelType.Name == subprop.PropertyType.Name) { continue; } if (subprop.PropertyType.IsGenericType && (subprop.PropertyType.GetGenericArguments()[0].Name == modelType.Name)) { continue; } ProcessDependentModelSubProperty(modelType, ref combined, subprop, ref missingClasses); } return; } if (IsArrayType(prop.PropertyType) || IsNullable(prop.PropertyType) || prop.PropertyType.IsGenericType) { var actualPropName = ""; Type actualPropType = null; if (prop.PropertyType.IsArray) { if (!prop.PropertyType.Name.EndsWith("[]")) { throw new Exception($"Array type does not end with [] for property {prop.PropertyType.Name}"); } actualPropName = prop.PropertyType.Name.Substring(0, prop.PropertyType.Name.Length - 2); actualPropType = prop.PropertyType; } else { var newPropType = prop.PropertyType.GetGenericArguments()[0]; actualPropName = newPropType.Name; actualPropType = newPropType; } if (IsBasicType(actualPropName)) { return; } if (missingClasses.Any(x => x.Name == actualPropName) || combined.TypescriptModelClassInfoList.Any(x => x.Name == actualPropName)) { foreach (var subprop in actualPropType.GetProperties()) { if (modelType.Name == subprop.PropertyType.Name) { continue; } if (subprop.PropertyType.IsGenericType && (subprop.PropertyType.GetGenericArguments()[0].Name == modelType.Name)) { continue; } ProcessDependentModelSubProperty(modelType, ref combined, subprop, ref missingClasses); } return; } foreach (var subprop in actualPropType.GetProperties()) { if (modelType.Name == subprop.PropertyType.Name) { continue; } if (subprop.PropertyType.IsGenericType && (subprop.PropertyType.GetGenericArguments()[0].Name == modelType.Name)) { continue; } ProcessDependentModelSubProperty(modelType, ref combined, subprop, ref missingClasses); } missingClasses.Add(new TypescriptModelClass { CanIgnore = false, Name = actualPropName, ModelType = actualPropType }); return; } foreach (var subprop in prop.PropertyType.GetProperties()) { if (modelType.Name == subprop.PropertyType.Name) { continue; } if (subprop.PropertyType.IsGenericType && (subprop.PropertyType.GetGenericArguments()[0].Name == modelType.Name)) { continue; } ProcessDependentModelSubProperty(modelType, ref combined, subprop, ref missingClasses); } missingClasses.Add(new TypescriptModelClass { CanIgnore = false, Name = prop.PropertyType.Name, ModelType = prop.PropertyType }); }