Skip to content

Commit

Permalink
Keep track of dictionary type on resource (closes #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbarbettini committed Jun 4, 2017
1 parent 014e038 commit 13c93c4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/Okta.Sdk.UnitTests/ResourceCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Okta.Sdk.Abstractions;

namespace Okta.Sdk.UnitTests
{
public class ResourceCreator<T>
where T : Resource, new()
{
private readonly IChangeTrackingDictionary<string, object> _data
= new DefaultChangeTrackingDictionary(keyComparer: StringComparer.OrdinalIgnoreCase);
private readonly IDictionary<string, object> _data = new Dictionary<string, object>();

public ResourceCreator<T> With(Expression<Func<T, object>> propertySelector, object value)
{
Expand All @@ -38,7 +37,7 @@ public ResourceCreator<T> With(params (Expression<Func<T, object>> propertySelec
public static implicit operator T(ResourceCreator<T> creator)
{
var factory = new ResourceFactory();
return factory.Create<T>(creator._data);
return factory.CreateNew<T>(creator._data);
}

private static PropertyInfo GetPropertyName<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda)
Expand Down
9 changes: 4 additions & 5 deletions src/Okta.Sdk/DefaultDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ private HttpResponse<T> HandleResponse<T>(HttpResponse<string> response)
throw new InvalidOperationException("The response from the RequestExecutor was null.");
}

var dictionary = _serializer.Deserialize(response.Payload ?? string.Empty);
var changeTrackingDictionary = new DefaultChangeTrackingDictionary(dictionary, StringComparer.OrdinalIgnoreCase);
var data = _serializer.Deserialize(response.Payload ?? string.Empty);

if (response.StatusCode != 200)
{
throw new OktaApiException(response.StatusCode, _resourceFactory.Create<Resource>(changeTrackingDictionary));
throw new OktaApiException(response.StatusCode, _resourceFactory.CreateNew<Resource>(data));
}

var resource = _resourceFactory.Create<T>(changeTrackingDictionary);
var resource = _resourceFactory.CreateNew<T>(data);

return new HttpResponse<T>
{
Expand Down Expand Up @@ -79,7 +78,7 @@ public async Task<HttpResponse<IEnumerable<T>>> GetArrayAsync<T>(string href, Ca

var resources = _serializer
.DeserializeArray(response.Payload ?? string.Empty)
.Select(x => _resourceFactory.Create<T>(new DefaultChangeTrackingDictionary(x, StringComparer.OrdinalIgnoreCase)));
.Select(x => _resourceFactory.CreateNew<T>(x));

return new HttpResponse<IEnumerable<T>>
{
Expand Down
6 changes: 4 additions & 2 deletions src/Okta.Sdk/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public Resource(ResourceDictionaryType dictionaryType)
Initialize(null);
}

internal ResourceDictionaryType DictionaryType => _dictionaryType;

public void Initialize(IDictionary<string, object> data)
{
_data = data ?? _resourceFactory.NewDictionary(_dictionaryType);
_data = data ?? _resourceFactory.NewDictionary(_dictionaryType, null);
}

public IDictionary<string, object> GetModifiedData()
Expand Down Expand Up @@ -111,7 +113,7 @@ public T GetProperty<T>(string key)
where T : Resource, new()
{
var nestedData = GetProperty(key) as IDictionary<string, object>;
return _resourceFactory.Create<T>(nestedData);
return _resourceFactory.CreateFromExistingData<T>(nestedData);
}
}
}
19 changes: 15 additions & 4 deletions src/Okta.Sdk/ResourceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@ namespace Okta.Sdk
{
public sealed class ResourceFactory
{
public IDictionary<string, object> NewDictionary(ResourceDictionaryType type)
public IDictionary<string, object> NewDictionary(ResourceDictionaryType type, IDictionary<string, object> existingData)
{
var initialData = existingData ?? new Dictionary<string, object>();

switch (type)
{
case ResourceDictionaryType.Default: return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
case ResourceDictionaryType.ChangeTracking: return new DefaultChangeTrackingDictionary(keyComparer: StringComparer.OrdinalIgnoreCase);
case ResourceDictionaryType.Default: return new Dictionary<string, object>(initialData, StringComparer.OrdinalIgnoreCase);
case ResourceDictionaryType.ChangeTracking: return new DefaultChangeTrackingDictionary(initialData, StringComparer.OrdinalIgnoreCase);
}

throw new ArgumentException($"Unknown resource dictionary type {type}");
}

public T Create<T>(IDictionary<string, object> data)
public T CreateFromExistingData<T>(IDictionary<string, object> data)
where T : Resource, new()
{
var resource = new T();
resource.Initialize(data);
return resource;
}

public T CreateNew<T>(IDictionary<string, object> data)
where T : Resource, new()
{
var resource = new T();
var dictionary = NewDictionary(resource.DictionaryType, data);
resource.Initialize(dictionary);
return resource;
}
}
}

0 comments on commit 13c93c4

Please sign in to comment.