コード例 #1
0
        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()}");
            }
        }
コード例 #2
0
        public static bool IsLiteral(this HoconType hoconType)
        {
            switch (hoconType)
            {
            case HoconType.Boolean:
            case HoconType.Number:
            case HoconType.String:
                return(true);

            default:
                return(false);
            }
        }
コード例 #3
0
ファイル: HoconValue.cs プロジェクト: nagytech/HOCON
        /// <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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: HoconValue.cs プロジェクト: nagytech/HOCON
 public new void Clear()
 {
     Type = HoconType.Empty;
     base.Clear();
 }
コード例 #6
0
 public static bool IsMergeable(this HoconType t1, HoconType t2)
 {
     return(t1 == t2 || t1.IsLiteral() && t2.IsLiteral());
 }