internal void ResolveValue(HoconSubstitution child) { var index = IndexOf(child); Remove(child); if (child.Type != HoconType.Empty) { if (Type == HoconType.Empty) { Type = child.Type; } else if (!Type.IsMergeable(child.Type)) { throw HoconParserException.Create(child, child.Path, "Invalid substitution, substituted type be must be mergeable with its sibling type. " + $"Sibling type:{Type}, substitution type:{child.Type}"); } var clonedValue = (HoconValue)child.ResolvedValue.Clone(Parent); switch (Type) { case HoconType.Object: Insert(index, clonedValue.GetObject()); break; case HoconType.Array: var hoconArray = new HoconArray(this); hoconArray.AddRange(clonedValue.GetArray()); Insert(index, hoconArray); break; case HoconType.Boolean: case HoconType.Number: case HoconType.String: var elementList = new List <IHoconElement>(); foreach (var element in clonedValue) { elementList.Add(element); } InsertRange(index, elementList); break; } } switch (Parent) { case HoconField v: v.ResolveValue(this); break; case HoconArray a: a.ResolveValue(child); break; default: throw new Exception($"Invalid parent type while resolving substitution:{Parent.GetType()}"); } }
public static bool IsLiteral(this HoconType hoconType) { switch (hoconType) { case HoconType.Boolean: case HoconType.Number: case HoconType.String: return(true); default: return(false); } }
/// <summary> /// Merge an <see cref="IHoconElement"/> into this <see cref="HoconValue"/>. /// </summary> /// <param name="value">The <see cref="IHoconElement"/> value to be merged into this <see cref="HoconValue"/></param> /// <exception cref="HoconParserException"> /// Throws when the merged <see cref="IHoconElement.Type"/> type did not match <see cref="HoconValue.Type"/>, /// if <see cref="HoconValue.Type"/> is not <see cref="HoconType.Empty"/>. /// </exception> public new virtual void Add(IHoconElement value) { if (Type == HoconType.Empty) { Type = value.Type; } else { if (!value.IsSubstitution() && Type != value.Type) { throw new HoconException($"Hocon value merge mismatch. Existing value: {Type}, merged item: {value.Type}"); } } base.Add(value); }
public new void Add(HoconValue value) { if (value.Type != HoconType.Empty) { if (_arrayType == HoconType.Empty) { _arrayType = value.Type; } else if (!value.Type.IsMergeable(_arrayType)) { throw new HoconException( $"Array value must match the rest of the array type or empty. Array value type: {_arrayType}, inserted type: {value.Type}"); } } base.Add(value); }
public new void Clear() { Type = HoconType.Empty; base.Clear(); }
public static bool IsMergeable(this HoconType t1, HoconType t2) { return(t1 == t2 || t1.IsLiteral() && t2.IsLiteral()); }