diff --git a/src/Elasticsearch.Net/Responses/Sniff/SniffResponse.cs b/src/Elasticsearch.Net/Responses/Sniff/SniffResponse.cs index 9e9d4146970..90cddd83951 100644 --- a/src/Elasticsearch.Net/Responses/Sniff/SniffResponse.cs +++ b/src/Elasticsearch.Net/Responses/Sniff/SniffResponse.cs @@ -78,8 +78,8 @@ internal bool HttpEnabled { get { - if (settings != null && settings.ContainsKey("http.enabled")) - return Convert.ToBoolean(settings["http.enabled"]); + if (settings != null && settings.TryGetValue("http.enabled", out object httpEnabled)) + return Convert.ToBoolean(httpEnabled); return http != null; } diff --git a/src/Elasticsearch.Net/Serialization/SimpleJson.cs b/src/Elasticsearch.Net/Serialization/SimpleJson.cs index 0c5ac95602b..8023dd8414a 100644 --- a/src/Elasticsearch.Net/Serialization/SimpleJson.cs +++ b/src/Elasticsearch.Net/Serialization/SimpleJson.cs @@ -267,7 +267,7 @@ public void Clear() /// public bool Contains(KeyValuePair item) { - return _members.ContainsKey(item.Key) && _members[item.Key] == item.Value; + return _members.TryGetValue(item.Key, out object value) && value == item.Value; } /// diff --git a/src/Nest/Aggregations/Metric/PercentileRanks/PercentileRanksAggregationJsonConverter.cs b/src/Nest/Aggregations/Metric/PercentileRanks/PercentileRanksAggregationJsonConverter.cs index cc3546fcc2f..a400c07b81e 100644 --- a/src/Nest/Aggregations/Metric/PercentileRanks/PercentileRanksAggregationJsonConverter.cs +++ b/src/Nest/Aggregations/Metric/PercentileRanks/PercentileRanksAggregationJsonConverter.cs @@ -17,8 +17,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist var percentileRanks = new PercentileRanksAggregation(); ReadMetricProperties(percentileRanks, properties); percentileRanks.Method = ReadMethodProperty(properties); - if (properties.ContainsKey("values")) - percentileRanks.Values = properties["values"].ToObject>(); + if (properties.TryGetValue("values", out JToken valuesToken)) + percentileRanks.Values = valuesToken.ToObject>(); return percentileRanks; ; diff --git a/src/Nest/Aggregations/Metric/Percentiles/PercentilesAggregationJsonConverter.cs b/src/Nest/Aggregations/Metric/Percentiles/PercentilesAggregationJsonConverter.cs index 457bda12d8f..d85c752b868 100644 --- a/src/Nest/Aggregations/Metric/Percentiles/PercentilesAggregationJsonConverter.cs +++ b/src/Nest/Aggregations/Metric/Percentiles/PercentilesAggregationJsonConverter.cs @@ -17,37 +17,37 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist var percentiles = new PercentilesAggregation(); ReadMetricProperties(percentiles, properties); percentiles.Method = ReadMethodProperty(properties); - if (properties.ContainsKey("percents")) - percentiles.Percents = properties["percents"].ToObject>(); + if (properties.TryGetValue("percents", out JToken percentsToken)) + percentiles.Percents = percentsToken.ToObject>(); return percentiles; } protected IPercentilesMethod ReadMethodProperty(Dictionary properties) { IPercentilesMethod method = null; - if (properties.ContainsKey("hdr")) - method = properties["hdr"].ToObject(); - else if (properties.ContainsKey("tdigest")) - method = properties["tdigest"].ToObject(); + if (properties.TryGetValue("hdr", out JToken hdrToken)) + method = hdrToken.ToObject(); + else if (properties.TryGetValue("tdigest", out JToken tdigestToken)) + method = tdigestToken.ToObject(); return method; } protected void ReadMetricProperties(IMetricAggregation metric, Dictionary properties) { - if (properties.ContainsKey("field")) - metric.Field = properties["field"].ToString(); + if (properties.TryGetValue("field", out JToken fieldToken)) + metric.Field = fieldToken.ToString(); - if (properties.ContainsKey("script")) + if (properties.TryGetValue("script", out JToken scriptToken)) { - var scriptProps = JObject.FromObject(properties["script"]).Properties().ToDictionary(p => p.Name, p => p.Value); + var scriptProps = JObject.FromObject(scriptToken).Properties().ToDictionary(p => p.Name, p => p.Value); if (scriptProps.ContainsKey("source") || scriptProps.ContainsKey("inline")) - metric.Script = properties["script"].ToObject(); - else if (scriptProps.ContainsKey("id")) - metric.Script = properties["id"].ToObject(); + metric.Script = scriptToken.ToObject(); + else if (scriptProps.TryGetValue("id", out JToken idToken)) + metric.Script = idToken.ToObject(); } - if (properties.ContainsKey("missing")) - metric.Missing = double.Parse(properties["missing"].ToString()); + if (properties.TryGetValue("missing", out JToken missingToken)) + metric.Missing = double.Parse(missingToken.ToString()); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) diff --git a/src/Nest/Aggregations/Pipeline/MovingAverage/MovingAverageAggregationJsonConverter.cs b/src/Nest/Aggregations/Pipeline/MovingAverage/MovingAverageAggregationJsonConverter.cs index 7d6770a4e02..4584f8bf530 100644 --- a/src/Nest/Aggregations/Pipeline/MovingAverage/MovingAverageAggregationJsonConverter.cs +++ b/src/Nest/Aggregations/Pipeline/MovingAverage/MovingAverageAggregationJsonConverter.cs @@ -83,11 +83,11 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s private T GetOrDefault(string key, Dictionary properties) { - if (!properties.ContainsKey(key)) return default(T); + if (!properties.TryGetValue(key, out JToken value)) return default(T); #if DOTNETCORE - return properties[key].ToObject(); + return value.ToObject(); #else - return (T)Convert.ChangeType(properties[key], typeof(T)); + return (T)Convert.ChangeType(value, typeof(T)); #endif } diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs index 0cc77301dfa..387260f9e0f 100644 --- a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs +++ b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs @@ -173,9 +173,9 @@ private void MapIdPropertyFor(Expression> obj var memberInfo = new MemberInfoResolver(objectPath); var fieldName = memberInfo.Members.Single().Name; - if (_idProperties.ContainsKey(typeof(TDocument))) + if (_idProperties.TryGetValue(typeof(TDocument), out string idPropertyFieldName)) { - if (_idProperties[typeof(TDocument)].Equals(fieldName)) return; + if (idPropertyFieldName.Equals(fieldName)) return; throw new ArgumentException( $"Cannot map '{fieldName}' as the id property for type '{typeof(TDocument).Name}': it already has '{_idProperties[typeof(TDocument)]}' mapped."); @@ -191,9 +191,9 @@ private void MapRoutePropertyFor(Expression> var memberInfo = new MemberInfoResolver(objectPath); var fieldName = memberInfo.Members.Single().Name; - if (_routeProperties.ContainsKey(typeof(TDocument))) + if (_routeProperties.TryGetValue(typeof(TDocument), out string routePropertyFieldName)) { - if (_routeProperties[typeof(TDocument)].Equals(fieldName)) return; + if (routePropertyFieldName.Equals(fieldName)) return; throw new ArgumentException( $"Cannot map '{fieldName}' as the route property for type '{typeof(TDocument).Name}': it already has '{_routeProperties[typeof(TDocument)]}' mapped."); @@ -216,10 +216,10 @@ private void ApplyPropertyMappings(IList>(); + if (dict.TryGetValue("lang", out JToken langToken)) + script.Lang = langToken.ToString(); + if (dict.TryGetValue("params", out JToken paramsToken)) + script.Params = paramsToken.ToObject>(); return script; } diff --git a/src/Nest/IndexModules/IndexSettings/Settings/IndexSettingsConverter.cs b/src/Nest/IndexModules/IndexSettings/Settings/IndexSettingsConverter.cs index 296cb161e3f..eaa8e5065d2 100644 --- a/src/Nest/IndexModules/IndexSettings/Settings/IndexSettingsConverter.cs +++ b/src/Nest/IndexModules/IndexSettings/Settings/IndexSettingsConverter.cs @@ -256,9 +256,8 @@ private static void Set(IIndexSettings s, IDictionary sett JsonSerializer serializer = null ) { - if (!settings.ContainsKey(key)) return; + if (!settings.TryGetValue(key, out JProperty v)) return; - var v = settings[key]; var value = serializer == null ? v.Value.ToObject() : v.Value.ToObject(serializer); assign(value); s.Add(key, value); @@ -269,9 +268,8 @@ private static void SetArray(IIndexSettings s, IDictionary assign2, JsonSerializer serializer = null ) { - if (!settings.ContainsKey(key)) return; + if (!settings.TryGetValue(key, out JProperty v)) return; - var v = settings[key]; if (v.Value is JArray) { var value = serializer == null ? v.Value.ToObject() : v.Value.ToObject(serializer); diff --git a/src/Nest/Modules/SnapshotAndRestore/Repositories/GetRepository/GetRepositoryResponse.cs b/src/Nest/Modules/SnapshotAndRestore/Repositories/GetRepository/GetRepositoryResponse.cs index 64c5cc62bc2..1ef47d8c777 100644 --- a/src/Nest/Modules/SnapshotAndRestore/Repositories/GetRepository/GetRepositoryResponse.cs +++ b/src/Nest/Modules/SnapshotAndRestore/Repositories/GetRepository/GetRepositoryResponse.cs @@ -40,9 +40,9 @@ private TRepository Get(string name) where TRepository : class, ISnapshotRepository { if (Repositories == null) return null; - if (!Repositories.ContainsKey(name)) return null; + if (!Repositories.TryGetValue(name, out ISnapshotRepository repository)) return null; - return Repositories[name] as TRepository; + return repository as TRepository; } } } diff --git a/src/Nest/XPack/Watcher/Condition/ScriptConditionBase.cs b/src/Nest/XPack/Watcher/Condition/ScriptConditionBase.cs index a3f2973c897..d0d3720cf3a 100644 --- a/src/Nest/XPack/Watcher/Condition/ScriptConditionBase.cs +++ b/src/Nest/XPack/Watcher/Condition/ScriptConditionBase.cs @@ -68,28 +68,28 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist if (!dict.HasAny()) return null; IScriptCondition scriptCondition = null; - if (dict.ContainsKey("inline")) + if (dict.TryGetValue("inline", out JToken inlineToken)) { - var inline = dict["inline"].ToString(); + var inline = inlineToken.ToString(); scriptCondition = new InlineScriptCondition(inline); } - if (dict.ContainsKey("source")) + if (dict.TryGetValue("source", out JToken sourceToken)) { - var inline = dict["source"].ToString(); + var inline = sourceToken.ToString(); scriptCondition = new InlineScriptCondition(inline); } - if (dict.ContainsKey("id")) + if (dict.TryGetValue("id", out JToken idToken)) { - var id = dict["id"].ToString(); + var id = idToken.ToString(); scriptCondition = new IndexedScriptCondition(id); } if (scriptCondition == null) return null; - if (dict.ContainsKey("lang")) - scriptCondition.Lang = dict["lang"].ToString(); - if (dict.ContainsKey("params")) - scriptCondition.Params = dict["params"].ToObject>(); + if (dict.TryGetValue("lang", out JToken langToken)) + scriptCondition.Lang = langToken.ToString(); + if (dict.TryGetValue("params", out JToken paramsToken)) + scriptCondition.Params = paramsToken.ToObject>(); return scriptCondition; } diff --git a/src/Nest/XPack/Watcher/Input/ChainInput.cs b/src/Nest/XPack/Watcher/Input/ChainInput.cs index 6faf7ee68dd..0bf8a3cbeb3 100644 --- a/src/Nest/XPack/Watcher/Input/ChainInput.cs +++ b/src/Nest/XPack/Watcher/Input/ChainInput.cs @@ -42,10 +42,13 @@ public ChainInputDescriptor() { } /// public ChainInputDescriptor Input(string name, Func selector) { - if (Self.Inputs == null) Self.Inputs = new Dictionary(); - - if (Self.Inputs.ContainsKey(name)) - throw new InvalidOperationException($"An input named '{name}' has already been specified. Choose a different name"); + if (Self.Inputs != null) + { + if (Self.Inputs.ContainsKey(name)) + throw new InvalidOperationException($"An input named '{name}' has already been specified. Choose a different name"); + } + else + Self.Inputs = new Dictionary(); Self.Inputs.Add(name, selector.InvokeOrDefault(new InputDescriptor())); return this; diff --git a/src/Nest/XPack/Watcher/Transform/ScriptTransformBase.cs b/src/Nest/XPack/Watcher/Transform/ScriptTransformBase.cs index 463651d0816..7b935d2f063 100644 --- a/src/Nest/XPack/Watcher/Transform/ScriptTransformBase.cs +++ b/src/Nest/XPack/Watcher/Transform/ScriptTransformBase.cs @@ -67,28 +67,28 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist if (!dict.HasAny()) return null; IScriptTransform scriptTransform = null; - if (dict.ContainsKey("inline")) + if (dict.TryGetValue("inline", out JToken inlineToken)) { - var inline = dict["inline"].ToString(); + var inline = inlineToken.ToString(); scriptTransform = new InlineScriptTransform(inline); } - if (dict.ContainsKey("source")) + if (dict.TryGetValue("source", out JToken sourceToken)) { - var inline = dict["source"].ToString(); + var inline = sourceToken.ToString(); scriptTransform = new InlineScriptTransform(inline); } - if (dict.ContainsKey("id")) + if (dict.TryGetValue("id", out JToken idToken)) { - var id = dict["id"].ToString(); + var id = idToken.ToString(); scriptTransform = new IndexedScriptTransform(id); } if (scriptTransform == null) return null; - if (dict.ContainsKey("lang")) - scriptTransform.Lang = dict["lang"].ToString(); - if (dict.ContainsKey("params")) - scriptTransform.Params = dict["params"].ToObject>(); + if (dict.TryGetValue("lang", out JToken langToken)) + scriptTransform.Lang = langToken.ToString(); + if (dict.TryGetValue("params", out JToken paramsToken)) + scriptTransform.Params = paramsToken.ToObject>(); return scriptTransform; }