Skip to content

Commit

Permalink
Merge pull request #243 from kasperk81/main
Browse files Browse the repository at this point in the history
Removes LINQ usages from product code
  • Loading branch information
andrueastman authored May 27, 2024
2 parents 857c2b3 + 458fa94 commit 173ecb9
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 115 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.9.1] - 2024-05-24

### Changed

- Removes LINQ usages from product code.

## [1.9.1] - 2024-05-13

### Changed
Expand Down
38 changes: 25 additions & 13 deletions src/ApiClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ------------------------------------------------------------------------------

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Kiota.Abstractions.Serialization;
using Microsoft.Kiota.Abstractions.Store;

Expand Down Expand Up @@ -74,26 +74,38 @@ public static IParseNodeFactory EnableBackingStoreForParseNodeFactory(IParseNode

return result;
}

private static void EnableBackingStoreForParseNodeRegistry(ParseNodeFactoryRegistry registry)
{
foreach(var entry in registry
.ContentTypeAssociatedFactories
.Where(x => !(x.Value is BackingStoreParseNodeFactory ||
x.Value is ParseNodeFactoryRegistry))
.ToList()) // earlier versions of .NET do not allow direct modification of dictionary values while iterating
var keysToUpdate = new List<string>();
foreach(var entry in registry.ContentTypeAssociatedFactories)
{
if(entry.Value is not (BackingStoreSerializationWriterProxyFactory or SerializationWriterFactoryRegistry))
{
keysToUpdate.Add(entry.Key);
}
}

foreach(var key in keysToUpdate)
{
registry.ContentTypeAssociatedFactories[entry.Key] = new BackingStoreParseNodeFactory(entry.Value);
registry.ContentTypeAssociatedFactories[key] = new BackingStoreParseNodeFactory(registry.ContentTypeAssociatedFactories[key]);
}
}

private static void EnableBackingStoreForSerializationRegistry(SerializationWriterFactoryRegistry registry)
{
foreach(var entry in registry
.ContentTypeAssociatedFactories
.Where(x => !(x.Value is BackingStoreSerializationWriterProxyFactory ||
x.Value is SerializationWriterFactoryRegistry))
.ToList()) // earlier versions of .NET do not allow direct modification of dictionary values while iterating
var keysToUpdate = new List<string>();
foreach(var entry in registry.ContentTypeAssociatedFactories)
{
if(entry.Value is not (BackingStoreSerializationWriterProxyFactory or SerializationWriterFactoryRegistry))
{
keysToUpdate.Add(entry.Key);
}
}

foreach(var key in keysToUpdate)
{
registry.ContentTypeAssociatedFactories[entry.Key] = new BackingStoreSerializationWriterProxyFactory(entry.Value);
registry.ContentTypeAssociatedFactories[key] = new BackingStoreSerializationWriterProxyFactory(registry.ContentTypeAssociatedFactories[key]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.9.1</VersionPrefix>
<VersionPrefix>1.9.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>false</SignAssembly>
Expand Down
3 changes: 1 addition & 2 deletions src/MultipartBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.Kiota.Abstractions.Serialization;
Expand Down Expand Up @@ -126,7 +125,7 @@ public void Serialize(ISerializationWriter writer)
contentDispositionBuilder.Append(part.Name);
contentDispositionBuilder.Append("\"");

if (part.FileName != null)
if(part.FileName != null)
{
contentDispositionBuilder.Append("; filename=\"");
contentDispositionBuilder.Append(part.FileName);
Expand Down
69 changes: 62 additions & 7 deletions src/RequestHeaders.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Kiota.Abstractions;

Expand Down Expand Up @@ -35,6 +34,7 @@ public void Add(string headerName, params string[] headerValues)
else
_headers.Add(headerName, new HashSet<string>(headerValues));
}

/// <summary>
/// Adds values to the header with the specified name if it's not already present
/// </summary>
Expand All @@ -54,14 +54,30 @@ public bool TryAdd(string headerName, string headerValue)
}
return false;
}

/// <inheritdoc/>
public ICollection<string> Keys => _headers.Keys;

/// <inheritdoc/>
public ICollection<IEnumerable<string>> Values => _headers.Values.Cast<IEnumerable<string>>().ToList();
public ICollection<IEnumerable<string>> Values
{
get
{
var values = new List<IEnumerable<string>>();
foreach(var value in _headers.Values)
{
values.Add(value);
}
return values;
}
}

/// <inheritdoc/>
public int Count => _headers.Count;

/// <inheritdoc/>
public bool IsReadOnly => false;

/// <inheritdoc/>
public IEnumerable<string> this[string key] { get => TryGetValue(key, out var result) ? result : throw new KeyNotFoundException($"Key not found : {key}"); set => Add(key, value); }

Expand All @@ -85,6 +101,7 @@ public bool Remove(string headerName, string headerValue)
}
return false;
}

/// <summary>
/// Adds all the headers values from the specified headers collection.
/// </summary>
Expand All @@ -97,19 +114,36 @@ public void AddAll(RequestHeaders headers)
foreach(var value in header.Value)
Add(header.Key, value);
}

/// <summary>
/// Removes all headers.
/// </summary>
public void Clear()
{
_headers.Clear();
}

/// <inheritdoc/>
public bool ContainsKey(string key) => !string.IsNullOrEmpty(key) && _headers.ContainsKey(key);

/// <inheritdoc/>
#pragma warning disable CS8604 // Possible null reference argument. //Can't change signature of overriden method implementation
public void Add(string key, IEnumerable<string> value) => Add(key, value?.ToArray());
#pragma warning restore CS8604 // Possible null reference argument.
public void Add(string key, IEnumerable<string> value)
{
if(value == null)
{
Add(key, null!);
}
else
{
var valueArray = new List<string>();
foreach(var v in value)
{
valueArray.Add(v);
}
Add(key, valueArray.ToArray());
}
}

/// <inheritdoc/>
public bool Remove(string key)
{
Expand All @@ -127,15 +161,36 @@ public bool TryGetValue(string key, out IEnumerable<string> value)
value = values;
return true;
}
value = Enumerable.Empty<string>();
value = [];
return false;
}

/// <inheritdoc/>
public void Add(KeyValuePair<string, IEnumerable<string>> item) => Add(item.Key, item.Value);

/// <inheritdoc/>
public bool Contains(KeyValuePair<string, IEnumerable<string>> item) => TryGetValue(item.Key, out var values) && item.Value.All(x => values.Contains(x)) && values.Count() == item.Value.Count();
public bool Contains(KeyValuePair<string, IEnumerable<string>> item)
{
if(!TryGetValue(item.Key, out var values)) return false;

var itemValueList = new List<string>(item.Value);

int valuesCount = 0;
foreach(var value in values)
{
valuesCount++;
if(!itemValueList.Contains(value))
{
return false;
}
}

return valuesCount == itemValueList.Count;
}

/// <inheritdoc/>
public void CopyTo(KeyValuePair<string, IEnumerable<string>>[] array, int arrayIndex) => throw new NotImplementedException();

/// <inheritdoc/>
public bool Remove(KeyValuePair<string, IEnumerable<string>> item)
{
Expand Down
Loading

0 comments on commit 173ecb9

Please sign in to comment.