Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IEnumerable<string> ctor from SynonynMap #11394

Merged
merged 3 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/search/Azure.Search.Documents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.0.0-preview.3 (Unreleased)

### Breaking Changes

- Removed constructor from `SynonymMap` with `IEnumerable<string>` parameter.

## 1.0.0-preview.2 (2020-04-06)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1670,8 +1670,8 @@ internal SuggestResults() { }
}
public partial class SynonymMap
{
public SynonymMap(string name, System.Collections.Generic.IEnumerable<string> synonyms, string format = "soln") { }
public SynonymMap(string name, string synonym, string format = "soln") { }
public SynonymMap(string name, System.IO.TextReader reader) { }
public SynonymMap(string name, string synonyms) { }
public Azure.Search.Documents.Models.EncryptionKey EncryptionKey { get { throw null; } set { } }
public string ETag { get { throw null; } set { } }
public string Format { get { throw null; } }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 55 additions & 37 deletions sdk/search/Azure.Search.Documents/src/Models/SynonymMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,94 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Azure.Core;

namespace Azure.Search.Documents.Models
{
[CodeGenSuppress(nameof(SynonymMap), typeof(string), typeof(string))]
public partial class SynonymMap
{
private const string DefaultFormat = "soln";
private const string NewLine = "\n";
private const string DefaultFormat = "solr";

// TODO: Replace constructor and read-only properties when https://github.com/Azure/autorest.csharp/issues/554 is fixed.

/// <summary>
/// Initializes a new instance of the <see cref="SynonymMap"/> class.
/// </summary>
/// <param name="name">The name of the synonym map.</param>
/// <param name="synonym">The synonym to define.</param>
/// <param name="format">The format of the synonym map. Currently, only "soln" is supported and is the default value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/>, <paramref name="synonym"/>, or <paramref name="format"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/>, <paramref name="synonym"/>, or <paramref name="format"/> is null.</exception>
public SynonymMap(string name, string synonym, string format = DefaultFormat)
/// <param name="synonyms">
/// The formatted synonyms string to define.
/// Because only the Solr synonym map format is currently supported, these are values delimited by "\n".
/// </param>
/// <exception cref="ArgumentException"><paramref name="name"/> or <paramref name="synonyms"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="synonyms"/> is null.</exception>
public SynonymMap(string name, string synonyms)
{
Argument.AssertNotNullOrEmpty(name, nameof(name));
Argument.AssertNotNullOrEmpty(format, nameof(format));
Argument.AssertNotNullOrEmpty(synonym, nameof(synonym));
Argument.AssertNotNullOrEmpty(synonyms, nameof(synonyms));

Name = name;
Format = format;
Synonyms = synonym;
Format = DefaultFormat;
Synonyms = synonyms;
}

/// <summary>
/// Initializes a new instance of the <see cref="SynonymMap"/> class.
/// </summary>
/// <param name="name">The name of the synonym map.</param>
/// <param name="synonyms">One or more synonyms to define. These values will be separated by line breaks automatically.</param>
/// <param name="format">The format of the synonym map. Currently, only "soln" is supported and is the default value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/>, <paramref name="synonyms"/>, or <paramref name="format"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/>, <paramref name="synonyms"/>, or <paramref name="format"/> is null.</exception>
public SynonymMap(string name, IEnumerable<string> synonyms, string format = DefaultFormat)
/// <param name="reader">
/// A <see cref="TextReader"/> from which formatted synonyms are read.
/// Because only the Solr synonym map format is currently supported, these are values delimited by "\n".
/// </param>
/// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="reader"/> is null.</exception>
public SynonymMap(string name, TextReader reader)
{
Argument.AssertNotNullOrEmpty(name, nameof(name));
Argument.AssertNotNullOrEmpty(format, nameof(format));
Argument.AssertNotNullOrEmpty(synonyms, nameof(synonyms));
Argument.AssertNotNull(reader, nameof(reader));

Name = name;
Format = format;
Synonyms = string.Join(NewLine, synonyms);
}

private SynonymMap()
{
Format = DefaultFormat;
Synonyms = reader.ReadToEnd();
}

/// <summary>
/// Gets the name of the synonym map.
/// Canonicalizes property names from how they appear on <see cref="SynonymMap"/> to those expected by the Search service.
/// </summary>
[CodeGenMember("name")]
public string Name { get; internal set; }
/// <param name="names">The given property names.</param>
/// <returns>Canonicalized property names expected by the Search service, or null if <paramref name="names"/> is null.</returns>
internal static IEnumerable<string> CanonicalizePropertyNames(IEnumerable<string> names) =>
// TODO: Replace when https://github.com/Azure/azure-sdk-for-net/issues/11393 is resolved.
names?.Select(name =>
{
if (string.Equals("name", name, StringComparison.InvariantCultureIgnoreCase))
{
return "name";
}

/// <summary>
/// Gets the format of the synonym map.
/// </summary>
[CodeGenMember("format")]
public string Format { get; internal set; }
if (string.Equals("format", name, StringComparison.InvariantCultureIgnoreCase))
{
return "format";
}

/// <summary>
/// Gets the synonym rules for this synonym map.
/// </summary>
[CodeGenMember("synonyms")]
public string Synonyms { get; internal set; }
if (string.Equals("synonyms", name, StringComparison.InvariantCultureIgnoreCase))
{
return "synonyms";
}

if (string.Equals("encryptionKey", name, StringComparison.InvariantCultureIgnoreCase))
{
return "encryptionKey";
}

if (string.Equals("etag", name, StringComparison.InvariantCultureIgnoreCase))
{
return "@odata.etag";
}

return name;
});
}
}
8 changes: 6 additions & 2 deletions sdk/search/Azure.Search.Documents/src/SearchServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,8 +1616,10 @@ public virtual Response<IReadOnlyList<SynonymMap>> GetSynonymMaps(
SearchRequestOptions options = null,
CancellationToken cancellationToken = default)
{
string select = SynonymMap.CanonicalizePropertyNames(selectProperties).CommaJoin() ?? Constants.All;

Response<ListSynonymMapsResult> result = SynonymMapsClient.List(
selectProperties.CommaJoin() ?? Constants.All,
select,
options?.ClientRequestId,
cancellationToken);

Expand All @@ -1638,8 +1640,10 @@ public virtual async Task<Response<IReadOnlyList<SynonymMap>>> GetSynonymMapsAsy
SearchRequestOptions options = null,
CancellationToken cancellationToken = default)
{
string select = SynonymMap.CanonicalizePropertyNames(selectProperties).CommaJoin() ?? Constants.All;

Response<ListSynonymMapsResult> result = await SynonymMapsClient.ListAsync(
selectProperties.CommaJoin() ?? Constants.All,
select,
options?.ClientRequestId,
cancellationToken)
.ConfigureAwait(false);
Expand Down
84 changes: 84 additions & 0 deletions sdk/search/Azure.Search.Documents/tests/Models/SynonymMapTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using Azure.Search.Documents.Models;
using NUnit.Framework;

namespace Azure.Search.Documents.Tests.Models
{
public class SynonymMapTests
{
[Test]
public void StringConstructorTests()
{
ArgumentException ex = Assert.Throws<ArgumentNullException>(() => new SynonymMap(null, (string)null));
Assert.AreEqual("name", ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => new SynonymMap(string.Empty, (string)null));
Assert.AreEqual("name", ex.ParamName);

ex = Assert.Throws<ArgumentNullException>(() => new SynonymMap("test", (string)null));
Assert.AreEqual("synonyms", ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => new SynonymMap("test", string.Empty));
Assert.AreEqual("synonyms", ex.ParamName);
}

[Test]
public void TextReaderConstructorTests()
{
ArgumentException ex = Assert.Throws<ArgumentNullException>(() => new SynonymMap(null, (TextReader)null));
Assert.AreEqual("name", ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => new SynonymMap(string.Empty, (TextReader)null));
Assert.AreEqual("name", ex.ParamName);

ex = Assert.Throws<ArgumentNullException>(() => new SynonymMap("test", (TextReader)null));
Assert.AreEqual("reader", ex.ParamName);
}

[Test]
public void SynonymsFromTextReader()
{
using StringReader reader = new StringReader("ms,msft=>Microsoft\naz=>Azure");

SynonymMap map = new SynonymMap("test", reader);
Assert.AreEqual("ms,msft=>Microsoft\naz=>Azure", map.Synonyms);
}

[Test]
public void CanonicalizesNullPropertyNames()
{
Assert.IsNull(SynonymMap.CanonicalizePropertyNames(null));
}

[Test]
public void CanonicalizesPropertyNames()
{
IEnumerable<string> actual = SynonymMap.CanonicalizePropertyNames(new[]
{
nameof(SynonymMap.Name),
nameof(SynonymMap.Format),
nameof(SynonymMap.Synonyms),
nameof(SynonymMap.EncryptionKey),
nameof(SynonymMap.ETag),
"Other",
});

IEnumerable<string> expected = new[]
{
"name",
"format",
"synonyms",
"encryptionKey",
"@odata.etag",
"Other",
};

CollectionAssert.AreEqual(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,38 @@ await serviceClient.RunIndexerAsync(
Assert.AreEqual(SearchResources.TestDocuments.Length, count);
}

[Test]
public async Task CrudSynonymMaps()
{
await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

string synonymMapName = Recording.Random.GetName();

SearchServiceClient client = resources.GetServiceClient();

SynonymMap map = await client.CreateSynonymMapAsync(new SynonymMap(synonymMapName, "msft=>Microsoft"));
Assert.AreEqual(synonymMapName, map.Name);
Assert.AreEqual("solr", map.Format);
Assert.AreEqual("msft=>Microsoft", map.Synonyms);

map = await client.CreateOrUpdateSynonymMapAsync(new SynonymMap(synonymMapName, "ms,msft=>Microsoft"), new MatchConditions { IfMatch = new ETag(map.ETag) });
Assert.AreEqual(synonymMapName, map.Name);
Assert.AreEqual("solr", map.Format);
Assert.AreEqual("ms,msft=>Microsoft", map.Synonyms);

Response<IReadOnlyList<SynonymMap>> mapsResponse = await client.GetSynonymMapsAsync(new[] { nameof(SynonymMap.Name) });
foreach (SynonymMap namedMap in mapsResponse.Value)
{
if (string.Equals(map.Name, namedMap.Name, StringComparison.OrdinalIgnoreCase))
{
SynonymMap fetchedMap = await client.GetSynonymMapAsync(namedMap.Name);
Assert.AreEqual(map.Synonyms, fetchedMap.Synonyms);
}
}

await client.DeleteSynonymMapAsync(map.Name, new MatchConditions { IfMatch = new ETag(map.ETag) });
}

/// <summary>
/// Gets a new <see cref="SearchRequestOptions"/>.
/// </summary>
Expand Down
Loading