Skip to content

Commit

Permalink
Added Array Support for JSON selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepleez committed Jan 13, 2020
1 parent d861184 commit 4913df5
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions taskt/Core/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,28 @@ public static string ConvertToUserVariable(this String str, object sender)
//get the value from the list
var complexJson = matchingVar.GetDisplayValue();

//deserialize into json object
JObject parsedObject = JObject.Parse(complexJson);

//attempt to match based on user defined pattern
var match = parsedObject.SelectToken(jsonPattern);

//check match
if (match != null)
{
//replace with value
str = str.Replace(startVariableMarker + potentialVariable + endVariableMarker, match.ToString());
continue;
JToken match;
if (complexJson.StartsWith("[") && complexJson.EndsWith("]"))
{
//attempt to match array based on user defined pattern
JArray parsedObject = JArray.Parse(complexJson);
match = parsedObject.SelectToken(jsonPattern);
}

else
{
//attempt to match object based on user defined pattern
JObject parsedObject = JObject.Parse(complexJson);
match = parsedObject.SelectToken(jsonPattern);
}

//check match
if (match != null)
{
//replace with value
str = str.Replace(startVariableMarker + potentialVariable + endVariableMarker, match.ToString());
continue;
}

}
}
}
Expand Down Expand Up @@ -161,6 +169,27 @@ public static string ConvertToUserVariable(this String str, object sender)
varCheck.CurrentPosition = directElementIndex;
str = str.Replace(searchVariable, (string)varCheck.GetDisplayValue());
varCheck.CurrentPosition = savePosition;
}
else if (varCheck.VariableValue is DataTable && potentialVariable.Split('.').Length == 2)
{
//user is trying to get data from column name or index
string columnName = potentialVariable.Split('.')[1];
var dt = varCheck.VariableValue as DataTable;

string cellItem;
if (int.TryParse(columnName, out var columnIndex))
{
cellItem = dt.Rows[varCheck.CurrentPosition].Field<object>(columnIndex).ToString();
}
else
{
cellItem = dt.Rows[varCheck.CurrentPosition].Field<object>(columnName).ToString();
}


str = str.Replace(searchVariable, cellItem);


}
else if (potentialVariable.Split('.').Length == 2) // This handles vVariable.count
{
Expand Down

0 comments on commit 4913df5

Please sign in to comment.