Copilot commented on code in PR #322:
URL: https://github.com/apache/arrow-dotnet/pull/322#discussion_r3113827624


##########
src/Apache.Arrow.Variant/VariantValueWriter.cs:
##########
@@ -0,0 +1,672 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Buffers;
+using System.Buffers.Binary;
+using System.Collections.Generic;
+using System.Data.SqlTypes;
+using System.IO;
+using System.Text;
+
+namespace Apache.Arrow.Variant
+{
+    /// <summary>
+    /// Streams variant value bytes directly from primitive calls, without
+    /// requiring an intermediate <see cref="VariantValue"/>. Use this to
+    /// implement encoders from arbitrary input formats (JSON, CBOR, etc.).
+    /// </summary>
+    /// <remarks>
+    /// Usage pattern:
+    /// <list type="number">
+    /// <item>Create a <see cref="VariantMetadataBuilder"/> and <see 
cref="VariantMetadataBuilder.Add(string)"/> every field name that will 
appear.</item>
+    /// <item>Call <see cref="VariantMetadataBuilder.Build(out int[])"/> to 
produce the metadata bytes and the ID remap.</item>
+    /// <item>Create a <see cref="VariantValueWriter"/> with the metadata 
builder and remap, emit the value via the <c>Write*</c> / <c>Begin*</c> / 
<c>End*</c> methods, then call <see cref="ToArray"/>.</item>
+    /// </list>
+    /// </remarks>
+    public sealed class VariantValueWriter
+    {
+        private const int StackAllocThreshold = 256;
+
+        private readonly VariantMetadataBuilder _metadata;
+        private readonly int[] _idRemap;
+        private readonly MemoryStream _root = new MemoryStream();
+        private readonly Stack<Frame> _frameStack = new Stack<Frame>();
+        private readonly Stack<MemoryStream> _streamPool = new 
Stack<MemoryStream>();
+        private Frame _frame;
+
+#if !NET8_0_OR_GREATER
+        private readonly byte[] _scratch = new byte[16];
+#endif
+
+        /// <summary>
+        /// Creates a writer that produces value bytes referencing the given 
metadata.
+        /// </summary>
+        /// <param name="metadata">The metadata builder used to resolve field 
names to IDs.</param>
+        /// <param name="idRemap">The remap returned by <see 
cref="VariantMetadataBuilder.Build(out int[])"/>.</param>
+        public VariantValueWriter(VariantMetadataBuilder metadata, int[] 
idRemap)
+        {
+            _metadata = metadata ?? throw new 
ArgumentNullException(nameof(metadata));
+            _idRemap = idRemap ?? throw new 
ArgumentNullException(nameof(idRemap));

Review Comment:
   Consider validating that idRemap matches the provided metadata builder at 
construction time (e.g., metadata.Count == idRemap.Length). As written, a 
mismatched or later-mutated VariantMetadataBuilder can lead to 
IndexOutOfRangeException in WriteFieldName, which will be harder for callers to 
diagnose than an ArgumentException thrown from the constructor.
   ```suggestion
               _idRemap = idRemap ?? throw new 
ArgumentNullException(nameof(idRemap));
   
               if (_metadata.Count != _idRemap.Length)
               {
                   throw new ArgumentException(
                       "The idRemap array length must match the metadata 
builder count used to create it.",
                       nameof(idRemap));
               }
   ```



##########
src/Apache.Arrow.Variant/Properties/AssemblyInfo.cs:
##########
@@ -16,3 +16,4 @@
 using System.Runtime.CompilerServices;
 
 [assembly: InternalsVisibleTo("Apache.Arrow.Variant.Tests, 
PublicKey=0024000004800000940000000602000000240000525341310004000001000100e504183f6d470d6b67b6d19212be3e1f598f70c246a120194bc38130101d0c1853e4a0f2232cb12e37a7a90e707aabd38511dac4f25fcb0d691b2aa265900bf42de7f70468fc997551a40e1e0679b605aa2088a4a69e07c117e988f5b1738c570ee66997fba02485e7856a49eca5fd0706d09899b8312577cbb9034599fc92d4")]
+[assembly: InternalsVisibleTo("Apache.Arrow.Operations, 
PublicKey=0024000004800000940000000602000000240000525341310004000001000100e504183f6d470d6b67b6d19212be3e1f598f70c246a120194bc38130101d0c1853e4a0f2232cb12e37a7a90e707aabd38511dac4f25fcb0d691b2aa265900bf42de7f70468fc997551a40e1e0679b605aa2088a4a69e07c117e988f5b1738c570ee66997fba02485e7856a49eca5fd0706d09899b8312577cbb9034599fc92d4")]

Review Comment:
   The InternalsVisibleTo grant to Apache.Arrow.Operations appears unnecessary: 
Apache.Arrow.Operations only uses public APIs from Apache.Arrow.Variant in this 
PR. If no internal members are required, removing this reduces the surface area 
between assemblies and avoids accidentally creating an internal API dependency.
   ```suggestion
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to