public static YamlAssetPath ConvertPath(GraphNodePath path, int inNonIdentifiableType)
        {
            var currentNode = (AssetNode)path.RootNode;
            var result      = new YamlAssetPath();
            var i           = 0;

            foreach (var item in path.Path)
            {
                switch (item.Type)
                {
                case GraphNodePath.ElementType.Member:
                    var member = (string)item.Value;
                    result.PushMember(member);
                    currentNode = (AssetNode)((IGraphNode)currentNode).TryGetChild(member);
                    break;

                case GraphNodePath.ElementType.Target:
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (AssetNode)((IGraphNode)currentNode).Target;
                    }
                    break;

                case GraphNodePath.ElementType.Index:
                    var index = (Index)item.Value;
                    if (inNonIdentifiableType > 0 || currentNode.IsNonIdentifiableCollectionContent)
                    {
                        result.PushIndex(index.Value);
                    }
                    else
                    {
                        var id = currentNode.IndexToId(index);
                        // Create a new id if we don't have any so far
                        if (id == ItemId.Empty)
                        {
                            id = ItemId.New();
                        }
                        result.PushItemId(id);
                    }
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (AssetNode)((IGraphNode)currentNode).IndexedTarget(index);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                ++i;
            }
            return(result);
        }
 public static YamlAssetPath ConvertPath(GraphNodePath path, int inNonIdentifiableType)
 {
     var currentNode = (AssetNode)path.RootNode;
     var result = new YamlAssetPath();
     var i = 0;
     foreach (var item in path.Path)
     {
         switch (item.Type)
         {
             case GraphNodePath.ElementType.Member:
                 var member = (string)item.Value;
                 result.PushMember(member);
                 currentNode = (AssetNode)((IGraphNode)currentNode).TryGetChild(member);
                 break;
             case GraphNodePath.ElementType.Target:
                 if (i < path.Path.Count - 1)
                 {
                     currentNode = (AssetNode)((IGraphNode)currentNode).Target;
                 }
                 break;
             case GraphNodePath.ElementType.Index:
                 var index = (Index)item.Value;
                 if (inNonIdentifiableType > 0 || currentNode.IsNonIdentifiableCollectionContent)
                 {
                     result.PushIndex(index.Value);
                 }
                 else
                 {
                     var id = currentNode.IndexToId(index);
                     // Create a new id if we don't have any so far
                     if (id == ItemId.Empty)
                         id = ItemId.New();
                     result.PushItemId(id);
                 }
                 if (i < path.Path.Count - 1)
                 {
                     currentNode = (AssetNode)((IGraphNode)currentNode).IndexedTarget(index);
                 }
                 break;
             default:
                 throw new ArgumentOutOfRangeException();
         }
         ++i;
     }
     return result;
 }
        public override void WriteDictionaryKey(ref ObjectContext objectContext, object key, Type keyType)
        {
            YamlAssetMetadata <OverrideType> overrides;

            if (objectContext.SerializerContext.Properties.TryGetValue(OverrideDictionaryKey, out overrides))
            {
                var           itemPath = GetCurrentPath(ref objectContext, true);
                YamlAssetPath keyPath  = null;
                ItemId        id;
                object        actualKey;
                if (YamlAssetPath.IsCollectionWithIdType(objectContext.Descriptor.Type, key, out id, out actualKey))
                {
                    keyPath = itemPath.Clone();
                    keyPath.PushIndex(actualKey);
                    itemPath.PushItemId(id);
                }
                else
                {
                    itemPath.PushIndex(key);
                }
                var overrideType = overrides.TryGet(itemPath);
                if ((overrideType & OverrideType.New) != 0)
                {
                    objectContext.SerializerContext.Properties.Set(ItemIdSerializerBase.OverrideInfoKey, OverridePostfixes.PostFixNew.ToString());
                }
                if ((overrideType & OverrideType.Sealed) != 0)
                {
                    objectContext.SerializerContext.Properties.Set(ItemIdSerializerBase.OverrideInfoKey, OverridePostfixes.PostFixSealed.ToString());
                }
                if (keyPath != null)
                {
                    overrideType = overrides.TryGet(keyPath);
                    if ((overrideType & OverrideType.New) != 0)
                    {
                        objectContext.SerializerContext.Properties.Set(KeyWithIdSerializer.OverrideKeyInfoKey, OverridePostfixes.PostFixNew.ToString());
                    }
                    if ((overrideType & OverrideType.Sealed) != 0)
                    {
                        objectContext.SerializerContext.Properties.Set(KeyWithIdSerializer.OverrideKeyInfoKey, OverridePostfixes.PostFixSealed.ToString());
                    }
                }
            }
            base.WriteDictionaryKey(ref objectContext, key, keyType);
        }
Beispiel #4
0
        public void TestAbstractNonIdentifiableReferenceableDictionarySerialization()
        {
            var obj  = new NonIdentifiableCollectionContainer();
            var item = new Referenceable {
                Id = GuidGenerator.Get(1), Value = "Test"
            };

            obj.AbstractRefDictionary.Add("Item1", item);
            obj.AbstractRefDictionary.Add("Item2", item);
            var objectReferences = new YamlAssetMetadata <Guid>();
            var path             = new YamlAssetPath();

            path.PushMember(nameof(CollectionContainer.AbstractRefDictionary));
            path.PushIndex("Item1");
            objectReferences.Set(path, GuidGenerator.Get(1));
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(AbstractNonIdentifiableReferenceableDictionaryYaml, yaml);
        }
Beispiel #5
0
        public void TestConcreteNonIdentifiableReferenceableListSerialization()
        {
            var obj  = new NonIdentifiableCollectionContainer();
            var item = new Referenceable {
                Id = GuidGenerator.Get(1), Value = "Test"
            };

            obj.ConcreteRefList.Add(item);
            obj.ConcreteRefList.Add(item);
            var objectReferences = new YamlAssetMetadata <Guid>();
            var path             = new YamlAssetPath();

            path.PushMember(nameof(CollectionContainer.ConcreteRefList));
            path.PushIndex(0);
            objectReferences.Set(path, GuidGenerator.Get(1));
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.AreEqual(ConcreteNonIdentifiableReferenceableListYaml, yaml);
        }
Beispiel #6
0
        public void TestConcreteNonIdentifiableReferenceableDictionarySerialization()
        {
            var obj  = new NonIdentifiableCollectionContainer();
            var item = new Referenceable {
                Id = GuidGenerator.Get(1), Value = "Test"
            };

            obj.ConcreteRefDictionary.Add("Item1", item);
            obj.ConcreteRefDictionary.Add("Item2", item);
            var objectReferences = new YamlAssetMetadata <Guid>();
            var ids = CollectionItemIdHelper.GetCollectionItemIds(obj.ConcreteRefDictionary);

            ids["Item1"] = IdentifierGenerator.Get(1);
            ids["Item2"] = IdentifierGenerator.Get(2);
            var path = new YamlAssetPath();

            path.PushMember(nameof(CollectionContainer.ConcreteRefDictionary));
            path.PushIndex("Item1");
            objectReferences.Set(path, GuidGenerator.Get(1));
            var yaml = SerializeAsString(obj, objectReferences);

            Assert.Equal(ConcreteNonIdentifiableReferenceableDictionaryYaml, yaml);
        }
Beispiel #7
0
        public static YamlAssetPath ConvertPath(GraphNodePath path, int inNonIdentifiableType)
        {
            var currentNode = (IAssetNode)path.RootNode;
            var result      = new YamlAssetPath();
            var i           = 0;

            foreach (var item in path.Path)
            {
                switch (item.Type)
                {
                case GraphNodePath.ElementType.Member:
                    var member = (string)item.Value;
                    result.PushMember(member);
                    var objectNode = currentNode as IObjectNode;
                    if (objectNode == null)
                    {
                        throw new InvalidOperationException($"An IObjectNode was expected when processing the path [{path}]");
                    }
                    currentNode = (IAssetNode)objectNode.TryGetChild(member);
                    break;

                case GraphNodePath.ElementType.Target:
                    if (i < path.Path.Count - 1)
                    {
                        var targetingMemberNode = currentNode as IMemberNode;
                        if (targetingMemberNode == null)
                        {
                            throw new InvalidOperationException($"An IMemberNode was expected when processing the path [{path}]");
                        }
                        currentNode = (IAssetNode)targetingMemberNode.Target;
                    }
                    break;

                case GraphNodePath.ElementType.Index:
                    var index      = (Index)item.Value;
                    var memberNode = currentNode as AssetMemberNode;
                    if (memberNode == null)
                    {
                        throw new InvalidOperationException($"An AssetMemberNode was expected when processing the path [{path}]");
                    }
                    if (inNonIdentifiableType > 0 || memberNode.IsNonIdentifiableCollectionContent)
                    {
                        result.PushIndex(index.Value);
                    }
                    else
                    {
                        var id = memberNode.IndexToId(index);
                        // Create a new id if we don't have any so far
                        if (id == ItemId.Empty)
                        {
                            id = ItemId.New();
                        }
                        result.PushItemId(id);
                    }
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (IAssetNode)currentNode.IndexedTarget(index);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                ++i;
            }
            return(result);
        }
        public static YamlAssetPath ConvertPath([NotNull] GraphNodePath path, int inNonIdentifiableType = 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            var currentNode = (IAssetNode)path.RootNode;
            var result      = new YamlAssetPath();
            var i           = 0;

            foreach (var item in path.Path)
            {
                switch (item.Type)
                {
                case GraphNodePath.ElementType.Member:
                {
                    var member = item.Name;
                    result.PushMember(member);
                    var objectNode = currentNode as IObjectNode;
                    if (objectNode == null)
                    {
                        throw new InvalidOperationException($"An IObjectNode was expected when processing the path [{path}]");
                    }
                    currentNode = (IAssetNode)objectNode.TryGetChild(member);
                    break;
                }

                case GraphNodePath.ElementType.Target:
                {
                    if (i < path.Path.Count - 1)
                    {
                        var targetingMemberNode = currentNode as IMemberNode;
                        if (targetingMemberNode == null)
                        {
                            throw new InvalidOperationException($"An IMemberNode was expected when processing the path [{path}]");
                        }
                        currentNode = (IAssetNode)targetingMemberNode.Target;
                    }
                    break;
                }

                case GraphNodePath.ElementType.Index:
                {
                    var index      = item.Index;
                    var objectNode = currentNode as AssetObjectNode;
                    if (objectNode == null)
                    {
                        throw new InvalidOperationException($"An IObjectNode was expected when processing the path [{path}]");
                    }
                    if (inNonIdentifiableType > 0 || !CollectionItemIdHelper.HasCollectionItemIds(objectNode.Retrieve()))
                    {
                        result.PushIndex(index.Value);
                    }
                    else
                    {
                        var id = objectNode.IndexToId(index);
                        // Create a new id if we don't have any so far
                        if (id == ItemId.Empty)
                        {
                            id = ItemId.New();
                        }
                        result.PushItemId(id);
                    }
                    if (i < path.Path.Count - 1)
                    {
                        currentNode = (IAssetNode)objectNode.IndexedTarget(index);
                    }
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
                ++i;
            }
            return(result);
        }