Skip to content

Commit

Permalink
Merge pull request #456 from chadiel/dev
Browse files Browse the repository at this point in the history
Update PsObjectExtensions.cs: hotfix for set-azureresource issue
  • Loading branch information
markcowl committed Jun 2, 2015
2 parents 59defdd + 2f0f889 commit 2405634
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,36 @@ private static JToken ToJToken(object value)
return obj;
}

var valueAsArray = value as Array;
if (valueAsArray != null)
var valueAsDictionary = value as IDictionary;
if (valueAsDictionary != null)
{
var retVal = new JToken[valueAsArray.Length];
for (int i = 0; i < valueAsArray.Length; ++i)
JObject obj = new JObject();
var dictionaryEntries = valueAsDictionary is IDictionary<string, object>
? valueAsDictionary.OfType<KeyValuePair<string, object>>().Select(kvp => Tuple.Create(kvp.Key, kvp.Value))
: valueAsDictionary.OfType<DictionaryEntry>().Select(dictionaryEntry => Tuple.Create(dictionaryEntry.Key.ToString(), dictionaryEntry.Value));

dictionaryEntries = dictionaryEntries.Any(dictionaryEntry => dictionaryEntry.Item1.EqualsInsensitively(Constants.MicrosoftAzureResource))
? dictionaryEntries.Where(dictionaryEntry => !PsObjectExtensions.PropertiesToRemove.ContainsKey(dictionaryEntry.Item1))
: dictionaryEntries;

foreach (var dictionaryEntry in dictionaryEntries)
{
obj.Add(dictionaryEntry.Item1, PsObjectExtensions.ToJToken(dictionaryEntry.Item2));
}

return obj;
}

var valueAsIList = value as IList;
if (valueAsIList != null)
{
var tmpList = new List<JToken>();
foreach(var v in valueAsIList)
{
retVal[i] = PsObjectExtensions.ToJToken(valueAsArray.GetValue(i));
tmpList.Add(PsObjectExtensions.ToJToken(v));
}

return JArray.FromObject(retVal);
return JArray.FromObject(tmpList.ToArray());
}

return new JValue(value.ToString());
Expand Down

0 comments on commit 2405634

Please sign in to comment.