internal static MaterialProperty GetMaterialProperty(SearchItem item, SearchColumn column) { var mat = item.ToObject <Material>(); if (!mat) { return(null); } foreach (var m in SelectorManager.Match(column.selector, item.provider?.type)) { var selectorArgs = new SearchSelectorArgs(m, item); if (selectorArgs.name == null) { continue; } if (!mat.HasProperty(selectorArgs.name)) { continue; } return(MaterialEditor.GetMaterialProperty(new Object[] { mat }, selectorArgs.name)); } return(null); }
public static IEnumerable <SearchItem> Select(SearchExpressionContext c) { if (c.args.Length < 2) { c.ThrowError($"Invalid arguments"); } // Select dataset var dataset = c.args[0].Execute(c); var results = dataset; var sIt = c.args.Skip(1).GetEnumerator(); while (sIt.MoveNext()) { var selector = sIt.Current; if (IsSelectorLiteral(selector)) { var selectorName = selector.innerText.ToString(); var selectorAlias = c.ResolveAlias(selector); results = TaskEvaluatorManager.EvaluateMainThread(results, item => { var selectedValue = SelectorManager.SelectValue(item, c.search, selectorName, out string suggestedSelectorName); AddSelectedValue(item, selector.innerText.ToString(), selectorAlias ?? suggestedSelectorName, selectedValue); return(item); }); } else { results = ProcessIterableSelector(c, results, selector); } } return(results); }
static string ResolveVariable(string query, string token, string varName, SearchExpressionContext c) { var v = SelectorManager.SelectValue(c, varName, out var _); if (v == null) { c.ThrowError($"Cannot resolve variable {token}{varName}"); } return(query.Replace(token + varName, v.ToString())); }
public static bool TryConvertToDouble(SearchItem item, out double value, string selector = null) { value = double.NaN; object itemValue = SelectorManager.SelectValue(item, null, selector); if (itemValue == null) { return(false); } return(Utils.TryGetNumber(itemValue, out value)); }
public static string FormatItem(SearchContext ctx, SearchItem item, string formatString) { if (formatString == null) { var value = SelectorManager.SelectValue(item, ctx, null); return(value == null ? "null" : value.ToString()); } return(ParserUtils.ReplaceSelectorInExpr(formatString, (selector, cleanedSelector) => { var value = SelectorManager.SelectValue(item, ctx, cleanedSelector); return value == null ? "<no value>" : value.ToString(); })); }
public static IEnumerable <SearchItem> Compare(SearchExpressionContext c, Func <object, object, bool> comparer) { if (c.args.Length < 2 || c.args.Length > 3) { c.ThrowError($"Invalid arguments"); } if (c.args.Length == 3 && !IsSelectorLiteral(c.args[1])) { c.ThrowError($"Invalid selector"); } var setExpr = c.args[0]; if (!setExpr.types.IsIterable()) { c.ThrowError("Primary set is not iterable", setExpr.outerText); } string valueSelector = null; var compareExprIndex = 1; if (c.args.Length == 3) { valueSelector = c.args[1].innerText.ToString(); compareExprIndex++; } var compareExpr = c.args[compareExprIndex]; var compareValueItr = compareExpr.Execute(c).FirstOrDefault(e => e != null); if (compareValueItr == null) { c.ThrowError("Invalid comparer value", compareExpr.outerText); } var compareValue = compareValueItr.value; if (valueSelector == null) { return(setExpr.Execute(c).Where(item => item != null && comparer(item.GetValue(valueSelector), compareValue))); } return(TaskEvaluatorManager.EvaluateMainThread(setExpr.Execute(c), item => { var value = SelectorManager.SelectValue(item, c.search, valueSelector); if (value != null && comparer(value, compareValue)) { return item; } return null; }, 25)); }
public static IEnumerable <SearchItem> GroupBy(SearchExpressionContext c) { string selector = null; if (c.args.Length > 1) { selector = c.args[1].innerText.ToString(); } var outputValueFieldName = System.Guid.NewGuid().ToString("N"); var dataSet = SelectorManager.SelectValues(c.search, c.args[0].Execute(c), selector, outputValueFieldName); foreach (var _group in dataSet.GroupBy(item => item.GetValue(outputValueFieldName))) { var group = _group; var groupId = group.Key?.ToString() ?? $"group{++s_NextGroupId}"; if (c.HasFlag(SearchExpressionExecutionFlags.Expand)) { var evaluator = new SearchExpressionEvaluator(groupId, _ => group, SearchExpressionEvaluationHints.Default); var genExpr = new SearchExpression(SearchExpressionType.Group, groupId.GetStringView(), groupId.GetStringView(), (group.Key?.ToString() ?? groupId).GetStringView(), evaluator); yield return(EvaluatorUtils.CreateSearchExpressionItem(genExpr)); } else { SearchProvider groupProvider = null; foreach (var item in group) { if (groupProvider == null) { groupProvider = SearchUtils.CreateGroupProvider(item.provider, groupId, s_NextGroupId); } item.provider = groupProvider; yield return(item); } } } }
internal static SerializedProperty GetSerializedProperty(SearchItem item, SearchColumn column, out SerializedObject so) { foreach (var m in SelectorManager.Match(column.selector, item.provider?.type)) { var selectorArgs = new SearchSelectorArgs(m, item); if (selectorArgs.name == null) { continue; } var property = GetSerializedProperty(item, selectorArgs.name, out so); if (property == null) { so?.Dispose(); continue; } so.UpdateIfRequiredOrScript(); return(property); } so = null; return(null); }
public static IEnumerable <SearchItem> Alias(SearchExpressionContext c) { var aliasSelector = c.args.Last(); if (c.HasFlag(SearchExpressionExecutionFlags.Expand)) { yield return(EvaluatorUtils.CreateSearchExpressionItem(new SearchExpression(c.args[0], newAlias: aliasSelector.innerText))); } else { foreach (var r in c.args[0].Execute(c)) { if (r == null) { yield return(null); continue; } var hasFormatString = EvaluatorUtils.GetFormatString(aliasSelector, out var formatStr); if (hasFormatString && aliasSelector.types.HasAny(SearchExpressionType.Text)) { r.label = EvaluatorUtils.FormatItem(c.search, r, formatStr); } else if (aliasSelector.types.HasAny(SearchExpressionType.Selector)) { r.label = SelectorManager.SelectValue(r, c.search, aliasSelector.innerText.ToString()).ToString(); yield return(r); } else if (aliasSelector.types.HasAny(SearchExpressionType.Iterable)) { bool valueSelected = false; using (c.runtime.Push(r)) { foreach (var s in aliasSelector.Execute(c)) { if (s != null) { r.label = s.value.ToString(); valueSelected = true; break; } else { yield return(null); } } } if (!valueSelected) { r.label = r.label = EvaluatorUtils.FormatItem(c.search, r, aliasSelector.innerText.ToString()); } } else { c.ThrowError($"Alias selector `{aliasSelector.outerText}` not supported", aliasSelector.outerText); } yield return(r); } } }
internal object SelectValue(SearchItem item, SearchContext context) { return(SelectorManager.SelectValue(item, context, selector)); }