Skip to content

Commit

Permalink
safely extract dictionary value
Browse files Browse the repository at this point in the history
- added a function to safely extract the dictionary value from the dictionary we prepare for the AST
  • Loading branch information
dnenov committed May 22, 2024
1 parent 0d4fe67 commit 0fb562b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/Libraries/CoreNodeModels/DefineData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ public DefineData() : base(">")
{
PropertyChanged += OnPropertyChanged;

//Items.Add(new DynamoDropDownItem("Select a type", null));

foreach (var dataType in Data.DataNodeDynamoTypeList)
{
var displayName = dataType.Name;
Expand Down Expand Up @@ -164,11 +162,8 @@ public override void Dispose()
DataBridge.Instance.UnregisterCallback(GUID.ToString());
}

private static readonly string BuiltinDictionaryTypeName = typeof(DesignScript.Builtin.Dictionary).FullName;
private static readonly string BuiltinDictionaryGet = nameof(DesignScript.Builtin.Dictionary.ValueAtKey);

public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
{
{
var resultAst = new List<AssociativeNode>();

// function call inputs - reference to the function, and the function arguments coming from the inputs
Expand All @@ -190,14 +185,15 @@ public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode
resultAst.Add(AstFactory.BuildAssignment(functionCallIdentifier, functionCall));

// Next add the first key value pair to the output port
var getFirstKey = AstFactory.BuildFunctionCall(BuiltinDictionaryTypeName, BuiltinDictionaryGet,
new List<AssociativeNode> { functionCallIdentifier, AstFactory.BuildStringNode(">") });
var safeExtractDictionaryValue = new Func<Dictionary<string, object>, string, object>(DSCore.Data.SafeExtractDictionaryValue);
var getFirstKey = AstFactory.BuildFunctionCall(safeExtractDictionaryValue,
[functionCallIdentifier, AstFactory.BuildStringNode(">")]);

resultAst.Add(AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), getFirstKey));

// Second get the key value pair to pass to the databridge callback
var getSecondKey = AstFactory.BuildFunctionCall(BuiltinDictionaryTypeName, BuiltinDictionaryGet,
new List<AssociativeNode> { functionCallIdentifier, AstFactory.BuildStringNode("Validation") });
var getSecondKey = AstFactory.BuildFunctionCall(safeExtractDictionaryValue,
[functionCallIdentifier, AstFactory.BuildStringNode("Validation")]);

resultAst.Add(AstFactory.BuildAssignment(
AstFactory.BuildIdentifier(GUID + "_db"),
Expand All @@ -207,10 +203,6 @@ public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode
}


/// <summary>
/// Not sure at the moment how relevant is the databridge for this node type
/// </summary>
/// <param name="data"></param>
private void DataBridgeCallback(object data)
{
//Todo If the playerValue is not empty string then we can chanage the UI to reflect the value is coming from the player
Expand Down
17 changes: 17 additions & 0 deletions src/Libraries/CoreNodes/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,23 @@ static Data()
DataNodeDynamoTypeList = new ReadOnlyCollection<DataNodeDynamoType>(typeList);
}

/// <summary>
/// A helper function to safely extract a dictionary value
/// </summary>
/// <param name="dict">The dictionary to extract the value from</param>
/// <param name="key">The key of the key/value pair</param>
/// <returns></returns>
[IsVisibleInDynamoLibrary(false)]
public static object SafeExtractDictionaryValue(Dictionary<string, object> dict, string key)
{
if (dict?.TryGetValue(key, out var value) == true)
{
return value;
}

return null;
}

/// <summary>
/// This is the function used by AST
/// Handles some of the the node logic while performing the validation
Expand Down

0 comments on commit 0fb562b

Please sign in to comment.