private static List <Schema.Node> GetSchemaNode(Schema.Node node)
        {
            var schemaNodes = new List <Schema.Node>();

            for (var n = node; n != null; n = n.Parent)
            {
                schemaNodes.Add(n);
            }
            schemaNodes.RemoveAt(schemaNodes.Count - 1); // we don't need the schema root
            schemaNodes.Reverse();                       // root to leaf
            return(schemaNodes);
        }
        private static (short nestingDepth, short[] nullDefinitionLevels) GetArraySchemaInfo(Type elementType, Schema.Node node, int maxRepetitionLevel)
        {
            var schemaNodes = new List <Schema.Node>();

            for (; node != null; node = node.Parent)
            {
                schemaNodes.Add(node);
            }
            schemaNodes.RemoveAt(schemaNodes.Count - 1); // we don't need the schema root
            schemaNodes.Reverse();                       // root to leaf

            var nestingDepth         = schemaNodes.Count(n => n.LogicalType == LogicalType.List);
            var nullDefinitionLevels = new short[nestingDepth + 1];
            int nestingLevel         = 0;

            // By default mark every level as required.
            for (int i = 0; i < nullDefinitionLevels.Length; i++)
            {
                nullDefinitionLevels[i] = -1;
            }

            for (int i = 0; i < schemaNodes.Count; i++)
            {
                if (schemaNodes[i].Repetition == Repetition.Optional)
                {
                    nullDefinitionLevels[nestingLevel++] = (short)i;
                }
            }

            // Check the type matches
            var maxRepLevel = maxRepetitionLevel;
            var depth       = 0;

            for (var type = elementType; type != typeof(byte[]) && type.IsArray; type = type.GetElementType())
            {
                depth++;
            }

            if (nestingDepth != depth || depth != maxRepLevel)
            {
                throw new Exception("Schema does not match type we are trying to read into.");
            }

            return(checked ((short)nestingDepth), nullDefinitionLevels);
        }