using System.Collections.Concurrent;
|
using System.Collections.Generic;
|
using System.Dynamic;
|
using System.IO;
|
using System.Numerics;
|
|
namespace OpenTap.Addin
|
{
|
public class VariableContext : DynamicObject
|
{
|
private static readonly char SPLIT_MARK = '.';
|
|
private readonly ConcurrentDictionary<string, RuntimeVariable> Variables;
|
|
public VariableContext(ConcurrentDictionary<string, RuntimeVariable> variables)
|
{
|
Variables = variables;
|
}
|
|
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
{
|
result = null;
|
string name = binder.Name;
|
if (Variables.TryGetValue(name, out var variable))
|
{
|
result = variable.data;
|
return true;
|
}
|
return false;
|
}
|
|
public override bool TrySetMember(SetMemberBinder binder, object value)
|
{
|
string name = binder.Name;
|
if (Variables.TryGetValue(name, out var variable))
|
{
|
variable.data = value;
|
return true;
|
}
|
else
|
{
|
var v = new RuntimeVariable
|
{
|
Name = name,
|
data = value,
|
Type = value.GetType(),
|
TypeName = value.GetType().Name,
|
};
|
Variables[name] = v;
|
return true;
|
}
|
}
|
|
public void Merge(VariableContext source)
|
{
|
if (source?.Variables != null)
|
{
|
foreach (var key in source.Variables.Keys)
|
{
|
Variables[key] = source.Variables[key];
|
}
|
}
|
}
|
|
public void Merge(ConcurrentDictionary<string, RuntimeVariable> Variables)
|
{
|
if (Variables != null)
|
{
|
foreach (var key in Variables.Keys)
|
{
|
this.Variables[key] = Variables[key];
|
}
|
}
|
}
|
|
public static VariableContext FromTestVariables(IEnumerable<TestVariable> variables)
|
{
|
var runtimeVariablePool = new ConcurrentDictionary<string, RuntimeVariable>();
|
foreach (var data in variables)
|
{
|
RuntimeVariable runTime = data.ToRuntime();
|
if (runTime == null) continue;
|
runtimeVariablePool[runTime.Name] = runTime;
|
}
|
return new VariableContext(runtimeVariablePool);
|
}
|
}
|
}
|