-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Object Extension QueryString added. The method supports to create a Q…
…ueryString from an object (simple/primitives/lists types).
- Loading branch information
Bruno de Souza Melo
committed
Sep 10, 2023
1 parent
ec34507
commit 16eeb5e
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using NuvTools.Common.Serialization; | ||
using System.Collections; | ||
using System.Globalization; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
|
||
namespace NuvTools.Common.Web; | ||
|
||
public static class ObjectExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the query string url from the object and uriBase. Only simple/primitives/lists types are converted. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="obj">Object source to generate QueryString</param> | ||
/// <param name="uriBase">Uri base address</param> | ||
/// <returns></returns> | ||
public static string GetQueryString<T>(this T obj, string uriBase) where T : class | ||
{ | ||
var properties = from p in obj.GetType().GetProperties() | ||
where p.GetValue(obj, null) != null | ||
select p; | ||
|
||
if (!properties.Any()) | ||
return uriBase; | ||
|
||
var list = new List<KeyValuePair<string, string>>(); | ||
|
||
foreach (var item in properties) | ||
{ | ||
var itemValue = item.GetValue(obj, null); | ||
var itemType = itemValue.GetType(); | ||
|
||
if (itemType.IsSimple()) | ||
{ | ||
var value = itemType == typeof(DateTime) ? | ||
((DateTime)itemValue).ToString("yyyy-MM-ddThh:mm:ss", CultureInfo.InvariantCulture) | ||
: itemValue.ToString(); | ||
|
||
list.Add(new KeyValuePair<string, string>(item.Name, value)); | ||
continue; | ||
} | ||
|
||
if (!itemType.IsArray && !itemType.IsList()) continue; | ||
|
||
var listValue = (itemType.IsArray ? (Array)itemValue : (IEnumerable)itemValue).Cast<object>(); | ||
|
||
if (!listValue.Any(e => e.GetType().IsSimple())) continue; | ||
|
||
var a = listValue.Select(e => new KeyValuePair<string, string>(item.Name, e.ToString())); | ||
|
||
list.AddRange(a); | ||
} | ||
|
||
var result = $"{uriBase}?{string.Join('&', list.Select(e => $"{UrlEncoder.Default.Encode(e.Key)}={UrlEncoder.Default.Encode(e.Value)}"))}"; | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using NUnit.Framework; | ||
using NuvTools.Common.Web; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NuvTools.Common.Tests.Web; | ||
|
||
public class QueryStringTest | ||
{ | ||
public class FakeTest | ||
{ | ||
public int Id { get; set; } | ||
public DateTime? Date { get; set; } | ||
public string Name { get; set; } | ||
public List<long> Codes { get; set; } | ||
} | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
} | ||
|
||
[Test] | ||
public void Test1() | ||
{ | ||
var obj = new FakeTest | ||
{ | ||
Date = new DateTime(2023, 1, 1), | ||
Id = 1, | ||
Name = "Hello World!", | ||
Codes = new List<long> { 1, 2, 3 } | ||
}; | ||
|
||
var queryString = obj.GetQueryString("https://nuv.tools"); | ||
Assert.That(queryString, Is.EqualTo("https://nuv.tools?Id=1&Date=2023-01-01T12%3A00%3A00&Name=Hello%20World!&Codes=1&Codes=2&Codes=3")); | ||
} | ||
} |