예제 #1
0
 public static bool GetBoolean(this HoconImmutableElement element, string path)
 {
     if (!(element is HoconImmutableObject obj))
     {
         throw new HoconException(
                   $"Path value getter can only work on {nameof(HoconImmutableObject)} type. {element.GetType()} found instead.");
     }
     return(obj[path]);
 }
예제 #2
0
        /// <summary>
        ///     Retrieves the long value, optionally suffixed with a 'b', from this <see cref="HoconValue" />.
        /// </summary>
        /// <returns>The long value represented by this <see cref="HoconValue" />.</returns>
        public static long?GetByteSize(this HoconImmutableElement element)
        {
            if (!(element is HoconImmutableLiteral lit))
            {
                throw new HoconException(
                          $"Value getter functions can only work on {nameof(HoconImmutableLiteral)} type. {element.GetType()} found instead.");
            }

            var res = lit.Value;

            if (string.IsNullOrEmpty(res))
            {
                return(null);
            }
            res = res.Trim();
            var index = res.LastIndexOfAny(Digits);

            if (index == -1 || index + 1 >= res.Length)
            {
                return(long.Parse(res));
            }

            var value = res.Substring(0, index + 1);
            var unit  = res.Substring(index + 1).Trim();

            foreach (var byteSize in ByteSizes)
            {
                if (byteSize.Suffixes.Any(suffix => string.Equals(unit, suffix, StringComparison.Ordinal)))
                {
                    return((long)(byteSize.Factor * double.Parse(value)));
                }
            }

            throw new FormatException($"{unit} is not a valid byte size suffix");
        }
예제 #3
0
        public static IList <int> GetIntList(this HoconImmutableElement element, string path)
        {
            if (!(element is HoconImmutableObject obj))
            {
                throw new HoconException(
                          $"Path list getter can only work on {nameof(HoconImmutableObject)} type. {element.GetType()} found instead.");
            }

            return(obj[path].ToIntArray());
        }
예제 #4
0
 public static string GetString(this HoconImmutableElement element)
 {
     if (!(element is HoconImmutableLiteral lit))
     {
         throw new HoconException(
                   $"Value getter can only work on {nameof(HoconImmutableLiteral)} type. {element.GetType()} found instead.");
     }
     return(lit.Value);
 }
예제 #5
0
        /// <summary>
        ///     Retrieves a list of objects from this <see cref="HoconImmutableElement" />.
        /// </summary>
        /// <returns>A list of objects represented by this <see cref="HoconImmutableElement" />.</returns>
        public static HoconImmutableObject[] ToObjectArray(this HoconImmutableElement element)
        {
            if (element is HoconImmutableObject obj)
            {
                return(obj.ToArray().ToObjectArray());
            }

            if (!(element is HoconImmutableArray arr))
            {
                throw new HoconException(
                          $"Array getter functions can only work on {nameof(HoconImmutableArray)} type. {element.GetType()} found instead.");
            }

            return(arr.Cast <HoconImmutableObject>().ToArray());
        }