< Summary

Class:Azure.Search.Documents.Models.IndexDocumentsAction`1
Assembly:Azure.Search.Documents
File(s):C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Models\IndexDocumentsAction{T}.cs
Covered lines:21
Uncovered lines:1
Coverable lines:22
Total lines:215
Line coverage:95.4% (21 of 22)
Covered branches:6
Total branches:6
Branch coverage:100% (6 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_ActionType()-100%100%
get_Document()-100%100%
.ctor()-0%100%
.ctor(...)-100%100%
SerializeAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Models\IndexDocumentsAction{T}.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Diagnostics;
 6using System.IO;
 7using System.Text.Json;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using Azure.Core;
 11#if EXPERIMENTAL_SERIALIZER
 12using Azure.Core.Serialization;
 13#endif
 14
 15#pragma warning disable SA1402 // File may only contain a single type
 16
 17namespace Azure.Search.Documents.Models
 18{
 19    // Hide the untyped IndexAction
 20    [CodeGenModel("IndexAction")]
 21    internal partial class IndexAction { }
 22
 23    /// <summary>
 24    /// Represents an index action that operates on a document.
 25    /// </summary>
 26    /// <typeparam name="T">
 27    /// The .NET type that maps to the index schema. Instances of this type can
 28    /// be retrieved as documents from the index.
 29    /// </typeparam>
 30    public class IndexDocumentsAction<T>
 31    {
 32        /// <summary>
 33        /// The operation to perform on a document in an indexing batch.
 34        /// </summary>
 3337035        public IndexActionType ActionType { get; private set; }
 36
 37        /// <summary>
 38        /// The document to index.
 39        /// </summary>
 3337040        public T Document { get; private set; }
 41
 42        /// <summary>
 43        /// Private constructor to prevent direct instantiation.
 44        /// </summary>
 045        private IndexDocumentsAction() { }
 46
 47        /// <summary>
 48        /// Initializes a new instance of the IndexDocumentsAction class.
 49        /// </summary>
 50        /// <param name="type">
 51        /// The operation to perform on the document.
 52        /// </param>
 53        /// <param name="doc">The document to index.</param>
 1668554        public IndexDocumentsAction(IndexActionType type, T doc)
 55        {
 56            Debug.Assert(Enum.IsDefined(typeof(IndexActionType), type));
 57
 1668558            ActionType = type;
 1668559            Document = doc;
 1668560        }
 61
 62        #pragma warning disable CS1572 // Not all parameters will be used depending on feature flags
 63        #pragma warning disable CA1801 // Not all parameters are used depending on feature flags
 64        #pragma warning disable CS1998 // Won't await depending on feature flags
 65        /// <summary>
 66        /// Serialize the document write action.
 67        /// </summary>
 68        /// <param name="writer">The JSON writer.</param>
 69        /// <param name="serializer">
 70        /// Optional serializer that can be used to customize the serialization
 71        /// of strongly typed models.
 72        /// </param>
 73        /// <param name="options">JSON serializer options.</param>
 74        /// <param name="async">Whether to execute sync or async.</param>
 75        /// <param name="cancellationToken">
 76        /// Optional <see cref="CancellationToken"/> to propagate notifications
 77        /// that the operation should be canceled.
 78        /// </param>
 79        /// <returns>A task representing the serialization.</returns>
 80        internal async Task SerializeAsync(
 81            Utf8JsonWriter writer,
 82#if EXPERIMENTAL_SERIALIZER
 83            ObjectSerializer serializer,
 84#endif
 85            JsonSerializerOptions options,
 86            bool async,
 87            CancellationToken cancellationToken)
 88        #pragma warning restore CS1998
 89        #pragma warning restore CA1801
 90        #pragma warning restore CS1572
 91        {
 92            Debug.Assert(writer != null);
 93
 1668594            writer.WriteStartObject();
 1668595            writer.WriteString(Constants.SearchActionKeyJson, ActionType.ToSerialString());
 96
 97            // HACK: Serialize the user's model, parse it, and then write each
 98            // of its properties as if they were our own.
 99            byte[] json;
 100#if EXPERIMENTAL_SERIALIZER
 16685101            if (serializer != null)
 102            {
 3103                using MemoryStream stream = new MemoryStream();
 3104                if (async)
 105                {
 1106                    await serializer.SerializeAsync(stream, Document, typeof(T), cancellationToken).ConfigureAwait(false
 107                }
 108                else
 109                {
 2110                    serializer.Serialize(stream, Document, typeof(T), cancellationToken);
 111                }
 3112                json = stream.ToArray();
 3113            }
 114            else
 115            {
 116#endif
 16682117                json = JsonSerializer.SerializeToUtf8Bytes<T>(Document, options);
 118#if EXPERIMENTAL_SERIALIZER
 119            }
 120#endif
 16685121            using JsonDocument nested = JsonDocument.Parse(json);
 68548122            foreach (JsonProperty property in nested.RootElement.EnumerateObject())
 123            {
 17589124                property.WriteTo(writer);
 125            }
 126
 16685127            writer.WriteEndObject();
 16685128        }
 129    }
 130
 131    /// <summary>
 132    /// Creates <see cref="IndexDocumentsAction{T}"/> instances to add to an
 133    /// <see cref="IndexDocumentsBatch{T}"/> and submit via
 134    /// <see cref="SearchClient.IndexDocuments"/>.
 135    /// </summary>
 136    public static class IndexDocumentsAction
 137    {
 138        /// <summary>
 139        /// Create an <see cref="IndexDocumentsAction{T}"/> to upload.
 140        /// </summary>
 141        /// <typeparam name="T">
 142        /// The .NET type that maps to the index schema. Instances of this type
 143        /// can be retrieved as documents from the index.
 144        /// </typeparam>
 145        /// <param name="document">The document to upload.</param>
 146        /// <returns>
 147        /// An <see cref="IndexDocumentsAction{T}"/> to upload.
 148        /// </returns>
 149        public static IndexDocumentsAction<T> Upload<T>(T document) =>
 150            new IndexDocumentsAction<T>(IndexActionType.Upload, document);
 151
 152        /// <summary>
 153        /// Create an <see cref="IndexDocumentsAction{T}"/> to merge.
 154        /// </summary>
 155        /// <typeparam name="T">
 156        /// The .NET type that maps to the index schema. Instances of this type
 157        /// can be retrieved as documents from the index.
 158        /// </typeparam>
 159        /// <param name="document">The document to merge.</param>
 160        /// <returns>
 161        /// An <see cref="IndexDocumentsAction{T}"/> to merge.
 162        /// </returns>
 163        public static IndexDocumentsAction<T> Merge<T>(T document) =>
 164            new IndexDocumentsAction<T>(IndexActionType.Merge, document);
 165
 166        /// <summary>
 167        /// Create an <see cref="IndexDocumentsAction{T}"/> to merge or upload.
 168        /// </summary>
 169        /// <typeparam name="T">
 170        /// The .NET type that maps to the index schema. Instances of this type
 171        /// can be retrieved as documents from the index.
 172        /// </typeparam>
 173        /// <param name="document">The document to merge or upload.</param>
 174        /// <returns>
 175        /// An <see cref="IndexDocumentsAction{T}"/> to merge or upload.
 176        /// </returns>
 177        public static IndexDocumentsAction<T> MergeOrUpload<T>(T document) =>
 178            new IndexDocumentsAction<T>(IndexActionType.MergeOrUpload, document);
 179
 180        /// <summary>
 181        /// Create an <see cref="IndexDocumentsAction{T}"/> to delete.
 182        /// </summary>
 183        /// <typeparam name="T">
 184        /// The .NET type that maps to the index schema. Instances of this type
 185        /// can be retrieved as documents from the index.
 186        /// </typeparam>
 187        /// <param name="document">The document to delete.</param>
 188        /// <returns>
 189        /// An <see cref="IndexDocumentsAction{T}"/> to delete.
 190        /// </returns>
 191        public static IndexDocumentsAction<T> Delete<T>(T document) =>
 192            new IndexDocumentsAction<T>(IndexActionType.Delete, document);
 193
 194        /// <summary>
 195        /// Create an <see cref="IndexDocumentsAction{SearchDocument}"/> to
 196        /// delete.
 197        /// </summary>
 198        /// <param name="keyName">
 199        /// The name of the key field of the index.
 200        /// </param>
 201        /// <param name="keyValue">The key of the document to delete.</param>
 202        /// <returns>
 203        /// An <see cref="IndexDocumentsAction{SearchDocument}"/> to delete.
 204        /// </returns>
 205        public static IndexDocumentsAction<SearchDocument> Delete(string keyName, string keyValue)
 206        {
 207            Argument.AssertNotNullOrEmpty(keyName, nameof(keyName));
 208            Argument.AssertNotNull(keyValue, nameof(keyValue));
 209
 210            return new IndexDocumentsAction<SearchDocument>(
 211                IndexActionType.Delete,
 212                new SearchDocument { [keyName] = keyValue });
 213        }
 214    }
 215}