Skip to content

Commit

Permalink
[feat] Add ability to construct Options object from dictionary (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 authored Feb 13, 2024
1 parent 35d46e1 commit 2f9c8a5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions EasyPost.Tests/ModelsTests/OptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using EasyPost.Models.API;
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
using EasyPost.Utilities.Internal;
using Xunit;

namespace EasyPost.Tests.ModelsTests;
Expand Down Expand Up @@ -37,4 +39,19 @@ public void TestOptionsSerialization()
Assert.True(dictionary.ContainsKey(unsupportedOption));
Assert.Equal(unsupportedValue, dictionary[unsupportedOption]);
}

[Fact]
[Testing.Properties]
public void TestOptionsFromDictionary()
{
var dictionary = new Dictionary<string, object>
{
{ "alcohol", true },
};

var options = Options.FromDictionary(dictionary);

// Data should be deserialized correctly and values set accordingly
Assert.True(options.Alcohol);
}
}
3 changes: 3 additions & 0 deletions EasyPost/Exceptions/General/MissingPropertyError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public class MissingPropertyError : EasyPostError
/// <param name="obj">Object missing the property.</param>
/// <param name="propertyName">Name of the missing property.</param>
#pragma warning disable CA2241
#pragma warning disable IDE0300
internal MissingPropertyError(object obj, object propertyName)
// ReSharper disable once UseCollectionExpression
: base(string.Format(CultureInfo.InvariantCulture, Constants.ErrorMessages.MissingProperty, new[] { obj.GetType().Name, propertyName }))
#pragma warning restore IDE0300
#pragma warning restore CA2241
{
}
Expand Down
14 changes: 14 additions & 0 deletions EasyPost/Models/API/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ public class Options : EasyPostObject
/// <param name="value">Value of the option to add.</param>
public void AddAdditionalOption(string key, object value) => _additionalOptions.Add(key, value);

/// <inheritdoc />
public override Dictionary<string, object> AsDictionary()
{
Dictionary<string, object> data = base.AsDictionary();
Expand All @@ -1004,5 +1005,18 @@ public override Dictionary<string, object> AsDictionary()

return data;
}

/// <summary>
/// Create an <see cref="Options"/> object from a dictionary.
/// WARNING: This method involves serializing and deserializing the data, which can be slow. Use sparingly.
/// </summary>
/// <param name="data">A <see cref="Dictionary{TKey,TValue}"/> of key-value pairs of options.</param>
/// <returns>An <see cref="Options"/> object.</returns>
public static Options? FromDictionary(Dictionary<string, object> data)
{
// Take the incoming dictionary, convert to JSON, then deserialize into an Options object
string jsonData = JsonConvert.SerializeObject(data);
return JsonConvert.DeserializeObject<Options>(jsonData);
}
}
}

0 comments on commit 2f9c8a5

Please sign in to comment.