Exemplo n.º 1
0
        protected void CreateDocumentation(TomlObject tomlObject)
        {
            var docs = tomlObject.Comments.Where(x => x.Text.StartsWith("#")).ToArray();

            tomlObject.ClearComments();
            if (!string.IsNullOrEmpty(Documentation))
            {
                tomlObject.AddComment(Documentation);
            }
            if (docs.Length > 0)
            {
                tomlObject.AddComments(docs);
            }
        }
Exemplo n.º 2
0
        // *** Convenience method for setting values to a toml object. ***

        public static TomlObject Set <T>(this TomlTable tomlTable, string key, T value)
        {
            if (tomlTable is null)
            {
                throw new ArgumentNullException(nameof(tomlTable));
            }
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            // I literally have no idea how to write it better with this toml library.

            // Note for TimeSpan: since TimeSpan as Nett (de)serializes it is not standartized we have to cast it manually

            TomlObject retobj = tomlTable.TryGetValue(key);

            if (retobj is null)
            {
                if (typeof(T) == typeof(bool))
                {
                    return(tomlTable.Add(key, (bool)(object)value).Added);
                }
                else if (typeof(T) == typeof(string))
                {
                    return(tomlTable.Add(key, (string)(object)value).Added);
                }
                else if (typeof(T) == typeof(double))
                {
                    return(tomlTable.Add(key, (double)(object)value).Added);
                }
                else if (typeof(T) == typeof(float))
                {
                    return(tomlTable.Add(key, (float)(object)value).Added);
                }
                else if (typeof(T) == typeof(ushort))
                {
                    return(tomlTable.Add(key, /*auto*/ (ushort)(object)value).Added);
                }
                else if (typeof(T) == typeof(int))
                {
                    return(tomlTable.Add(key, (int)(object)value).Added);
                }
                else if (typeof(T) == typeof(long))
                {
                    return(tomlTable.Add(key, (long)(object)value).Added);
                }
                else if (typeof(T) == typeof(ulong))
                {
                    return(tomlTable.Add(key, (long)(ulong)(object)value).Added);
                }
                else if (typeof(T) == typeof(TimeSpan))
                {
                    return(tomlTable.Add(key, SerializeTime((TimeSpan)(object)value)).Added);
                }
                else if (typeof(T) == typeof(DateTime))
                {
                    return(tomlTable.Add(key, (DateTime)(object)value).Added);
                }
                else if (typeof(T).IsEnum)
                {
                    return(tomlTable.Add(key, value.ToString()).Added);
                }
                else if (value is IEnumerable <bool> enubool)
                {
                    return(tomlTable.Add(key, enubool).Added);
                }
                else if (value is IEnumerable <string> enustring)
                {
                    return(tomlTable.Add(key, enustring).Added);
                }
                else if (value is IEnumerable <double> enudouble)
                {
                    return(tomlTable.Add(key, enudouble).Added);
                }
                else if (value is IEnumerable <float> enufloat)
                {
                    return(tomlTable.Add(key, enufloat).Added);
                }
                else if (value is IEnumerable <ushort> enuushort)
                {
                    return(tomlTable.Add(key, enuushort.Select(x => (int)x)).Added);
                }
                else if (value is IEnumerable <int> enuint)
                {
                    return(tomlTable.Add(key, enuint).Added);
                }
                else if (value is IEnumerable <long> enulong)
                {
                    return(tomlTable.Add(key, enulong).Added);
                }
                else if (value is IEnumerable <ulong> enuulong)
                {
                    return(tomlTable.Add(key, enuulong.Select(x => (long)x)).Added);
                }
                else if (value is IEnumerable <TimeSpan> enuTimeSpan)
                {
                    return(tomlTable.Add(key, enuTimeSpan.Select(SerializeTime)).Added);
                }
                else if (value is IEnumerable <DateTime> enuDateTime)
                {
                    return(tomlTable.Add(key, enuDateTime).Added);
                }
            }
            else
            {
                TomlComment[] docs = null;
                if (retobj.Comments.Any())
                {
                    docs = retobj.Comments.ToArray();
                }
                if (typeof(T) == typeof(bool))
                {
                    retobj = tomlTable.Update(key, (bool)(object)value).Added;
                }
                else if (typeof(T) == typeof(string))
                {
                    retobj = tomlTable.Update(key, (string)(object)value).Added;
                }
                else if (typeof(T) == typeof(double))
                {
                    retobj = tomlTable.Update(key, (double)(object)value).Added;
                }
                else if (typeof(T) == typeof(float))
                {
                    retobj = tomlTable.Update(key, (float)(object)value).Added;
                }
                else if (typeof(T) == typeof(ushort))
                {
                    retobj = tomlTable.Update(key, /*auto*/ (ushort)(object)value).Added;
                }
                else if (typeof(T) == typeof(int))
                {
                    retobj = tomlTable.Update(key, /*auto*/ (int)(object)value).Added;
                }
                else if (typeof(T) == typeof(long))
                {
                    retobj = tomlTable.Update(key, (long)(object)value).Added;
                }
                else if (typeof(T) == typeof(ulong))
                {
                    retobj = tomlTable.Update(key, (long)(ulong)(object)value).Added;
                }
                else if (typeof(T) == typeof(TimeSpan))
                {
                    retobj = tomlTable.Update(key, SerializeTime((TimeSpan)(object)value)).Added;
                }
                else if (typeof(T) == typeof(DateTime))
                {
                    retobj = tomlTable.Update(key, (DateTime)(object)value).Added;
                }
                else if (typeof(T).IsEnum)
                {
                    retobj = tomlTable.Update(key, value.ToString()).Added;
                }
                else if (value is IEnumerable <bool> enubool)
                {
                    return(tomlTable.Update(key, enubool).Added);
                }
                else if (value is IEnumerable <string> enustring)
                {
                    return(tomlTable.Update(key, enustring).Added);
                }
                else if (value is IEnumerable <double> enudouble)
                {
                    return(tomlTable.Update(key, enudouble).Added);
                }
                else if (value is IEnumerable <float> enufloat)
                {
                    return(tomlTable.Update(key, enufloat).Added);
                }
                else if (value is IEnumerable <ushort> enuushort)
                {
                    return(tomlTable.Update(key, enuushort.Select(x => (int)x)).Added);
                }
                else if (value is IEnumerable <int> enuint)
                {
                    return(tomlTable.Update(key, enuint).Added);
                }
                else if (value is IEnumerable <long> enulong)
                {
                    return(tomlTable.Update(key, enulong).Added);
                }
                else if (value is IEnumerable <ulong> enuulong)
                {
                    return(tomlTable.Update(key, enuulong.Select(x => (long)x)).Added);
                }
                else if (value is IEnumerable <TimeSpan> enuTimeSpan)
                {
                    return(tomlTable.Update(key, enuTimeSpan.Select(SerializeTime)).Added);
                }
                else if (value is IEnumerable <DateTime> enuDateTime)
                {
                    return(tomlTable.Update(key, enuDateTime).Added);
                }
                else
                {
                    throw new NotSupportedException("The type is not supported");
                }
                if (docs != null)
                {
                    retobj.AddComments(docs);
                }
                return(retobj);
            }
            throw new NotSupportedException("The type is not supported");
        }