<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using FullSerializer.Internal;

namespace FullSerializer {
    /// <summary>
    /// MetaType contains metadata about a type. This is used by the reflection serializer.
    /// </summary>
    public class fsMetaType {
        private static Dictionary<fsConfig, Dictionary<Type, fsMetaType>> _configMetaTypes =
            new Dictionary<fsConfig, Dictionary<Type, fsMetaType>>();

        public static fsMetaType Get(fsConfig config, Type type) {
            Dictionary<Type, fsMetaType> metaTypes;
            if (_configMetaTypes.TryGetValue(config, out metaTypes) == false)
                metaTypes = _configMetaTypes[config] = new Dictionary<Type, fsMetaType>();

            fsMetaType metaType;
            if (metaTypes.TryGetValue(type, out metaType) == false) {
                metaType = new fsMetaType(config, type);
                metaTypes[type] = metaType;
            }

            return metaType;
        }

        /// <summary>
        /// Clears out the cached type results. Useful if some prior assumptions become invalid, ie, the default member
        /// serialization mode.
        /// </summary>
        public static void ClearCache() {
            _configMetaTypes = new Dictionary<fsConfig, Dictionary<Type, fsMetaType>>();
        }

        private fsMetaType(fsConfig config, Type reflectedType) {
            ReflectedType = reflectedType;

            List<fsMetaProperty> properties = new List<fsMetaProperty>();
            CollectProperties(config, properties, reflectedType);
            Properties = properties.ToArray();
        }

        public Type ReflectedType;

        private static void CollectProperties(fsConfig config, List<fsMetaProperty> properties, Type reflectedType) {
            // do we require a [SerializeField] or [fsProperty] attribute?
            bool requireOptIn = config.DefaultMemberSerialization == fsMemberSerialization.OptIn;
            bool requireOptOut = config.DefaultMemberSerialization == fsMemberSerialization.OptOut;

            fsObjectAttribute attr = fsPortableReflection.GetAttribute<fsObjectAttribute>(reflectedType);
            if (attr != null) {
                requireOptIn = attr.MemberSerialization == fsMemberSerialization.OptIn;
                requireOptOut = attr.MemberSerialization == fsMemberSerialization.OptOut;
            }

            MemberInfo[] members = reflectedType.GetDeclaredMembers();
            foreach (var member in members) {
                // We don't serialize members annotated with any of the ignore serialize attributes
                if (config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {
                    continue;
                }

                PropertyInfo property = member as PropertyInfo;
                FieldInfo field = member as FieldInfo;

                // If an opt-in annotation is required, then skip the property if it doesn't have one
                // of the serialize attributes
                if (requireOptIn &&
                    !config.SerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {

                    continue;<commit_msg>Early-out of serializable check if member is neither property nor field<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using FullSerializer.Internal;

namespace FullSerializer {
    /// <summary>
    /// MetaType contains metadata about a type. This is used by the reflection serializer.
    /// </summary>
    public class fsMetaType {
        private static Dictionary<fsConfig, Dictionary<Type, fsMetaType>> _configMetaTypes =
            new Dictionary<fsConfig, Dictionary<Type, fsMetaType>>();

        public static fsMetaType Get(fsConfig config, Type type) {
            Dictionary<Type, fsMetaType> metaTypes;
            if (_configMetaTypes.TryGetValue(config, out metaTypes) == false)
                metaTypes = _configMetaTypes[config] = new Dictionary<Type, fsMetaType>();

            fsMetaType metaType;
            if (metaTypes.TryGetValue(type, out metaType) == false) {
                metaType = new fsMetaType(config, type);
                metaTypes[type] = metaType;
            }

            return metaType;
        }

        /// <summary>
        /// Clears out the cached type results. Useful if some prior assumptions become invalid, ie, the default member
        /// serialization mode.
        /// </summary>
        public static void ClearCache() {
            _configMetaTypes = new Dictionary<fsConfig, Dictionary<Type, fsMetaType>>();
        }

        private fsMetaType(fsConfig config, Type reflectedType) {
            ReflectedType = reflectedType;

            List<fsMetaProperty> properties = new List<fsMetaProperty>();
            CollectProperties(config, properties, reflectedType);
            Properties = properties.ToArray();
        }

        public Type ReflectedType;

        private static void CollectProperties(fsConfig config, List<fsMetaProperty> properties, Type reflectedType) {
            // do we require a [SerializeField] or [fsProperty] attribute?
            bool requireOptIn = config.DefaultMemberSerialization == fsMemberSerialization.OptIn;
            bool requireOptOut = config.DefaultMemberSerialization == fsMemberSerialization.OptOut;

            fsObjectAttribute attr = fsPortableReflection.GetAttribute<fsObjectAttribute>(reflectedType);
            if (attr != null) {
                requireOptIn = attr.MemberSerialization == fsMemberSerialization.OptIn;
                requireOptOut = attr.MemberSerialization == fsMemberSerialization.OptOut;
            }

            MemberInfo[] members = reflectedType.GetDeclaredMembers();
            foreach (var member in members) {
                // We don't serialize members annotated with any of the ignore serialize attributes
                if (config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {
                    continue;
                }

                PropertyInfo property = member as PropertyInfo;
                FieldInfo field = member as FieldInfo;

                // Early out if it's neither a field or a property, since we don't serialize anything else.
                if (property == null && field == null) {
                    continue;
                }

                // If an opt-in annotation is required, then skip the property if it doesn't have one
                // of the serialize attributes
                if (requireOptIn &&
                    !config.SerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {

                    continue;
{'fraction_non_alphanumeric': 0.08002319512902291, 'fraction_numerical': 0.0, 'mean_word_length': 2.3264223722275794, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, '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': '525788', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1568, 'n_words': 477}