Beispiel #1
0
        public void SearchIndex()
        {
            try
            {
                Configuration configuration = ReadConfiguration();

                Assert.NotNull(configuration);
                string             path = "root/configuration/node_1/ELEMENT_LIST";
                AbstractConfigNode node = configuration.Find(path);
                Assert.NotNull(node);
                Assert.Equal(path, node.GetSearchPath());

                path = "ELEMENT_LIST[3]";
                AbstractConfigNode nnode = node.Find(path);
                Assert.NotNull(nnode);
                Assert.True(nnode.GetType() == typeof(ConfigPathNode));
                LogUtils.Debug(nnode.GetAbsolutePath());

                path = "ELEMENT_LIST[2]/string_2";
                node = node.Find(path);
                Assert.NotNull(node);
                Assert.True(node.GetType() == typeof(ConfigValueNode));
                LogUtils.Debug(node.GetAbsolutePath());
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex);
                throw ex;
            }
        }
Beispiel #2
0
        public void SearchParameters()
        {
            try
            {
                Configuration configuration = ReadConfiguration();

                Assert.NotNull(configuration);
                string             path = "root/configuration/node_1/node_2#PARAM_3";
                AbstractConfigNode node = configuration.Find(path);
                Assert.NotNull(node);
                Assert.True(node is ConfigValueNode);
                string value = ((ConfigValueNode)node).GetDecryptedValue();
                Assert.Equal("TEST-PARAM-3", value);
                LogUtils.Debug(String.Format("[{0}:{1}]", node.GetSearchPath(), ((ConfigValueNode)node).GetValue()));
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex);
                throw ex;
            }
        }
Beispiel #3
0
        public void SearchParent()
        {
            try
            {
                Configuration configuration = ReadConfiguration();

                Assert.NotNull(configuration);
                string             path = "root/configuration/node_1/ELEMENT_LIST";
                AbstractConfigNode node = configuration.Find(path);
                Assert.NotNull(node);
                Assert.Equal(path, node.GetSearchPath());
                path = "../node_2/node_3/../#";
                node = node.Find(path);
                Assert.NotNull(node);
                Assert.True(node.GetType() == typeof(ConfigParametersNode));
                LogUtils.Debug(node.GetAbsolutePath());
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex);
                throw ex;
            }
        }
Beispiel #4
0
        public void Search()
        {
            try
            {
                Configuration configuration = ReadConfiguration();

                Assert.NotNull(configuration);
                string             path = "root/configuration/node_1";
                AbstractConfigNode node = configuration.Find(path);
                Assert.NotNull(node);
                Assert.Equal(path, node.GetSearchPath());
                path = "VALUE_LIST";
                node = node.Find(path);
                Assert.True(node.GetType() == typeof(ConfigListValueNode));
                Assert.Equal(8, ((ConfigListValueNode)node).Count());
                Assert.Equal(path, node.Name);
                LogUtils.Debug(node.GetAbsolutePath());
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex);
                throw ex;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Process the annotated field and set the values from the configuration value.
        /// </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="configValue">Config value annotation</param>
        /// <returns>Updated Target Type instance.</returns>
        private static T ProcessField <T>(ConfigPathNode node, T target, FieldInfo field, ConfigValue configValue, List <string> valuePaths)
        {
            string pname = configValue.Name;

            if (String.IsNullOrWhiteSpace(pname))
            {
                pname = field.Name;
            }

            string value = null;

            if (!String.IsNullOrWhiteSpace(configValue.Path))
            {
                AbstractConfigNode nnode = node.Find(configValue.Path);
                if (nnode != null && nnode.GetType() == typeof(ConfigPathNode))
                {
                    node = (ConfigPathNode)nnode;
                }
                else
                {
                    node = null;
                }
            }

            if (node != null)
            {
                AbstractConfigNode cnode = node.GetChildNode(pname);
                if (cnode != null)
                {
                    if (valuePaths != null)
                    {
                        valuePaths.Add(cnode.GetSearchPath());
                    }
                    if (cnode.GetType() == typeof(ConfigValueNode))
                    {
                        ConfigValueNode vn = (ConfigValueNode)cnode;
                        if (vn != null)
                        {
                            value = vn.GetValue();
                        }
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            object v = GetValue <T>(pname, value, configValue.Function, field.FieldType, target, configValue.Required);
                            if (v != null)
                            {
                                TypeUtils.CallSetter(field, target, v);
                            }
                        }
                    }
                    else
                    {
                        if (ReflectionUtils.IsSubclassOfRawGeneric(field.FieldType, typeof(List <>)))
                        {
                            if (cnode.GetType() == typeof(ConfigListValueNode))
                            {
                                ConfigListValueNode configList = (ConfigListValueNode)cnode;
                                List <string>       values     = configList.GetValueList();
                                if (values != null)
                                {
                                    Type   inner = field.FieldType.GetGenericArguments()[0];
                                    object v     = ReflectionUtils.ConvertListFromStrings(inner, values);
                                    if (v != null)
                                    {
                                        TypeUtils.CallSetter(field, target, v);
                                    }
                                }
                            }
                        }
                        else if (ReflectionUtils.IsSubclassOfRawGeneric(field.FieldType, typeof(HashSet <>)))
                        {
                            if (cnode.GetType() == typeof(ConfigListValueNode))
                            {
                                ConfigListValueNode configList = (ConfigListValueNode)cnode;
                                List <string>       values     = configList.GetValueList();
                                if (values != null)
                                {
                                    Type   inner = field.FieldType.GetGenericArguments()[0];
                                    object v     = ReflectionUtils.ConvertSetFromStrings(inner, values);
                                    if (v != null)
                                    {
                                        TypeUtils.CallSetter(field, target, v);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            object ov = TypeUtils.CallGetter(field, target);

            if (ov == null && configValue.Required)
            {
                throw AnnotationProcessorException.Throw(target.GetType(), pname);
            }

            return(target);
        }