Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Combine ExpandoObjectAdapter and DictionaryAdapter #106

Merged
merged 6 commits into from
Sep 5, 2017

Conversation

jbagga
Copy link
Contributor

@jbagga jbagga commented Aug 30, 2017

Refactoring some code to combine the two (similar) adapters, ensuring both types use the JsonDictionaryContract and resolve the dictionary keys.

May need more tests. This is just to get feedback on some generic type conversion. See my comment below.

cc @rynowak

}
}
}
return newValue;
}

private static T TryParse<T>(string value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rynowak Can we talk about what's the best way to either combine the two TryParse methods or if there is a better way to convert keys and values to TKey and TValue?

@jbagga jbagga requested a review from rynowak August 30, 2017 02:27
@jbagga jbagga assigned rynowak and kichalla and unassigned rynowak Aug 30, 2017
dictionary[key] = ConvertValue(dictionary, key, value);
var convertedKey = TryParse<TKey>(key);
var convertedValue = TryParse<TValue>(value);
dictionary[convertedKey] = ConvertValue(dictionary, convertedKey, convertedValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of TryParse methods, could you use ConversionResultProvider.ConvertTo for TKey (this could also solve the issue of non-string keys in dictionary) and use ConvertValue for TValue (you need not use TryParse for value as you are anyway going to convert the value)?

@jbagga
Copy link
Contributor Author

jbagga commented Aug 31, 2017

@kichalla @rynowak Updated


// As per JsonPatch spec, if a key already exists, adding should replace the existing value
dictionary[key] = ConvertValue(dictionary, key, value);
var convertedKey = (TKey)ConversionResultProvider.ConvertTo(key, typeof(TKey)).ConvertedInstance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this conversion fails? Casting a null value to int for instance will throw an exception

{
return new DictionaryAdapter();
var type = typeof(DictionaryAdapter<,>).MakeGenericType(jsonDictionaryContract.DictionaryKeyType, jsonDictionaryContract.DictionaryValueType);
return (IAdapter)Activator.CreateInstance(type);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may still need to handle the case where an object is an IDictionary but not an IDictionary<>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've decided not to do anything about this.

@@ -18,11 +17,11 @@ public void Add_KeyWhichAlreadyExists_ReplacesExistingValue()
var nameKey = "Name";
var dictionary = new Dictionary<string, object>(StringComparer.Ordinal);
dictionary[nameKey] = "Mike";
var dictionaryAdapter = new DictionaryAdapter();
var resolver = new Mock<IContractResolver>(MockBehavior.Strict);
var dictionaryAdapter = new DictionaryAdapter<string, object>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need some tests for things besides <string, object>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on adding these

@@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the expando object tests need an update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no unit tests for ExpandoObjectAdapter. Only integration tests, which pass using the new adapter

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lel ok

@jbagga
Copy link
Contributor Author

jbagga commented Sep 1, 2017

@rynowak Changed key and value conversion code


// As per JsonPatch spec, if a key already exists, adding should replace the existing value
dictionary[key] = ConvertValue(dictionary, key, value);
var canConvertKey = TryConvertKey(key, out var convertedKey, out errorMessage);
if (!canConvertKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just merge this into the if if you want it to be more terse.

}
else
{
errorMessage = Resources.FormatInvalidValueForProperty(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good message The value '{0}' is invalid for target location.

}
else
{
errorMessage = Resources.FormatInvalidValueForPath(key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this error message since we're just looking at the 'key' not a whole path.

Can we say something like:
The provided path segment '{0}' cannot be converted to the target type.

@rynowak
Copy link
Member

rynowak commented Sep 1, 2017

This is looking pretty good now. Just waiting on a new error message and some tests.

@jbagga jbagga changed the title [Design] Combine ExpandoObjectAdapter and DictionaryAdapter Combine ExpandoObjectAdapter and DictionaryAdapter Sep 1, 2017
@jbagga
Copy link
Contributor Author

jbagga commented Sep 1, 2017

@rynowak Updated with tests

@jbagga jbagga merged commit 0f51c56 into dev Sep 5, 2017
@jbagga jbagga deleted the jbagga/DictionaryAdapterRefactor branch September 5, 2017 23:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants