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]); }
/// <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"); }
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()); }
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); }
/// <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()); }