Example #1
0
 /// <summary>
 /// Attaches the given metadata value to the given YAML path.
 /// </summary>
 /// <param name="path">The path at which to attach metadata.</param>
 /// <param name="value">The metadata to attach.</param>
 public void Set([NotNull] YamlAssetPath path, T value)
 {
     if (isAttached)
     {
         throw new InvalidOperationException("Cannot modify a YamlAssetMetadata after it has been attached.");
     }
     metadata[path] = value;
 }
Example #2
0
 /// <summary>
 /// Removes attached metadata from the given YAML path.
 /// </summary>
 /// <param name="path">The path at which to remove metadata.</param>
 public void Remove(YamlAssetPath path)
 {
     if (isAttached)
     {
         throw new InvalidOperationException("Cannot modify a YamlAssetMetadata after it has been attached.");
     }
     metadata.Remove(path);
 }
Example #3
0
        public YamlAssetPath Append([CanBeNull] YamlAssetPath other)
        {
            var result = new YamlAssetPath(elements);

            if (other != null)
            {
                result.elements.AddRange(other.elements);
            }
            return(result);
        }
Example #4
0
        public static YamlAssetPath FromMemberPath([NotNull] MemberPath path, object root)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            var result = new YamlAssetPath();
            var clone  = new MemberPath();

            foreach (var item in path.Decompose())
            {
                if (item.MemberDescriptor != null)
                {
                    clone.Push(item.MemberDescriptor);
                    var member = item.MemberDescriptor.Name;
                    result.PushMember(member);
                }
                else
                {
                    object index = null;
                    if (item is MemberPath.ArrayPathItem arrayItem)
                    {
                        clone.Push(arrayItem.Descriptor, arrayItem.Index);
                        index = arrayItem.Index;
                    }
                    if (item is MemberPath.CollectionPathItem collectionItem)
                    {
                        clone.Push(collectionItem.Descriptor, collectionItem.Index);
                        index = collectionItem.Index;
                    }
                    if (item is MemberPath.DictionaryPathItem dictionaryItem)
                    {
                        clone.Push(dictionaryItem.Descriptor, dictionaryItem.Key);
                        index = dictionaryItem.Key;
                    }
                    if (!CollectionItemIdHelper.TryGetCollectionItemIds(clone.GetValue(root), out CollectionItemIdentifiers ids))
                    {
                        result.PushIndex(index);
                    }
                    else
                    {
                        var id = ids[index];
                        // Create a new id if we don't have any so far
                        if (id == ItemId.Empty)
                        {
                            id = ItemId.New();
                        }
                        result.PushItemId(id);
                    }
                }
            }
            return(result);
        }
Example #5
0
        /// <summary>
        /// Indicates whether the current path represents the same path of another object.
        /// </summary>
        /// <param name="other">An object to compare with this path.</param>
        /// <returns><c>true</c> if the current path matches the <paramref name="other"/> parameter; otherwise, <c>false</c>.</returns>
        public bool Match(YamlAssetPath other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }
            if (Elements.Count != other.Elements.Count)
            {
                return(false);
            }

            return(Elements.SequenceEqual(other.Elements));
        }
Example #6
0
        public bool StartsWith([NotNull] YamlAssetPath path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (path.elements.Count > elements.Count)
            {
                return(false);
            }

            for (var i = 0; i < path.Elements.Count; ++i)
            {
                if (!Elements[i].Equals(path.Elements[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #7
0
 /// <inheritdoc/>
 object IYamlAssetMetadata.TryGet(YamlAssetPath path) => TryGet(path);
Example #8
0
 /// <inheritdoc/>
 void IYamlAssetMetadata.Set(YamlAssetPath path, object value) => Set(path, (T)value);
Example #9
0
 /// <summary>
 /// Tries to retrieve the metadata for the given path.
 /// </summary>
 /// <param name="path">The path at which to retrieve metadata.</param>
 /// <returns>The metadata attached to the given path, or the default value of <typeparamref name="T"/> if no metadata is attached at the given path.</returns>
 public T TryGet([NotNull] YamlAssetPath path)
 {
     metadata.TryGetValue(path, out T value);
     return(value);
 }
Example #10
0
        public YamlAssetPath Clone()
        {
            var clone = new YamlAssetPath(elements);

            return(clone);
        }