public static GenerateNode AppendChild(this GenerateNode node, string key, string value) { GenerateNode child = new GenerateNode(key, value); node.ApplyParameters.Add(child); return(child); }
public static GenerateNode Add(this List <GenerateNode> nodes, string key, string value) { GenerateNode newNode = new GenerateNode(key, value); nodes.Add(newNode); return(newNode); }
public static void AppendChild(this GenerateNode node, string key, IEnumerable <string> values) { if (values != null) { foreach (string value in values) { node.ApplyParameters.Add(new GenerateNode(key, value)); } } }
/// <summary> /// 將 GenerateNode 轉換成容易閱讀的樹狀結構 /// </summary> public static async Task <JToken> ToJTokenForReadAsync(this GenerateNode node) { if (string.IsNullOrWhiteSpace(node.ApplyApi)) { return(JToken.FromObject(await node.ReadAsStringAsync())); } else { JObject jObject = new JObject(); if (node.ApplyParameters != null && node.ApplyParameters.Any()) { foreach (var groupParameters in node.ApplyParameters.GroupBy(x => x.ApplyKey)) { if (groupParameters != null && groupParameters.Any()) { if (groupParameters.Count() == 1) { if (string.IsNullOrWhiteSpace(node.ApplyApi)) { jObject.Add( groupParameters.Key, JToken.FromObject( await groupParameters.First().ReadAsStringAsync())); } else { jObject.Add( groupParameters.Key, await groupParameters.First().ToJTokenForReadAsync()); } } else { JArray jArray = new JArray(); foreach (var parameter in groupParameters) { jArray.Add(await parameter.ToJTokenForReadAsync()); } jObject.Add(groupParameters.Key, jArray); } } } } return(new JObject { { node.ApplyApi, jObject } }); } }
/// <summary> /// 取得 ApplyValue 的值或透過 ApplyApi 取得範本 /// </summary> public static async Task <string> ReadAsStringAsync(this GenerateNode node) { if (string.IsNullOrWhiteSpace(node.ApplyValue) == false) { return(node.ApplyValue); } if (string.IsNullOrWhiteSpace(node.ApplyApi) == false) { HttpClient client = new HttpClient(); var message = await client.GetAsync(node.ApplyApi); if (message.StatusCode == System.Net.HttpStatusCode.OK) { return(await message.Content.ReadAsStringAsync()); } throw new GenerateException($"HttpGet {node.ApplyApi} Failed ({message.StatusCode})."); } return(node.ApplyValue); }
private static async Task <GenerateNode[]> ToGenerateNodesAsync(this CodeTemplate.TransactionTemplateNode templateNode, JObject input) { GenerateNode generateNode = new GenerateNode(); generateNode.ApplyKey = templateNode.Name; generateNode.ApplyApi = templateNode.ToUrl(input); if (templateNode.TransactionParameterNodes != null) { foreach (var templateParameterNode in templateNode.TransactionParameterNodes) { var children = await templateParameterNode.ToGenerateNodesAsync(input, templateNode.TransactionAdapter); foreach (var child in children) { generateNode.AppendChild(child).ChangeKey(templateParameterNode.Name); } } } return(new GenerateNode[] { generateNode }); }
/// <summary> /// 以 GenerateNode 生成代碼 /// </summary> public static async Task <string> GenerateAsync(this GenerateNode root) { string result = await root.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(result)) { return(null); } string paramLeft = root.Settings?.ParamLeft ?? "{{#"; // 參數起始的保留字 string paramRight = root.Settings?.ParamRight ?? "}}"; // 參數結束的保留字 string eachSeparator = root.Settings?.EachSeparator ?? ","; // 參數設定分隔符的保留字 string withDefault = root.Settings?.WithDefault ?? "|"; // 參數設定預設值的保留字 int searchIndex = 0; int paramStartIndex = result.IndexOf(paramLeft); while (paramStartIndex >= 0) { paramStartIndex = paramStartIndex + paramLeft.Length; int paramEndIndex = result.IndexOf(paramRight, paramStartIndex) - 1; int lineStartIndex = result.Substring(0, paramStartIndex).LastIndexOf("\n") + 1; int lineEndIndex = CSharpHelper.Using(result.IndexOf("\n", lineStartIndex + 1), x => x >= 0 ? x : result.Length) - 1; int separatorStartIndex = CSharpHelper.Using(result.Substring(0, paramEndIndex).IndexOf(eachSeparator, paramStartIndex), x => x >= 0 ? x + eachSeparator.Length : -1); int defaultStartIndex = CSharpHelper.Using(result.Substring(0, paramEndIndex).IndexOf(withDefault, Math.Max(paramStartIndex, separatorStartIndex)), x => x >= 0 ? x + withDefault.Length : -1); int separatorEndIndex = defaultStartIndex >= 0 ? defaultStartIndex - withDefault.Length - 1 : paramEndIndex; int defaultEndIndex = paramEndIndex; int keyStartIndex = paramStartIndex; int keyEndIndex = separatorStartIndex >= 0 ? separatorStartIndex - eachSeparator.Length - 1 : defaultStartIndex >= 0 ? defaultStartIndex - withDefault.Length - 1 : paramEndIndex; if (paramStartIndex < paramEndIndex && paramEndIndex < lineEndIndex && separatorStartIndex < paramEndIndex && defaultStartIndex < paramEndIndex) { string prefix = result.Substring(lineStartIndex, paramStartIndex - paramLeft.Length - lineStartIndex); string suffix = result.Substring(paramEndIndex + paramRight.Length + 1, lineEndIndex - (paramEndIndex + paramRight.Length)); string key = result.Substring(keyStartIndex, keyEndIndex - keyStartIndex + 1).Trim(); string value = ""; string param = result.Substring(paramStartIndex, paramEndIndex - paramStartIndex + 1); string separator = separatorStartIndex >= 0 ? result.Substring(separatorStartIndex, separatorEndIndex - separatorStartIndex + 1).Trim() : ""; string @default = defaultStartIndex >= 0 ? result.Substring(defaultStartIndex, defaultEndIndex - defaultStartIndex + 1).Trim() : ""; foreach (var synonym in GenerateSynonyms) { separator = separator.Replace(synonym.Key, synonym.Value); @default = @default.Replace(synonym.Key, synonym.Value); } #region Apply if (string.IsNullOrWhiteSpace(key) == false) { if (root.ApplyParameters.Any(x => x.ApplyKey == key)) { value = string.Join( separator, await Task.WhenAll( root.ApplyParameters .Where(x => x.ApplyKey == key) .Select(async node => { if (string.IsNullOrWhiteSpace(node.ApplyApi) == false) { return(await GenerateAsync(node)); } return(await node.ReadAsStringAsync()); }))); } if (string.IsNullOrWhiteSpace(value)) { value = @default; } if (string.IsNullOrWhiteSpace(prefix)) { value = value.Replace("\n", $"\n{prefix}"); } if (string.IsNullOrWhiteSpace(value) && string.IsNullOrWhiteSpace(prefix) && string.IsNullOrWhiteSpace(suffix)) { if (lineEndIndex + 2 < result.Length) { result = $"{result.Substring(0, Math.Max(lineStartIndex - 1, 0))}{result.Substring(lineEndIndex + 2)}"; searchIndex = lineStartIndex; } else { result = $"{result.Substring(0, Math.Max(lineStartIndex - 1, 0))}"; searchIndex = -1; } } else { result = $"{result.Substring(0, paramStartIndex - paramLeft.Length)}{value}{result.Substring(paramEndIndex + paramRight.Length + 1)}"; searchIndex = paramStartIndex - paramLeft.Length + value.Length; } } else { searchIndex = paramStartIndex + paramRight.Length; } #endregion } else { searchIndex = paramStartIndex + paramRight.Length; } paramStartIndex = searchIndex >= 0 ? result.IndexOf(paramLeft, searchIndex) : -1; } return(result); }
public static GenerateNode AppendChild(this GenerateNode node, GenerateNode child) { node.ApplyParameters.Add(child); return(child); }