/// <summary> /// Adds an already resolved value to the cache /// </summary> public void AddResolved(string name, string strValue) { var value = new ResolvedValue(name, Entity.Types.STR, strValue); value.SetValue(strValue); Resolved.Add(name, value); }
/// <summary> /// When comparing the paths, any variables found will be /// returned in the cache. /// </summary> public bool Equals(Route route, out Cache cache) { // Must have same number of parts to match if (Parts.Length != route.Parts.Length) { cache = null; return(false); } // All parts must match cache = new Cache(); for (int i = 0; i < Parts.Length; i++) { // If either part is variable then they match if (IsVar[i]) { var resolved = new ResolvedValue("path." + Parts[i], Entity.Types.STR, route.Parts[i]); resolved.SetValue(route.Parts[i]); cache.SetResolved(resolved); } else if (route.IsVar[i]) { cache.AddResolved("path." + route.Parts[i], Parts[i]); } else if (!Parts[i].Equals(route.Parts[i])) { cache = null; return(false); } } return(true); }
/// <summary> /// Adds the value to the resolved set and removes it from the unresolved set. /// </summary> public void SetResolved(ResolvedValue value) { // Special case: name is null (or ends with '.NOCACHE') for values that shouldn't be cached if (value.Name == null || value.Name.EndsWith(".NOCACHE")) { return; } Resolved.Add(value.Name, value); unresolved.Remove(value.Name); }
public string InsertVars(Cache vars, ApiBody request, ApiBody response) { // Make sure folder is valid and exists try { if (!Directory.Exists(Folder)) { Directory.CreateDirectory(Folder); } } catch (Exception e) { throw new Exception($"Failed to create folder '{Folder}': {e.Message}"); } var sb = new StringBuilder(); int pos = 0; int varStart = Pattern.IndexOf('{'); while (varStart != -1) { int varEnd = Pattern.IndexOf('}', varStart); if (varEnd == -1) { throw new Exception($"Persist definition {OutputDef} has mismatched braces. Missing '}}'."); } sb.Append(Pattern.Substring(pos, varStart - pos)); string name = Pattern.Substring(varStart + 1, varEnd - (varStart + 1)); if (name.Equals("*")) { sb.Append('*'); } else { ResolvedValue resolved = null; try { if (name.StartsWith("path.") || name.StartsWith("query.") || name.StartsWith("request.")) { resolved = vars.GetValue(name); } else { if (response != null) { resolved = vars.GetValueOrNull(response.EntityName + "." + name); } if (resolved == null) { if (request == null) { resolved = vars.GetValue(name); } else { resolved = vars.GetValue("request." + request.EntityName + "." + name); } } } } catch (Exception) { if (response != null && response.Mods != null) { // Variable might be in response mods. string newName = response.Mods.GetValueOrDefault(response.EntityName + "." + name); if (newName != null) { if (newName[0] == '~') { // Mod is a reference try { resolved = vars.GetValue(newName.Substring(1)); } catch (Exception) { // Do nothing - will be caught by null check } } else { // Mod is a literal resolved = new ResolvedValue(name, Entity.Types.STR, newName); resolved.SetValue(newName); } } } } if (resolved == null) { throw new Exception($"Persist definition '{OutputDef}' variable '{name}' does not match any known attribute."); } sb.Append(resolved.Value); } pos = varEnd + 1; if (pos < Pattern.Length) { varStart = WildPattern.IndexOf('{', pos); } else { break; } } if (pos < Pattern.Length) { sb.Append(Pattern.Substring(pos)); } return(Path.Combine(Folder, sb.ToString())); }