private static string FormatKeyValue(object target) { string[] keys; object[] values; IBasicKeyValueProvider ikvp = target as IBasicKeyValueProvider; IDictionary <string, object> isod = target as IDictionary <string, object>; if (ikvp != null) { keys = ikvp.Keys.ToArray(); values = (from t in keys select ikvp[t]).ToArray(); } else if (isod != null) { keys = isod.Keys.ToArray(); values = (from t in keys select isod[t]).ToArray(); } else { return(target?.ToString()); } if (keys.Length != 0) { int mx = keys.Max(n => n.Length); return(string.Join(Environment.NewLine, from k in keys.Select((s, i) => new { Index = i, Key = s }) join v in values.Select((v, i) => new { Index = i, Value = v }) on k.Index equals v.Index orderby k.Key select string.Format($"{{0,-{mx}}} = {{1}}", k.Key, v.Value))); } return(""); }
/// <summary> /// Formats the given object using this formatter /// </summary> /// <param name="targetObject">the target object to format</param> /// <returns>a formatted string representing the given object</returns> public override string Format(object targetObject) { IBasicKeyValueProvider provider = targetObject as IBasicKeyValueProvider; StringBuilder bld = new StringBuilder(); var tmp = (from t in provider.Keys orderby t select new { t.Length, Key = t, Value = provider[t] }).ToArray(); int ml = tmp.Max(n => n.Length); string frmt = $"{{2}}{{0,-{ml}}}{{3}}{{1:obj}}{Environment.NewLine}"; int currentIndent = 0; if (indent.IsValueCreated && indent.Value != -1) { currentIndent = indent.Value; } string padString = new string(' ', currentIndent); string lineString = currentIndent == 0 ? "" : Environment.NewLine; indent.Value = currentIndent + 3; bool ok = false; try { foreach (var item in tmp) { ok = true; bld.AppendFormat(frmt, item.Key, item.Value, padString, lineString); } } finally { if (currentIndent != 0) { indent.Value = currentIndent; } else { indent.Value = -1; } } return(ok ? bld.ToString(0, bld.Length - 2) : string.Empty); }
/// <summary> /// Checks whether the columns need to be re-evaluated using the current raw-record /// </summary> /// <param name="data">the provided raw-data</param> /// <returns>a value indicating whether this is header-data</returns> public bool CheckFirstLine(IBasicKeyValueProvider data) { string msg; var tmp = ReEvaluateHeaders.Decide(new ColumnReEvaluationData(data), DecisionMethod.Simple, out msg); bool retVal = false; if ((tmp & (DecisionResult.Success | DecisionResult.Acceptable)) != DecisionResult.None) { retVal = true; autoMap.Clear(); foreach (string column in data.Keys) { if (column != "$origin") { string name = $"{data[column]}"; if (!string.IsNullOrEmpty(name)) { if (Regex.IsMatch(name, @"^\d+$")) { name = $"@{name}"; } if (ForceAlphanumericColumnNames) { name = Regex.Replace(name, @"[^\w_@]", "", RegexOptions.Singleline); } autoMap.Add(column, name); } } } } return(retVal); }
public static object GetMemberValue(this object target, string name, Type explicitType, ScriptValues.ValueType valueType) { if (valueType == ScriptValues.ValueType.Method || valueType == ScriptValues.ValueType.Constructor) { var bv = target; var olt = bv as ObjectLiteral; if (valueType == ScriptValues.ValueType.Constructor && olt != null) { return(olt[name]); } return(bv); } object targetObject; bool isEnum; MemberInfo mi = FindMember(target, name, explicitType, out targetObject, out isEnum); ObjectLiteral ojl = targetObject as ObjectLiteral; FunctionLiteral ful = targetObject as FunctionLiteral; IDictionary <string, object> odi = targetObject as IDictionary <string, object>; IBasicKeyValueProvider iba = targetObject as IBasicKeyValueProvider; if (mi == null) { if (ojl != null) { return(ojl[name]); } if (ful != null) { return(ful.GetInitialScopeValue(name)); } if (odi != null && odi.ContainsKey(name)) { return(odi[name]); } else if (odi != null) { return(null); } if (iba != null && iba.ContainsKey(name)) { return(iba[name]); } else if (iba != null) { return(null); } } if ( isEnum) { return(Enum.Parse((Type)targetObject, name)); } if (mi == null) { throw new ScriptException(string.Format("Member {0} is not declared on {1}", name, targetObject)); } if (mi is PropertyInfo) { PropertyInfo pi = (PropertyInfo)mi; if (pi.CanRead) { return(pi.GetValue(targetObject, null)); } return(null); } if (mi is FieldInfo) { return(((FieldInfo)mi).GetValue(targetObject)); } if (mi is EventInfo) { return(null); } throw new ScriptException(string.Format("GetValue is not supported for MemberType {0}", mi.MemberType)); }
public static Type GetMemberType(this object target, string name, Type explicitType, ScriptValues.ValueType valueType) { if (valueType == ScriptValues.ValueType.Method || valueType == ScriptValues.ValueType.Constructor) { return(null); } object targetObject; bool isEnum; MemberInfo mi = FindMember(target, name, explicitType, out targetObject, out isEnum); ObjectLiteral ojl = targetObject as ObjectLiteral; FunctionLiteral ful = targetObject as FunctionLiteral; IDictionary <string, object> odi = targetObject as IDictionary <string, object>; IBasicKeyValueProvider iba = targetObject as IBasicKeyValueProvider; if (mi == null) { if (ojl != null) { return(ojl[name]?.GetType() ?? typeof(object)); } if (ful != null) { return(ful.GetInitialScopeValue(name)?.GetType() ?? typeof(object)); } if (odi != null && odi.ContainsKey(name)) { return(odi[name]?.GetType() ?? typeof(object)); } else if (odi != null) { return(null); } if (iba != null && iba.ContainsKey(name)) { return(iba[name]?.GetType() ?? typeof(object)); } else if (iba != null) { return(null); } } if (isEnum) { return((Type)targetObject); } if (mi == null) { throw new ScriptException(string.Format("Member {0} is not declared on {1}", name, targetObject)); } if (mi is PropertyInfo pi) { if (pi.CanRead) { return(pi.PropertyType); } return(null); } if (mi is FieldInfo fi) { return(fi.FieldType); } if (mi is EventInfo ev) { return(ev.EventHandlerType); } throw new ScriptException(string.Format("GetValue is not supported for MemberType {0}", mi.MemberType)); }
/// <summary> /// Consumes the provided data and provides a value indicating whether the data could be processed and a callback to create a success-notification /// </summary> /// <param name="data">the data that was extracted from an ImportSource</param> /// <param name="logCallback">a callback that can be used to log events on the parser-process</param> /// <param name="getNotification">a callback that will provide an acceptance parameter for the current parsing-unit</param> /// <returns>a value indicating whether the data was successfully processed</returns> protected override bool Consume(IBasicKeyValueProvider data, LogParserEventCallback <IBasicKeyValueProvider> logCallback, out Func <DynamicResult, KeyValueAcceptanceCallbackParameter> getNotification) { bool ok = false; List <string> acceptedKeys = new List <string>(); switch (columnMode) { case ColumnNameMode.Original: { foreach (string name in data.Keys) { SetValueOfColumn(name, data[name]); acceptedKeys.Add(name); } AddCurrentRecord(); ok = true; break; } case ColumnNameMode.FromFirstLine: { if (!CheckFirstLine(data)) { foreach (KeyValuePair <string, string> item in autoMap) { SetValueOfColumn(item.Value, data[item.Key]); acceptedKeys.Add(item.Key); } if (data.ContainsKey("$origin")) { SetValueOfColumn("$origin", data["$origin"]); } AddCurrentRecord(); } ok = true; break; } case ColumnNameMode.FromConfig: { foreach (ColumnConfiguration column in config.Columns) { try { SetValueOfColumn(column.TableColumn, data[column.RawDataKey]); acceptedKeys.Add(column.RawDataKey); } catch (Exception ex) { logCallback(data, $@"Unable to process the provided Recordset: {ex.Message}", ParserEventSeverity.Error); } } if (data.ContainsKey("$origin")) { SetValueOfColumn("$origin", data["$origin"]); } AddCurrentRecord(); ok = true; break; } } getNotification = d => new KeyValueAcceptanceCallbackParameter(ok, tableName, d, data, acceptedKeys.ToArray()); return(ok); }
/// <summary> /// Gets the demanded value from the given object /// </summary> /// <param name="source">the source object from which to get the desired value</param> /// <param name="propertyPath">the path to the Property to read from the object</param> /// <returns>the value of the given property</returns> private static object GetObject(object source, string propertyPath) { string[] path = TokenizePath(propertyPath); object retVal = source; foreach (string propertyName in path) { object ret = null; if (retVal is IDictionary) { IDictionary tmp = (IDictionary)retVal; if (tmp.Contains(propertyName)) { ret = tmp[propertyName]; } } else if (retVal is IDictionary <string, object> ) { IDictionary <string, object> tmp = (IDictionary <string, object>)retVal; if (tmp.ContainsKey(propertyName)) { ret = tmp[propertyName]; } else { LogEnvironment.LogDebugEvent(null, string.Format("Property {0} is unknown", propertyName), (int)LogSeverity.Warning, null); } } else if (retVal is IBasicKeyValueProvider) { IBasicKeyValueProvider tmp = (IBasicKeyValueProvider)retVal; ret = tmp[propertyName]; } else if (retVal is IDataReader) { IDataReader tmp = (IDataReader)retVal; for (int i = 0; i < tmp.FieldCount; i++) { if (tmp.GetName(i).Equals(propertyName, StringComparison.OrdinalIgnoreCase)) { ret = tmp[i]; break; } } } else if (retVal is DataRow) { DataRow tmp = (DataRow)retVal; if (tmp.Table != null && tmp.Table.Columns.IndexOf(propertyName) != -1) { ret = tmp[propertyName]; } } else if (retVal is DynamicObject) { try { var binder = Binder.GetMember(CSharpBinderFlags.None, propertyName, retVal.GetType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); var callsite = CallSite <Func <CallSite, object, object> > .Create(binder); ret = callsite.Target(callsite, retVal); } catch (Exception ex) { LogEnvironment.LogDebugEvent(null, ex.ToString(), (int)LogSeverity.Error, null); throw; } } if (ret == null && retVal != null) { Type t = retVal.GetType(); PropertyInfo info = t.GetProperty(propertyName, BindingFlags.FlattenHierarchy | BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (info != null) { try { ret = info.GetValue(retVal, null); } catch (Exception ex) { ret = null; } } else if (propertyName == ".") { ret = retVal; } else { FieldInfo f = t.GetField(propertyName); if (f != null) { ret = f.GetValue(retVal); } } } retVal = ret; if (retVal == null) { break; } } return(retVal); }
/// <summary> /// Initializes a new instance of the ColumnReEvaluationData class /// </summary> /// <param name="data">the provided dataset that required to re-evaluate</param> public ColumnReEvaluationData(IBasicKeyValueProvider data) { Data = data; }