| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | |
|
| | 7 | | namespace Azure.Search.Documents.Indexes.Models |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Base field type for helper classes to more easily create a <see cref="SearchIndex"/>. |
| | 11 | | /// </summary> |
| | 12 | | public abstract class SearchFieldTemplate |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Initializes a new instance of the <see cref="SearchFieldTemplate"/> class. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="name">The name of the field, which must be unique within the index or parent field.</param> |
| | 18 | | /// <param name="type">The data type of the field.</param> |
| | 19 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | 20 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| 19541 | 21 | | private protected SearchFieldTemplate(string name, SearchFieldDataType type) |
| | 22 | | { |
| 19541 | 23 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| | 24 | |
|
| 19541 | 25 | | Name = name; |
| 19541 | 26 | | Type = type; |
| 19541 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Gets the name of the field. |
| | 31 | | /// </summary> |
| 19541 | 32 | | public string Name { get; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Gets the data type of the field. |
| | 36 | | /// </summary> |
| 19541 | 37 | | public SearchFieldDataType Type { get; } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Persists class-specific properties into the given <see cref="SearchField"/>. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="field">The <see cref="SearchField"/> into which properties are persisted.</param> |
| | 43 | | private protected abstract void Save(SearchField field); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Casts a <see cref="SearchFieldTemplate"/> or derivative to a <see cref="SearchField"/>. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="value">The <see cref="SearchFieldTemplate"/> or derivative to cast.</param> |
| | 49 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | 50 | | public static implicit operator SearchField(SearchFieldTemplate value) |
| | 51 | | { |
| 19541 | 52 | | Argument.AssertNotNull(value, nameof(value)); |
| | 53 | |
|
| 19541 | 54 | | SearchField field = new SearchField(value.Name, value.Type); |
| 19541 | 55 | | value.Save(field); |
| | 56 | |
|
| 19541 | 57 | | return field; |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |