Skip to content

Commit

Permalink
Replace ToList with AsDictionary extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tekgator committed Dec 6, 2022
1 parent f61da74 commit 4c5191b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [1.1.1] - 2022-12-06
### Changed
- Replace ToList with AsDictionary extension
- Remove JarTypeItems model


## [1.1.0] - 2022-12-06
### Changed
- Remove JsonPropertyName attribute from models and work with JsonSerializerOptions case insensitive property instead
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ var serverJar = new ServerJars();
var types = await serverJar.GetTypes();
Console.WriteLine(JsonSerializer.Serialize(types, jsonOptions));

// GetTypes.ToList() extension
// GetTypes.AsDictionary() extension
SetConsoleColor(ConsoleColor.White, ConsoleColor.Red);
Console.WriteLine("\nAPI call - GetTypes.ToList():\n");
Console.WriteLine("\nAPI call - GetTypes.AsDictionary():\n");
ResetConsoleColor();

types.ToList().ForEach(t => Console.WriteLine(t.ToString()));
var dict = types.AsDictionary();
Console.WriteLine(string.Join(Environment.NewLine, dict.Select((kv) => $"{kv.Key}: {string.Join(", ", kv.Value)}")));

// GetDetails
var details = await serverJar.GetDetails("servers", "spigot", "1.19.1");
Expand Down
7 changes: 4 additions & 3 deletions ServerJars.Demo.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
var types = await serverJar.GetTypes();
Console.WriteLine(JsonSerializer.Serialize(types, jsonOptions));

// GetTypes.ToList() extension
// GetTypes.AsDictionary() extension
SetConsoleColor(ConsoleColor.White, ConsoleColor.Red);
Console.WriteLine("\nAPI call - GetTypes.ToList() extension:\n");
Console.WriteLine("\nAPI call - GetTypes.AsDictionary() extension:\n");
ResetConsoleColor();

types.ToList().ForEach(t => Console.WriteLine(t.ToString()));
var dict = types.AsDictionary();
Console.WriteLine(string.Join(Environment.NewLine, dict.Select((kv) => $"{kv.Key}: {string.Join(", ", kv.Value)}")));


// GetDetails
Expand Down
11 changes: 7 additions & 4 deletions ServerJars.Tests/ServerJarsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public void GetTypes_InvalidType(string type)

[TestCase("")]
[TestCase("servers")]
public async Task GetTypes_SuccessAsList(string type)
public async Task GetTypes_SuccessAsDictionary(string type)
{
var types = (await _serverJars.GetTypes(type)).ToList();
var item = types.FirstOrDefault(t => t.Category == "servers");
var types = (await _serverJars.GetTypes(type)).AsDictionary();

Assert.That(item, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(types.ContainsKey("servers"), Is.True);
Assert.That(types["servers"].Count(), Is.GreaterThan(0));
});
}

[TestCase("servers", "spigot", "")]
Expand Down
19 changes: 9 additions & 10 deletions ServerJars/Extensions/JarTypesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ namespace ServerJarsAPI.Extensions;

public static class JarTypesExtensions
{
public static List<JarTypeItem> ToList(this JarTypes jarTypes)
public static Dictionary<string, IEnumerable<string>> AsDictionary(this JarTypes jarTypes)
{
List<JarTypeItem> list = new();

list.AddRange(jarTypes.Vanilla.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Vanilla).ToLower(), Type = v }));
list.AddRange(jarTypes.Bedrock.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Bedrock).ToLower(), Type = v }));
list.AddRange(jarTypes.Servers.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Servers).ToLower(), Type = v }));
list.AddRange(jarTypes.Modded.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Modded).ToLower(), Type = v }));
list.AddRange(jarTypes.Proxies.Select(v => new JarTypeItem() { Category = nameof(jarTypes.Proxies).ToLower(), Type = v }));

return list;
return new()
{
{ nameof(jarTypes.Vanilla).ToLower(), jarTypes.Vanilla },
{ nameof(jarTypes.Bedrock).ToLower(), jarTypes.Bedrock },
{ nameof(jarTypes.Servers).ToLower(), jarTypes.Servers },
{ nameof(jarTypes.Modded).ToLower(), jarTypes.Modded },
{ nameof(jarTypes.Proxies).ToLower(), jarTypes.Proxies },
};
}
}
9 changes: 0 additions & 9 deletions ServerJars/Models/JarTypeItem.cs

This file was deleted.

0 comments on commit 4c5191b

Please sign in to comment.