From 1911b4a3e78a73e9ab7f3c85adca64c4cb27147f Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Sat, 12 Nov 2016 15:09:41 -0500 Subject: [PATCH] object serialization yo --- .../ExtensionTests.cs | 10 +++++ src/UriBuilder.Fluent.UnitTests/project.json | 2 +- .../TerribleDevUriExtensions.cs | 18 ++++++++ src/UriBuilder.Fluent/project.json | 41 +++++++++++-------- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs index 308880a..8418956 100644 --- a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs +++ b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs @@ -116,5 +116,15 @@ namespace FluentUriBuilder.Tests .WithParameter("supgf", "no22"); Assert.Equal("http://awesome.com/?awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString()); } + [Fact] + public void TestSerializedParameters() + { + var url = new UriBuilder("http://awesome.com") + .WithSerializedObject(new {yo="dawg"}); + Assert.Equal("http://awesome.com/?yo=dawg", url.Uri.ToString()); + url = new UriBuilder("http://awesome.com") + .WithSerializedObject(new {yo=""}); + Assert.Equal("http://awesome.com/?yo", url.Uri.ToString()); + } } } \ No newline at end of file diff --git a/src/UriBuilder.Fluent.UnitTests/project.json b/src/UriBuilder.Fluent.UnitTests/project.json index d847271..08bc117 100644 --- a/src/UriBuilder.Fluent.UnitTests/project.json +++ b/src/UriBuilder.Fluent.UnitTests/project.json @@ -9,7 +9,7 @@ }, "frameworks": { - "net461": {}, + "net462": {}, "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { diff --git a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs index ac3f8ea..58a3198 100644 --- a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs +++ b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -10,6 +11,8 @@ namespace System { public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values); + public static UriBuilder WithParameter(this UriBuilder bld, string key, object value) => bld.WithParameter(key, new object[1] { value }); + public static UriBuilder WithParameter(this UriBuilder bld, string key, IEnumerable valuesEnum) { if(string.IsNullOrWhiteSpace(key)) @@ -74,5 +77,20 @@ namespace System bld.Scheme = predicate ? "https" : "http"; return bld; } + +#if netstandard15 + + public static UriBuilder WithSerializedObject(this UriBuilder bld, object serialize) + { + var types = serialize.GetType().GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty); + foreach(var type in types) + { + bld.WithParameter(type.Name, type.GetValue(serialize, null)); + } + + return bld; + } + +#endif } } \ No newline at end of file diff --git a/src/UriBuilder.Fluent/project.json b/src/UriBuilder.Fluent/project.json index 1694896..0f99b4b 100644 --- a/src/UriBuilder.Fluent/project.json +++ b/src/UriBuilder.Fluent/project.json @@ -1,30 +1,37 @@ { "version": "1.0.0-*", "packOptions": { - "owners": [ - "Tommy Parnell" - ], - "repository": { - "url":"https://github.com/TerribleDev/UriBuilder.Fluent" - }, - "summary": "Fluent extensions for UriBuilder", - "tags": [ - "Url building", - "Uri", - "Uri building", - "fluent", - "extension" - ] + "owners": [ + "Tommy Parnell" + ], + "repository": { + "url": "https://github.com/TerribleDev/UriBuilder.Fluent" + }, + "summary": "Fluent extensions for UriBuilder", + "tags": [ + "Url building", + "Uri", + "Uri building", + "fluent", + "extension" + ] }, "dependencies": { "NETStandard.Library": "1.6.0" }, "authors": [ - "Tommy Parnell" + "Tommy Parnell" ], "frameworks": { "netstandard1.3": { - "imports": "dnxcore50" + "buildOptions": { + "define": [ "netstandard13" ] + } + }, + "netstandard1.5": { + "buildOptions": { + "define": [ "netstandard15" ] + } } } -} +} \ No newline at end of file