Exemplo n.º 1
0
 public CollectionConverter(
     CollectionCreator createCollection,
     ITomlSerializerContext context)
 {
     _createCollection = createCollection;
     _itemConverter    = context.GetConverter <TItem>();
 }
Exemplo n.º 2
0
        public TomlObject ConvertTo(object value, ITomlSerializerContext context)
        {
            // TODO: null handling
            var converter = context.GetConverter(value.GetType());

            return(converter.ConvertTo(value, context));
        }
Exemplo n.º 3
0
 public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
 {
     var(instanceType, valueType) = GetTypes(typeToConvert);
     return((ITomlConverter)Activator.CreateInstance(
                typeof(DictionaryConverter <,>).MakeGenericType(valueType, typeToConvert),
                instanceType, context) !);
 }
Exemplo n.º 4
0
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var metadata = GetMetadata(typeToConvert, context);

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(NewTypeConverter <,>).MakeGenericType(typeToConvert, metadata.InnerType),
                       metadata) !);
        }
Exemplo n.º 5
0
        private static NewTypeMetadata GetMetadata(Type typeToConvert, ITomlSerializerContext context)
        {
            var constructor = FindConstructor(typeToConvert);
            var innerType   = constructor.GetParameters()[0].ParameterType;
            var converter   = context.GetConverter(innerType);

            return(new NewTypeMetadata(innerType, converter, constructor, null !));
        }
Exemplo n.º 6
0
        public T ConvertFrom(TomlObject value, ITomlSerializerContext context)
        {
            if (value is TomlTable tomlTable)
            {
                return(ConvertFrom(tomlTable, context));
            }

            throw new TomlException("Objects can only be converted from tables");
        }
Exemplo n.º 7
0
        public int ConvertFrom(TomlObject value, ITomlSerializerContext context)
        {
            if (value is TomlInteger tomlInteger)
            {
                // TODO: what happens if the value overflows?
                return((int)tomlInteger.Value);
            }

            throw new TomlException();
        }
Exemplo n.º 8
0
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var typeToCreate        = context.GetDefaultImplementation(typeToConvert) ?? typeToConvert;
            var constructor         = GetConstructorForDeserialization(typeToCreate);
            var parameterConverters = constructor.GetParameters().Select(p => GetCachedParameterInfo(p, context));

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(ObjectConverter <>).MakeGenericType(typeToConvert),
                       constructor,
                       parameterConverters.ToImmutableList()) !);
        }
Exemplo n.º 9
0
        private TDictionary ConvertFrom(TomlTable table, ITomlSerializerContext context)
        {
            var dictionary = (TDictionary)Activator.CreateInstance(_instanceType) !;

            foreach (var key in table.Keys)
            {
                var value = table.TryGetValue(key, out var dictionaryValue)
                    ? _valueConverter.ConvertFrom(dictionaryValue, context)
                    : throw new InvalidOperationException("This should never happen");
                dictionary[key] = value;
            }

            return(dictionary);
        }
Exemplo n.º 10
0
        public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
        {
            var itemType = IsIEnumerable(typeToConvert)
                ? typeToConvert.GetGenericArguments()[0]
                : typeToConvert.GetInterfaces().Single(IsIEnumerable).GetGenericArguments()[0];

            var typeToCreate = context.GetDefaultImplementation(typeToConvert) ?? typeToConvert;

            var factory = CollectionCreatorFactories.FirstOrDefault(f => f.CanCreate(typeToCreate, itemType))
                          ?? throw new TomlException($"No creator found for collection {typeToCreate}");

            return((ITomlConverter)Activator.CreateInstance(
                       typeof(CollectionConverter <,>).MakeGenericType(typeToConvert, itemType),
                       factory.CreateCollectionCreator(typeToCreate, itemType),
                       context) !);
        }
Exemplo n.º 11
0
        public object ConvertFrom(TomlObject value, ITomlSerializerContext context)
        {
            var converter = value switch
            {
                TomlArray => (ITomlConverter)context.GetConverter <IEnumerable <object> >(),
                TomlString => context.GetConverter <string>(),
                TomlInteger => context.GetConverter <long>(),
                TomlBoolean => context.GetConverter <bool>(),
                TomlFloat => context.GetConverter <double>(),
                TomlDateTime => context.GetConverter <DateTime>(),
                TomlTable => throw new NotImplementedException(),
                      _ => throw new NotSupportedException(),
            };

            return(converter.ConvertFrom(value, context));
        }
Exemplo n.º 12
0
 public TomlObject ConvertTo(TDictionary value, ITomlSerializerContext context) => throw new NotImplementedException();
Exemplo n.º 13
0
 public TDictionary ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => value is TomlTable tomlTable
Exemplo n.º 14
0
 public DictionaryConverter(Type instanceType, ITomlSerializerContext context)
 {
     _valueConverter = context.GetConverter <TValue>();
     _instanceType   = instanceType;
 }
Exemplo n.º 15
0
 public NullableConverter(ITomlSerializerContext context) => _valueConverter = context.GetConverter <T>();
Exemplo n.º 16
0
 public T ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => value is TomlValue <T> tomlValue
Exemplo n.º 17
0
 public TomlObject ConvertTo(T value, ITomlSerializerContext context) => ToTomlObject(value);
Exemplo n.º 18
0
 public Guid ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => Guid.Parse(context.GetConverter <string>().ConvertFrom(value, context));
Exemplo n.º 19
0
 public TNewType ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => _constructor(_innerConverter.ConvertFrom(value, context));
Exemplo n.º 20
0
 public T ConvertFromAbsent <T>(ITomlSerializerContext context) => throw new TomlException();
Exemplo n.º 21
0
 public ITomlConverter CreateConverter(Type optionType, ITomlSerializerContext context)
 => (ITomlConverter)Activator.CreateInstance(
     typeof(OptionConverter <>).MakeGenericType(optionType.GetGenericArguments()),
     context) !;
Exemplo n.º 22
0
 public T ConvertFromAbsent <T>(ITomlSerializerContext context) => default;
Exemplo n.º 23
0
        private static object?ConvertParameter(TomlTable table, CachedParameterInfo parameter, ITomlSerializerContext context)
        {
            var parameterName = parameter.Info.Name ?? throw new TomlException("Constructor parameter without name");
            var propertyName  = context.ConvertPropertyName(parameterName);

            // TODO: support nullable reference types

            return(table.TryGetValue(propertyName, out var tomlObject)
                ? parameter.Converter.ConvertFrom(tomlObject, context)
                : parameter.Converter.ConvertFromAbsent(context));
        }
Exemplo n.º 24
0
        private T ConvertFrom(TomlTable table, ITomlSerializerContext context)
        {
            var parameters = _parameterConverters.Select(p => ConvertParameter(table, p, context)).ToArray();

            return((T)_constructor.Invoke(parameters));
        }
Exemplo n.º 25
0
 public OptionConverter(ITomlSerializerContext context) => _itemConverter = context.GetConverter <TItem>();
Exemplo n.º 26
0
 public ITomlConverter CreateConverter(Type typeToConvert, ITomlSerializerContext context)
 => (ITomlConverter)Activator.CreateInstance(
     typeof(NullableConverter <>).MakeGenericType(typeToConvert.GetGenericArguments()),
     context) !;
Exemplo n.º 27
0
 public TCollection ConvertFromAbsent(ITomlSerializerContext context)
 => (TCollection)_createCollection(Enumerable.Empty <TItem>());
Exemplo n.º 28
0
 public TomlObject ConvertTo(Guid value, ITomlSerializerContext context)
 => new TomlString(value.ToString());
Exemplo n.º 29
0
        private static CachedParameterInfo GetCachedParameterInfo(ParameterInfo info, ITomlSerializerContext context)
        {
            var converter = context.GetConverter(info.ParameterType);

            return(new CachedParameterInfo(info, converter));
        }
Exemplo n.º 30
0
 public TCollection ConvertFrom(TomlObject value, ITomlSerializerContext context)
 => value is TomlArray tomlArray