Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetSetting.cs
1using System.Threading.Tasks;
2using Waher.Content;
7
9{
16 {
27 public GetSetting(ScriptNode Name, ScriptNode DefaultValue, int Start, int Length, Expression Expression)
28 : base(new ScriptNode[] { Name, DefaultValue }, argumentTypes2Scalar, Start, Length, Expression)
29 {
30 }
31
43 public GetSetting(ScriptNode Host, ScriptNode Name, ScriptNode DefaultValue, int Start, int Length, Expression Expression)
44 : base(new ScriptNode[] { Host, Name, DefaultValue }, argumentTypes3Scalar, Start, Length, Expression)
45 {
46 }
47
51 public override string FunctionName => nameof(GetSetting);
52
56 public override string[] DefaultArgumentNames => new string[] { "Name", "DefaultValue" };
57
62 public override bool IsAsynchronous => true;
63
71 {
72 return this.EvaluateAsync(Arguments, Variables).Result;
73 }
74
81 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
82 {
83 int i = 0;
84 int c = Arguments.Length;
85 string Host = c < 3 ? null : GetHost(Arguments[i++].AssociatedObjectValue, this);
86 string Name = Arguments[i++].AssociatedObjectValue?.ToString();
87 object DefaultValue = Arguments[i].AssociatedObjectValue;
88 object Result;
89
90 if (string.IsNullOrEmpty(Host))
91 Result = await RuntimeSettings.GetAsync(Name, DefaultValue);
92 else
93 Result = await HostSettings.GetAsync(Host, Name, DefaultValue);
94
95 return Expression.Encapsulate(Result);
96 }
97
104 public static string GetHost(object Value, ScriptNode Node)
105 {
106 if (Value is IHostReference Ref)
107 return Ref.Host;
108 else if (Value is string s)
109 return s;
110 else if (Value is null)
111 return null;
112 else
113 throw new ScriptRuntimeException("Expected a host reference, or null.", Node);
114 }
115 }
116}
Static class managing persistent host settings. Host settings default to runtime settings if host-spe...
Definition: HostSettings.cs:18
static async Task< string > GetAsync(string Host, string Key, string DefaultValue)
Gets a string-valued setting.
Definition: HostSettings.cs:40
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Gets a runtime setting with name Name. If Host is provided, the function first tries to get the corre...
Definition: GetSetting.cs:16
static string GetHost(object Value, ScriptNode Node)
Gets the host name from a value.
Definition: GetSetting.cs:104
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetSetting.cs:81
override string[] DefaultArgumentNames
Default Argument names
Definition: GetSetting.cs:56
override string FunctionName
Name of the function
Definition: GetSetting.cs:51
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetSetting.cs:70
GetSetting(ScriptNode Name, ScriptNode DefaultValue, int Start, int Length, Expression Expression)
Gets a runtime setting with name Name. If Host is provided, the function first tries to get the corre...
Definition: GetSetting.cs:27
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: GetSetting.cs:62
GetSetting(ScriptNode Host, ScriptNode Name, ScriptNode DefaultValue, int Start, int Length, Expression Expression)
Gets a runtime setting with name Name. If Host is provided, the function first tries to get the corre...
Definition: GetSetting.cs:43
Collection of variables.
Definition: Variables.cs:25
Interface for objects that contain a reference to a host.
Basic interface for all types of elements.
Definition: IElement.cs:20