public void CreateModels(IEnumerable <OvhApi> apis) { Dictionary <string, ModelType> modelsByNamespace = new Dictionary <string, ModelType>(); foreach (var ovhApi in apis) { foreach (var model in ovhApi.Models) { string fullTypeName = Util.GetType(model.Key); if (modelsByNamespace.ContainsKey(fullTypeName)) { continue; } modelsByNamespace.Add(fullTypeName, new ModelType(model.Value)); } } foreach (var st in modelsByNamespace) { string modelNamespace = Util.GetNamespace(st.Value.Model); if (modelsByNamespace.ContainsKey(modelNamespace)) { modelsByNamespace[modelNamespace].AddChild(st.Value); } } foreach (var type in modelsByNamespace.Where(x => x.Value.Parent == null)) { CodeNamespace ns = new CodeNamespace(Util.GetNamespace(type.Value.Model)); ns.Types.Add(CreateType(type.Value)); _code.Namespaces.Add(ns); } }
private static CodeTypeReference GetPropertyType(Model model, Property prop) { CodeTypeReference result = null; string type = Util.GetType(prop.Type); bool isString = false; if (type.Contains('.')) { result = new CodeTypeReference(type); } else if (model.Generics != null && model.Generics.Contains(type)) { result = new CodeTypeReference(type, CodeTypeReferenceOptions.GenericTypeParameter); } else { switch (type) { case "phoneNumber": case "string": result = new CodeTypeReference(typeof(string)); isString = true; break; case "long": result = new CodeTypeReference(typeof(long)); break; case "int": result = new CodeTypeReference(typeof(int)); break; case "DateTime": result = new CodeTypeReference(typeof(DateTime)); break; case "bool": result = new CodeTypeReference(typeof(bool)); break; case "double": result = new CodeTypeReference(typeof(double)); break; case "string[]": result = new CodeTypeReference(typeof(string[])); break; case "long[]": result = new CodeTypeReference(typeof(long[])); break; default: result = new CodeTypeReference(typeof(string)); isString = true; break; } } if (prop.CanBeNull && !isString && !type.EndsWith("[]") && !type.Contains(".")) { CodeTypeReference tmp = new CodeTypeReference(typeof(Nullable <>)); tmp.TypeArguments.Add(result); result = tmp; } return(result); }