/// <summary> /// Process the annotated field and set the values from the configuration parameters. /// </summary> /// <typeparam name="T">Target Instance type</typeparam> /// <param name="node">Configuration Node</param> /// <param name="target">Target Type instance.</param> /// <param name="field">Property to update</param> /// <param name="param">Config param annotation</param> /// <returns>Updated Target Type instance.</returns> private static T ProcessField <T>(ConfigPathNode node, T target, FieldInfo field, ConfigParam param, List <string> valuePaths) { string pname = param.Name; if (String.IsNullOrWhiteSpace(pname)) { pname = field.Name; } string value = null; if (!String.IsNullOrWhiteSpace(param.Path)) { AbstractConfigNode nnode = node.Find(param.Path); if (nnode != null && nnode.GetType() == typeof(ConfigPathNode)) { node = (ConfigPathNode)nnode; } else { node = null; } } if (node != null) { ConfigParametersNode pnode = node.GetParameters(); if (pnode != null) { ConfigValueNode vn = pnode.GetValue(pname); if (vn != null) { value = vn.GetValue(); } if (valuePaths != null) { valuePaths.Add(pnode.GetSearchPath()); } } } if (!String.IsNullOrWhiteSpace(value)) { object v = GetValue <T>(pname, value, param.Function, field.FieldType, target, param.Required); if (v != null) { TypeUtils.CallSetter(field, target, v); } } else if (param.Required) { throw AnnotationProcessorException.Throw(target.GetType(), pname); } return(target); }
/// <summary> /// Find the parameter values for the method. /// </summary> /// <param name="pnode">Parameters Node</param> /// <param name="method">Method Name</param> /// <param name="parameters">Parameters</param> /// <returns>List of object values</returns> private static List <object> FindParameters(ConfigParametersNode pnode, string method, ParameterInfo[] parameters) { List <object> values = new List <object>(); foreach (ParameterInfo pi in parameters) { ConfigParam param = (ConfigParam)Attribute.GetCustomAttribute(pi, typeof(ConfigParam)); if (param != null) { object v = null; ConfigValueNode cv = pnode.GetValue(param.Name); if (cv != null) { string value = cv.GetValue(); if (!String.IsNullOrWhiteSpace(value)) { v = ReflectionUtils.ConvertFromString(pi.ParameterType, value); } } if (v != null) { values.Add(v); } else { throw new AnnotationProcessorException(String.Format("Error Invoking Method: Value not found for parameter. [method={0}][parameter={1}]", method, pi.Name)); } } else { throw new AnnotationProcessorException(String.Format("Error Invoking Method: Annotation not defined for parameter. [method={0}][parameter={1}]", method, pi.Name)); } } if (values.Count > 0) { return(values); } return(null); }
/// <summary> /// Recursively replace defined variables with properties in the configuration nodes. /// /// </summary> /// <param name="node">Configuration Node.</param> /// <param name="inProps">Scoped properties map</param> /// <param name="replace">Replace variables?</param> private void NodePostLoad(AbstractConfigNode node, Dictionary <string, ConfigValueNode> inProps, bool replace) { Dictionary <string, ConfigValueNode> properties = General.Clone <string, ConfigValueNode>(inProps); if (node.GetType() == typeof(ConfigPathNode)) { ConfigPathNode pnode = (ConfigPathNode)node; ConfigPropertiesNode props = pnode.GetProperties(); if (props != null && !props.IsEmpty()) { Dictionary <string, ConfigValueNode> pd = props.GetValues(); foreach (string key in pd.Keys) { if (!properties.ContainsKey(key)) { properties.Add(key, pd[key]); } else { properties[key] = pd[key]; } } } if (!pnode.IsEmpty()) { foreach (string key in pnode.GetChildren().Keys) { NodePostLoad(pnode.GetChildren()[key], properties, replace); } } } else { if (node.GetType() == typeof(ConfigParametersNode)) { ConfigParametersNode pnode = (ConfigParametersNode)node; if (!pnode.IsEmpty() && replace) { foreach (string key in pnode.GetValues().Keys) { ConfigValueNode vn = pnode.GetValue(key); string value = vn.GetValue(); if (!String.IsNullOrWhiteSpace(value)) { string nv = ReplaceVariable(value, properties); vn.SetValue(nv); } } } } else if (node.GetType() == typeof(ConfigAttributesNode)) { ConfigAttributesNode pnode = (ConfigAttributesNode)node; if (!pnode.IsEmpty() && replace) { foreach (string key in pnode.GetValues().Keys) { ConfigValueNode vn = pnode.GetValue(key); string value = vn.GetValue(); if (!String.IsNullOrWhiteSpace(value)) { string nv = ReplaceVariable(value, properties); vn.SetValue(nv); } } } } else if (node.GetType() == typeof(ConfigListValueNode)) { ConfigListValueNode pnode = (ConfigListValueNode)node; if (!pnode.IsEmpty() && replace) { foreach (ConfigValueNode vn in pnode.GetValues()) { string value = vn.GetValue(); if (!String.IsNullOrWhiteSpace(value)) { string nv = ReplaceVariable(value, properties); vn.SetValue(nv); } } } } else if (node.GetType() == typeof(ConfigElementListNode)) { ConfigElementListNode pnode = (ConfigElementListNode)node; if (!pnode.IsEmpty()) { foreach (ConfigElementNode vn in pnode.GetValues()) { NodePostLoad(vn, properties, replace); } } } else if (node.GetType() == typeof(ConfigValueNode)) { if (replace) { ConfigValueNode vn = (ConfigValueNode)node; string value = vn.GetValue(); if (!String.IsNullOrWhiteSpace(value)) { string nv = ReplaceVariable(value, properties); vn.SetValue(nv); } } } } }