<commit_before>﻿using System;
using System.Globalization;

namespace Orchard.ContentManagement.FieldStorage {
    public class SimpleFieldStorage : IFieldStorage {
        public SimpleFieldStorage(Func<string, Type, string> getter, Action<string, Type, string> setter) {
            Getter = getter;
            Setter = setter;
        }

        public Func<string, Type, string> Getter { get; set; }
        public Action<string, Type, string> Setter { get; set; }

        public T Get<T>(string name) {
            var value = Getter(name, typeof(T));
            return string.IsNullOrEmpty(value)
                       ? default(T)
                       : (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
        }

        public void Set<T>(string name, T value) {
            Setter(name, typeof(T), Convert.ToString(value, CultureInfo.InvariantCulture));
        }
    }
}<commit_msg>Fixing Nullable types storage for fields<commit_after>﻿using System;
using System.Globalization;

namespace Orchard.ContentManagement.FieldStorage {
    public class SimpleFieldStorage : IFieldStorage {
        public SimpleFieldStorage(Func<string, Type, string> getter, Action<string, Type, string> setter) {
            Getter = getter;
            Setter = setter;
        }

        public Func<string, Type, string> Getter { get; set; }
        public Action<string, Type, string> Setter { get; set; }

        public T Get<T>(string name) {
            var value = Getter(name, typeof(T));
            if(String.IsNullOrEmpty(value)) {
                return default(T);
            }

            var t = typeof (T);

            // the T is nullable, convert using underlying type
            if(t.GetGenericTypeDefinition() == typeof(Nullable<>)) {
                t = Nullable.GetUnderlyingType(t);
            }

            return (T)Convert.ChangeType(value, t, CultureInfo.InvariantCulture);
        }

        public void Set<T>(string name, T value) {
            Setter(name, typeof(T), Convert.ToString(value, CultureInfo.InvariantCulture));
        }
    }
}
{'fraction_non_alphanumeric': 0.10486177311725453, 'fraction_numerical': 0.0, 'mean_word_length': 2.310725552050473, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '6345608', 'n_tokens_mistral': 581, 'n_tokens_neox': 562, 'n_words': 144}