Exemple #1
0
        private static TomlObject CreateArrayType(ITomlRoot root, IEnumerable e)
        {
            var et = e.GetElementType();

            if (et != null)
            {
                var conv = root.Settings.TryGetToTomlConverter(et);
                if (conv != null)
                {
                    if (conv.CanConvertTo(typeof(TomlValue)))
                    {
                        var values = e.Select((o) => (TomlValue)conv.Convert(root, o, Types.TomlValueType));
                        return(new TomlArray(root, values.ToArray()));
                    }
                    else if (conv.CanConvertTo(typeof(TomlTable)))
                    {
                        return(new TomlTableArray(root, e.Select((o) => (TomlTable)conv.Convert(root, o, Types.TomlTableType))));
                    }
                    else
                    {
                        throw new NotSupportedException(
                                  $"Cannot create array type from enumerable with element type {et.FullName}");
                    }
                }
                else
                {
                    return(new TomlTableArray(root, e.Select((o) =>
                                                             TomlTable.CreateFromComplexObject(root, o, root.Settings.GetTableType(et)))));
                }
            }

            return(new TomlArray(root));
        }
Exemple #2
0
        public static Result <TomlTable> Add(
            this TomlTable table, string key, object obj, TomlTable.TableTypes tableType = TomlTable.TableTypes.Default)
        {
            if (obj is IResult _)
            {
                throw new ArgumentException($"Cannot add factory result object of type '{obj.GetType()}'.");
            }

            return(AddTomlObjectInternal(table, key, TomlTable.CreateFromComplexObject(table.Root, obj, tableType)));
        }
        // Table
        public static TomlTable CreateAttached(
            this TomlObject rootSource, object obj, TomlTable.TableTypes type = TomlTable.TableTypes.Default)
        {
            if (obj is TomlObject to)
            {
                throw new ArgumentException(string.Format(TomlObjectCreateNotAllowed2A, nameof(obj), to.ReadableTypeName));
            }

            var t = TomlTable.CreateFromComplexObject(rootSource.Root, obj, type);

            return(t);
        }
        // Table Array
        public static TomlTableArray CreateAttached <T>(
            this TomlObject rootSource, IEnumerable <T> obj, TomlTable.TableTypes type = TomlTable.TableTypes.Default)
        {
            if (obj is TomlObject tobj)
            {
                throw new ArgumentException(string.Format(TomlObjectCreateNotAllowed2A, nameof(obj), tobj.ReadableTypeName));
            }

            foreach (var it in obj.Select((x, i) => new { Value = x, Index = i }))
            {
                if ((object)it.Value is TomlObject to)
                {
                    throw new InvalidOperationException(
                              $"Element location '{it.Index}' is a TOML object of type '{to.ReadableTypeName}'. " +
                              $"Passing TOML objects to a create method is not allowed.");
                }
            }

            var tables = obj.Select(o => TomlTable.CreateFromComplexObject(rootSource.Root, o, type));

            return(new TomlTableArray(rootSource.Root, tables));
        }
Exemple #5
0
        private static TomlObject CreateFrom(ITomlRoot root, object val, PropertyInfo pi, Type valueType)
        {
            var t         = valueType;
            var converter = root.Settings.TryGetToTomlConverter(t);

            if (converter != null)
            {
                return((TomlObject)converter.Convert(root, val, Types.TomlObjectType));
            }
            else if (val as IDictionary != null)
            {
                return(TomlTable.CreateFromDictionary(root, (IDictionary)val, root.Settings.GetTableType(t)));
            }
            else if (t != Types.StringType && (val as IEnumerable) != null)
            {
                return(CreateArrayType(root, (IEnumerable)val));
            }
            else
            {
                var tableType = root.Settings.GetTableType(t);
                return(TomlTable.CreateFromComplexObject(root, val, tableType));
            }
        }
Exemple #6
0
 public static TomlTable Add(
     this TomlTable table, string key, object obj, TomlTable.TableTypes tableType = TomlTable.TableTypes.Default)
 {
     return(AddTomlObjectInternal(table, key, TomlTable.CreateFromComplexObject(table.Root, obj, tableType)));
 }