<commit_before>﻿// Copyright (c) 2013-2016 <NAME>, MIT License.
// https://github.com/cemdervis/SharpConfig

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace SharpConfig
{
    /// <summary>
    /// Represents a group of <see cref="Setting"/> objects.
    /// </summary>
    public sealed class Section : ConfigurationElement, IEnumerable<Setting>
    {
        private List<Setting> mSettings;

        /// <summary>
        /// Initializes a new instance of the <see cref="Section"/> class.
        /// </summary>
        ///
        /// <param name="name">The name of the section.</param>
        public Section(string name)
            : base(name)
        {
            mSettings = new List<Setting>();
        }

        /// <summary>
        /// Creates a new instance of the <see cref="Section"/> class that is
        /// based on an existing object.
        /// Important: the section is built only from the public getter properties
        /// and fields of its type.
        /// When this method is called, all of those properties will be called
        /// and fields accessed once to obtain their values.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// <param name="name">The name of the section.</param>
        /// <param name="obj"></param>
        /// <returns>The newly created section.</returns>
        public static Section FromObject(string name, object obj)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("The section name must not be null or empty.", "name");

            if (obj == null)
                throw new ArgumentNullException("obj", "obj must not be null.");

            var section = new Section(name);
            var type = obj.GetType();

            foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (!prop.CanRead || ShouldIgnoreMappingFor(prop))
                {
                    // Skip this property, as it can't be read from.
                    continue;
                }

                Setting setting = new Setting(prop.Name, prop.GetValue(obj, null));
                section.mSettings.Add(setting);
            }

            // Repeat for each public field.
            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                if (ShouldIgnoreMappingFor(field))
                {
                    // Skip this field.
                    continue;
                }

                Setting setting = new Setting(field.Name, field.GetValue(obj));
                section.mSettings.Add(setting);
            }

            return section;
        }

        /// <summary>
        /// Creates an object of a specific type, and maps the settings
        /// in this section to the public properties and writable fields of the object.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// 
        /// <typeparam name="T">
        /// The type of object to create.
        /// Note: the type must be default-constructible, meaning it has a public default constructor.
        /// </typeparam>
        /// 
        /// <returns>The created object.</returns>
        /// 
        /// <remarks>
        /// The specified type must have a public default constructor
        /// in order to be created.
        /// </remarks>
        public T ToObject<T>() where T : new()
        {
            var obj = Activator.CreateInstance<T>();
            SetValuesTo(obj);
            return obj;
        }

        /// <summary>
        /// Creates an object of a specific type, and maps the settings
        /// in this section to the public properties and writable fields of the object.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// 
        /// <param name="type">
        /// The type of object to create.
        /// Note: the type must be default-constructible, meaning it has a public default constructor.
        /// </param>
        /// 
        /// <returns>The created object.</returns>
        /// 
        /// <remarks>
        /// The specified type must have a public default constructor
        /// in order to be created.
        /// </remarks>
        public object ToObject(Type type)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            var obj = Activator.CreateInstance(type);
            SetValuesTo(obj);
            return obj;
        }

        /// <summary>
        /// Assigns the values of an object's public properties and fields to the corresponding
        /// <b>already existing</b> settings in this section.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// 
        /// <param name="obj">The object from which the values are obtained.</param>
        public void GetValuesFrom(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            var type = obj.GetType();

            // Scan the type's properties.
            foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (!prop.CanRead || ShouldIgnoreMappingFor(prop))
                    continue;

                var setting = FindSetting(prop.Name);
                if (setting != null)
                {
                    object value = prop.GetValue(obj, null);
                    setting.SetValue(value);
                }
            }

            // Scan the type's fields.
            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                // Skip readonly fields.
                if (ShouldIgnoreMappingFor(field))
                    continue;

                var setting = FindSetting(field.Name);
                if (setting != null)
                {
                    object value = field.GetValue(obj);
                    setting.SetValue(value);
                }
            }
        }

        /// <summary>
        /// Assigns the values of this section to an object's public properties and fields.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute<commit_msg>Fix wrong comment in Section.GetValuesFrom()<commit_after>﻿// Copyright (c) 2013-2016 <NAME>, MIT License.
// https://github.com/cemdervis/SharpConfig

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace SharpConfig
{
    /// <summary>
    /// Represents a group of <see cref="Setting"/> objects.
    /// </summary>
    public sealed class Section : ConfigurationElement, IEnumerable<Setting>
    {
        private List<Setting> mSettings;

        /// <summary>
        /// Initializes a new instance of the <see cref="Section"/> class.
        /// </summary>
        ///
        /// <param name="name">The name of the section.</param>
        public Section(string name)
            : base(name)
        {
            mSettings = new List<Setting>();
        }

        /// <summary>
        /// Creates a new instance of the <see cref="Section"/> class that is
        /// based on an existing object.
        /// Important: the section is built only from the public getter properties
        /// and fields of its type.
        /// When this method is called, all of those properties will be called
        /// and fields accessed once to obtain their values.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// <param name="name">The name of the section.</param>
        /// <param name="obj"></param>
        /// <returns>The newly created section.</returns>
        public static Section FromObject(string name, object obj)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("The section name must not be null or empty.", "name");

            if (obj == null)
                throw new ArgumentNullException("obj", "obj must not be null.");

            var section = new Section(name);
            var type = obj.GetType();

            foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (!prop.CanRead || ShouldIgnoreMappingFor(prop))
                {
                    // Skip this property, as it can't be read from.
                    continue;
                }

                Setting setting = new Setting(prop.Name, prop.GetValue(obj, null));
                section.mSettings.Add(setting);
            }

            // Repeat for each public field.
            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                if (ShouldIgnoreMappingFor(field))
                {
                    // Skip this field.
                    continue;
                }

                Setting setting = new Setting(field.Name, field.GetValue(obj));
                section.mSettings.Add(setting);
            }

            return section;
        }

        /// <summary>
        /// Creates an object of a specific type, and maps the settings
        /// in this section to the public properties and writable fields of the object.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// 
        /// <typeparam name="T">
        /// The type of object to create.
        /// Note: the type must be default-constructible, meaning it has a public default constructor.
        /// </typeparam>
        /// 
        /// <returns>The created object.</returns>
        /// 
        /// <remarks>
        /// The specified type must have a public default constructor
        /// in order to be created.
        /// </remarks>
        public T ToObject<T>() where T : new()
        {
            var obj = Activator.CreateInstance<T>();
            SetValuesTo(obj);
            return obj;
        }

        /// <summary>
        /// Creates an object of a specific type, and maps the settings
        /// in this section to the public properties and writable fields of the object.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// 
        /// <param name="type">
        /// The type of object to create.
        /// Note: the type must be default-constructible, meaning it has a public default constructor.
        /// </param>
        /// 
        /// <returns>The created object.</returns>
        /// 
        /// <remarks>
        /// The specified type must have a public default constructor
        /// in order to be created.
        /// </remarks>
        public object ToObject(Type type)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            var obj = Activator.CreateInstance(type);
            SetValuesTo(obj);
            return obj;
        }

        /// <summary>
        /// Assigns the values of an object's public properties and fields to the corresponding
        /// <b>already existing</b> settings in this section.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
        /// or are of a type that is marked with that attribute, are ignored.
        /// </summary>
        /// 
        /// <param name="obj">The object from which the values are obtained.</param>
        public void GetValuesFrom(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            var type = obj.GetType();

            // Scan the type's properties.
            foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (!prop.CanRead || ShouldIgnoreMappingFor(prop))
                    continue;

                var setting = FindSetting(prop.Name);
                if (setting != null)
                {
                    object value = prop.GetValue(obj, null);
                    setting.SetValue(value);
                }
            }

            // Scan the type's fields.
            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                if (ShouldIgnoreMappingFor(field))
                    continue;

                var setting = FindSetting(field.Name);
                if (setting != null)
                {
                    object value = field.GetValue(obj);
                    setting.SetValue(value);
                }
            }
        }

        /// <summary>
        /// Assigns the values of this section to an object's public properties and fields.
        /// Properties and fields that are marked with the <see cref="IgnoreAttribute"/> attribute
{'fraction_non_alphanumeric': 0.10430247718383312, 'fraction_numerical': 0.0011589164131537012, 'mean_word_length': 1.551654038070597, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 105, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1421155', 'n_tokens_mistral': 3292, 'n_tokens_neox': 3134, 'n_words': 1186}