/// <summary> /// 根据Type类型和Type列表创建入口节点 /// </summary> /// <param name="t"></param> /// <param name="pointTypeList"></param> /// <param name="baseType">为RestLogic的子类,如果为空或不为RestLogic子类,则自动识别为RestLogic</param> /// <returns></returns> internal static EntryPointEntity CreateFrom(Type t, List <Type> pointTypeList, Type baseType) { if (LType == null) { if (baseType != null && baseType.GetTypeInfo().IsSubclassOf(typeof(RestLogic))) { LType = baseType; } else { LType = typeof(RestLogic); } } if (t.GetTypeInfo().BaseType != baseType) { return(null); } var rtn = new EntryPointEntity(); var tmp = (RestLogic)Activator.CreateInstance(t, true); //版本号 var ns = t.Namespace; if (_reg_version_.IsMatch(ns)) { rtn.APIVersion = _reg_version_.Match(ns).Value.ToLower().Replace("_", ""); } rtn.Name = tmp.Name; //建立树形调用链式结构 rtn.BuildEntryRouteInvokeLink(t, pointTypeList); //建立基于RouteAttribute描述的独立入口 rtn.BuildSingleEntryInvokeLink(t, pointTypeList); //建立API Description Doc rtn.RouteDesc = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); var index = 1; foreach (var item in rtn._d_invoke_) { var key = $"NO.{index}"; var v = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); v.Desc = item.Value.Last().RouteDesc; v.Verb = item.Key.Split(':')[0]; v.Route = item.Key.Split(':')[1]; v.Inputs = (from tt in item.Value.Last().InputItems select new { tt.Name, tt.Desc, tt.ValueType, tt.DefaultValue, tt.Position, tt.IsAllowEmpty }).ToList(); var outputitem = item.Value.Last().OutputDesc; v.Output = new { outputitem.Desc, outputitem.FormatDesc, outputitem.ReturnType }; if (item.Value.Last().IsVisible) { rtn.RouteDesc.SetValue(key, v); } index++; } //通过{verb+route+keyname@keyname所在位置}做key映射一个索引 var list = new List <string>(); foreach (var item in rtn._d_invoke_) { var verb = item.Key.Split(':')[0]; var url = item.Key.Split(':')[1]; var sary = url.Split('/').Where(p => p != ""); var express = _reg_brace_p_.Replace(url, @"{p}").Substring(1); //关键字 var keynames = express.Split('/', StringSplitOptions.RemoveEmptyEntries); list.AddRange(keynames); var key = $"{verb}:{sary.Count()}:"; for (var i = 0; i < keynames.Count(); i++) { if (keynames[i] != "{p}") { key += $"{keynames[i]}@{i}|"; } } if (!rtn._d_route_index_.ContainsKey(key)) { rtn._d_route_index_.Add(key, item.Key); } } rtn._invalid_keys_ = list.Distinct().ToList(); return(rtn); }
/// <summary> /// 创建Rest入口节点上下文 /// </summary> /// <param name="assemblyName">logic所在的Assembly名称</param> /// <param name="mainversion">主版本号</param> /// <returns></returns> public override void Load(string assemblyName, string mainversion, Type logicBaseType) { MainVersion = mainversion; Assembly asm = Assembly.Load(new AssemblyName(assemblyName)); Type[] ts = asm.GetTypes(); var reg = new Regex(@"(?<=.)V\d+._\d+", RegexOptions.IgnoreCase); var list = ts.Where(p => p.GetTypeInfo().IsSubclassOf(logicBaseType)).ToList(); foreach (var t in list.Where(p => p.GetTypeInfo().BaseType == logicBaseType)) { var ple = EntryPointEntity.CreateFrom(t, list, logicBaseType); //如果为null,则不处理 if (ple == null) { continue; } //在版本号下构建多入口节点 if (!_d_entry_.ContainsKey(ple.APIVersion)) { var entrylist = new List <EntryPointEntity>(); entrylist.Add(ple); _d_entry_.Add(ple.APIVersion, entrylist); } else { _d_entry_[ple.APIVersion].Add(ple); } } RouteDesc = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); MainRouteDesc = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); foreach (var litem in _d_entry_) { foreach (var item in litem.Value) { if (!RouteDesc.Keys.Contains(item.APIVersion)) { RouteDesc.SetValue(item.APIVersion, item.RouteDesc); } else { var rd = (FrameDLRObject)RouteDesc.GetValue(item.APIVersion); var index = rd.Keys.Count; foreach (var v in item.RouteDesc.Items) { rd.SetValue($"NO.{index+1}", v.Value); index++; } } } } if (MainVersion == "") { var ver = "v0.0"; foreach (KeyValuePair <string, object> item in RouteDesc.Items) { var v = double.Parse(item.Key.Replace("v", "")); if (v > double.Parse(ver.Replace("v", ""))) { ver = item.Key; } } MainVersion = ver; } if (RouteDesc.GetValue(MainVersion) != null) { MainRouteDesc = (FrameDLRObject)RouteDesc.GetValue(MainVersion); } else { MainRouteDesc.SetValue("warning", "无可调用API"); } }