Skip to content

Commit

Permalink
Object Extension QueryString added. The method supports to create a Q…
Browse files Browse the repository at this point in the history
…ueryString from an object (simple/primitives/lists types).
  • Loading branch information
Bruno de Souza Melo committed Sep 10, 2023
1 parent ec34507 commit 16eeb5e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/NuvTools.Common/NuvTools.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>NuvTools.Common.snk</AssemblyOriginatorKeyFile>
<Description>Common library for Web, Desktop and Mobile (MAUI) applications.</Description>
<Version>7.1.0</Version>
<Version>7.1.1</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageIcon>icon.png</PackageIcon>
Expand Down
59 changes: 59 additions & 0 deletions src/NuvTools.Common/Web/ObjectExtensions.cs
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;
}
}
2 changes: 1 addition & 1 deletion tests/NuvTools.Common.Test/NuvTools.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<Version>7.0.2</Version>
<Version>7.1.1</Version>
<Authors>Nuv Tools</Authors>
<Copyright>Copyright © 2023 Nuv Tools</Copyright>
<PackageProjectUrl>https://nuv.tools</PackageProjectUrl>
Expand Down
37 changes: 37 additions & 0 deletions tests/NuvTools.Common.Test/Web/QueryStringTest.cs
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"));
}
}

0 comments on commit 16eeb5e

Please sign in to comment.