Skip to content

Add TryGet methods for internally tagged unions #6916

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

Merged
merged 1 commit into from
Nov 4, 2022
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
13 changes: 0 additions & 13 deletions src/Elastic.Clients.Elasticsearch/Types/Mapping/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -20,18 +19,6 @@ public partial class Properties

public void Add<TDocument>(Expression<Func<TDocument, object>> propertyName, IProperty property) => BackingDictionary.Add(Sanitize(propertyName), property);

public bool TryGetProperty<T>(PropertyName propertyName, [NotNullWhen(returnValue: true)] out T? property) where T : class, IProperty
{
if (BackingDictionary.TryGetValue(propertyName, out var matchedProperty) && matchedProperty is T finalProperty)
{
property = finalProperty;
return true;
}

property = null;
return false;
}

protected override PropertyName Sanitize(PropertyName key) => _settings?.Inferrer.PropertyName(key) ?? key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -37,6 +38,19 @@ public Analyzers(IDictionary<string, IAnalyzer> container) : base(container)
}

public void Add(string name, IAnalyzer analyzer) => BackingDictionary.Add(Sanitize(name), analyzer);
public bool TryGetAnalyzer(string name, [NotNullWhen(returnValue: true)] out IAnalyzer analyzer) => BackingDictionary.TryGetValue(name, out analyzer);
public bool TryGetAnalyzer<T>(string name, [NotNullWhen(returnValue: true)] out T? analyzer)
where T : class, IAnalyzer
{
if (BackingDictionary.TryGetValue(name, out var matchedValue) && matchedValue is T finalValue)
{
analyzer = finalValue;
return true;
}

analyzer = null;
return false;
}
}

public sealed partial class AnalyzersDescriptor : IsADictionaryDescriptor<AnalyzersDescriptor, Analyzers, string, IAnalyzer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -37,6 +38,19 @@ public CharFilterDefinitions(IDictionary<string, ICharFilterDefinition> containe
}

public void Add(string name, ICharFilterDefinition charFilterDefinition) => BackingDictionary.Add(Sanitize(name), charFilterDefinition);
public bool TryGetCharFilterDefinition(string name, [NotNullWhen(returnValue: true)] out ICharFilterDefinition charFilterDefinition) => BackingDictionary.TryGetValue(name, out charFilterDefinition);
public bool TryGetCharFilterDefinition<T>(string name, [NotNullWhen(returnValue: true)] out T? charFilterDefinition)
where T : class, ICharFilterDefinition
{
if (BackingDictionary.TryGetValue(name, out var matchedValue) && matchedValue is T finalValue)
{
charFilterDefinition = finalValue;
return true;
}

charFilterDefinition = null;
return false;
}
}

public sealed partial class CharFilterDefinitionsDescriptor : IsADictionaryDescriptor<CharFilterDefinitionsDescriptor, CharFilterDefinitions, string, ICharFilterDefinition>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -37,6 +38,19 @@ public Normalizers(IDictionary<string, INormalizer> container) : base(container)
}

public void Add(string name, INormalizer normalizer) => BackingDictionary.Add(Sanitize(name), normalizer);
public bool TryGetNormalizer(string name, [NotNullWhen(returnValue: true)] out INormalizer normalizer) => BackingDictionary.TryGetValue(name, out normalizer);
public bool TryGetNormalizer<T>(string name, [NotNullWhen(returnValue: true)] out T? normalizer)
where T : class, INormalizer
{
if (BackingDictionary.TryGetValue(name, out var matchedValue) && matchedValue is T finalValue)
{
normalizer = finalValue;
return true;
}

normalizer = null;
return false;
}
}

public sealed partial class NormalizersDescriptor : IsADictionaryDescriptor<NormalizersDescriptor, Normalizers, string, INormalizer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -37,6 +38,19 @@ public TokenFilterDefinitions(IDictionary<string, ITokenFilterDefinition> contai
}

public void Add(string name, ITokenFilterDefinition tokenFilterDefinition) => BackingDictionary.Add(Sanitize(name), tokenFilterDefinition);
public bool TryGetTokenFilterDefinition(string name, [NotNullWhen(returnValue: true)] out ITokenFilterDefinition tokenFilterDefinition) => BackingDictionary.TryGetValue(name, out tokenFilterDefinition);
public bool TryGetTokenFilterDefinition<T>(string name, [NotNullWhen(returnValue: true)] out T? tokenFilterDefinition)
where T : class, ITokenFilterDefinition
{
if (BackingDictionary.TryGetValue(name, out var matchedValue) && matchedValue is T finalValue)
{
tokenFilterDefinition = finalValue;
return true;
}

tokenFilterDefinition = null;
return false;
}
}

public sealed partial class TokenFilterDefinitionsDescriptor : IsADictionaryDescriptor<TokenFilterDefinitionsDescriptor, TokenFilterDefinitions, string, ITokenFilterDefinition>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -37,6 +38,19 @@ public TokenizerDefinitions(IDictionary<string, ITokenizerDefinition> container)
}

public void Add(string name, ITokenizerDefinition tokenizerDefinition) => BackingDictionary.Add(Sanitize(name), tokenizerDefinition);
public bool TryGetTokenizerDefinition(string name, [NotNullWhen(returnValue: true)] out ITokenizerDefinition tokenizerDefinition) => BackingDictionary.TryGetValue(name, out tokenizerDefinition);
public bool TryGetTokenizerDefinition<T>(string name, [NotNullWhen(returnValue: true)] out T? tokenizerDefinition)
where T : class, ITokenizerDefinition
{
if (BackingDictionary.TryGetValue(name, out var matchedValue) && matchedValue is T finalValue)
{
tokenizerDefinition = finalValue;
return true;
}

tokenizerDefinition = null;
return false;
}
}

public sealed partial class TokenizerDefinitionsDescriptor : IsADictionaryDescriptor<TokenizerDefinitionsDescriptor, TokenizerDefinitions, string, ITokenizerDefinition>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -37,6 +38,19 @@ public Properties(IDictionary<PropertyName, IProperty> container) : base(contain
}

public void Add(PropertyName propertyName, IProperty property) => BackingDictionary.Add(Sanitize(propertyName), property);
public bool TryGetProperty(PropertyName propertyName, [NotNullWhen(returnValue: true)] out IProperty property) => BackingDictionary.TryGetValue(propertyName, out property);
public bool TryGetProperty<T>(PropertyName propertyName, [NotNullWhen(returnValue: true)] out T? property)
where T : class, IProperty
{
if (BackingDictionary.TryGetValue(propertyName, out var matchedValue) && matchedValue is T finalValue)
{
property = finalValue;
return true;
}

property = null;
return false;
}
}

public sealed partial class PropertiesDescriptor<TDocument> : IsADictionaryDescriptor<PropertiesDescriptor<TDocument>, Properties, PropertyName, IProperty>
Expand Down