private void ParseSubHierarchy(INode parent, AvroNode n)
        {
            INode parsed = null;
            if (n.packageNode != null && !n.packageNode.Equals(""))
            {
                parsed = new PackageNodeImpl(parent, n.name, n.fullName);
            }
            else if (n.namedParameterNode != null && !n.namedParameterNode.Equals(""))
            {
                AvroNamedParameterNode np = (AvroNamedParameterNode)n.namedParameterNode;
                parsed = new NamedParameterNodeImpl(parent, n.name,
                    n.fullName, np.fullArgClassName, np.simpleArgClassName,
                    np.isSet, np.isList, np.documentation, np.shortName,
                    np.instanceDefault.ToArray());
            }
            else if (n.classNode != null && !n.classNode.Equals(""))
            {
                AvroClassNode cn = (AvroClassNode)n.classNode;
                IList<IConstructorDef> injectableConstructors = new List<IConstructorDef>();
                IList<IConstructorDef> allConstructors = new List<IConstructorDef>();

                foreach (AvroConstructorDef injectable in cn.injectableConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(injectable, true);
                    injectableConstructors.Add(def);
                    allConstructors.Add(def);
                }
                foreach (AvroConstructorDef other in cn.otherConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(other, false);
                    allConstructors.Add(def);
                }

                parsed = new ClassNodeImpl(parent, n.name, n.fullName,
                cn.isUnit, cn.isInjectionCandidate,
                cn.isExternalConstructor, injectableConstructors,
                allConstructors, cn.defaultImplementation);
            }
            else
            {
                Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Bad protocol buffer: got abstract node" + n), LOGGER);
            }

            foreach (AvroNode child in n.children)
            {
                ParseSubHierarchy(parsed, child);
            }
        }
        /// <summary>
        /// Convert AvroNode into AvroClassHierarchy object
        /// </summary>
        /// <param name="root"></param>
        internal AvroClassHierarchy(AvroNode root)
        {
            _rootNode = new PackageNodeImpl();
            if (root.packageNode == null)
            {
                Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("Expected a package node.  Got: " + root), LOGGER); 
            }

            foreach (AvroNode child in root.children)
            {
                ParseSubHierarchy(_rootNode, child);
            }
            
            BuildHashTable(_rootNode);

            foreach (AvroNode child in root.children)
            {
                WireUpInheritanceRelationships(child);
            }
        }
 private byte[] AvroSerialize(AvroNode obj)
 {
     var serializer = AvroSerializer.Create<AvroNode>();
     using (MemoryStream stream = new MemoryStream())
     {
         serializer.Serialize(stream, obj);
         return stream.GetBuffer();
     }
 }
        private AvroNode NewPackageNode(string name,
            string fullName, IList<AvroNode> children)
        {
            AvroPackageNode packageNode = new AvroPackageNode();
            AvroNode n = new AvroNode();
            n.name = name;
            n.fullName = fullName;
            n.packageNode = packageNode;

            n.children = new List<AvroNode>();
            foreach (var c in children)
            {
                n.children.Add(c);
            }

            return n;
        }
        private AvroNode NewNamedParameterNode(string name,
            string fullName, string simpleArgClassName, string fullArgClassName,
            bool isSet, bool isList, string documentation, // can be null
            string shortName, // can be null
            string[] instanceDefault, // can be null
            IList<AvroNode> children)
        {
            AvroNamedParameterNode namedParameterNode = new AvroNamedParameterNode();
            namedParameterNode.simpleArgClassName = simpleArgClassName;
            namedParameterNode.fullArgClassName = fullArgClassName;
            namedParameterNode.isSet = isSet;
            namedParameterNode.isList = isList;

            if (documentation != null)
            {
                namedParameterNode.documentation = documentation;
            }

            if (shortName != null)
            {
                namedParameterNode.shortName = shortName;
            }

            namedParameterNode.instanceDefault = new List<string>();
            foreach (var id in instanceDefault)
            {
                namedParameterNode.instanceDefault.Add(id);
            }

            AvroNode n = new AvroNode();
            n.name = name;
            n.fullName = fullName;
            n.namedParameterNode = namedParameterNode;

            n.children = new List<AvroNode>();
            foreach (var c in children)
            {
                n.children.Add(c);
            }

            return n;
        }
        private AvroNode NewClassNode(String name,
            String fullName,
            bool isInjectionCandidate,
            bool isExternalConstructor, bool isUnit,
            IList<AvroConstructorDef> injectableConstructors,
            IList<AvroConstructorDef> otherConstructors,
            IList<String> implFullNames, IList<AvroNode> children)
        {
            AvroClassNode classNode = new AvroClassNode();

            classNode.isInjectionCandidate = isInjectionCandidate;
            classNode.injectableConstructors = new List<AvroConstructorDef>();
            foreach (var ic in injectableConstructors)
            {
                classNode.injectableConstructors.Add(ic);
            }

            classNode.otherConstructors = new List<AvroConstructorDef>();
            foreach (var oc in otherConstructors)
            {
                classNode.otherConstructors.Add(oc);
            }

            classNode.implFullNames = new List<string>();
            foreach (var implFullName in implFullNames)
            {
                classNode.implFullNames.Add(implFullName);
            }

            AvroNode n = new AvroNode();
            n.name = name;
            n.fullName = fullName;
            n.classNode = classNode;

            n.children = new List<AvroNode>();
            foreach (var c in children)
            {
                n.children.Add(c);
            }

            return n;
        }
 /// <summary>
 /// Deserailize ClassHierarchy from an AvroNode into AvroClassHierarchy object
 /// </summary>
 /// <param name="n"></param>
 /// <returns></returns>
 public IClassHierarchy FromAvroNode(AvroNode n)
 {
     return new AvroClassHierarchy(n);
 }
        private void WireUpInheritanceRelationships(AvroNode n)
        {
            if (n.classNode != null && !n.classNode.Equals(""))
            {
                AvroClassNode cn = (AvroClassNode)n.classNode;
                IClassNode iface = null;

                try
                {
                    iface = (IClassNode)GetNode(n.fullName);
                }
                catch (NameResolutionException e)
                {
                    Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                    var ex = new IllegalStateException("When reading protocol buffer node "
                        + n.fullName + " does not exist.  Full record is " + n, e);
                    Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                }

                foreach (string impl in cn.implFullNames)
                {
                    try
                    {
                        iface.PutImpl((IClassNode)GetNode(impl));
                    }
                    catch (NameResolutionException e)
                    {
                        Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                        var ex = new IllegalStateException("When reading protocol buffer node "
                            + n + " refers to non-existent implementation:" + impl);
                        Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);

                    }
                    catch (InvalidCastException e)
                    {
                        Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                        try
                        {
                            var ex = new IllegalStateException(
                                "When reading protocol buffer node " + n
                                + " found implementation" + GetNode(impl)
                                + " which is not a ClassNode!");
                            Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                        }
                        catch (NameResolutionException ne)
                        {
                            Utilities.Diagnostics.Exceptions.Caught(ne, Level.Error, LOGGER);
                            var ex = new IllegalStateException(
                                "Got 'cant happen' exception when producing error message for " + e);
                            Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                        }
                    }
                }
            }
        }