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;
8using Waher.Things;
9
11{
18 {
29 public GetSetting(ScriptNode Name, ScriptNode DefaultValue, int Start, int Length, Expression Expression)
30 : base(new ScriptNode[] { Name, DefaultValue }, argumentTypes2Scalar, Start, Length, Expression)
31 {
32 }
33
45 public GetSetting(ScriptNode Host, ScriptNode Name, ScriptNode DefaultValue, int Start, int Length, Expression Expression)
46 : base(new ScriptNode[] { Host, Name, DefaultValue }, argumentTypes3Scalar, Start, Length, Expression)
47 {
48 }
49
53 public override string FunctionName => nameof(GetSetting);
54
58 public override string[] DefaultArgumentNames => new string[] { "Name", "DefaultValue" };
59
64 public override bool IsAsynchronous => true;
65
73 {
74 return this.EvaluateAsync(Arguments, Variables).Result;
75 }
76
83 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
84 {
85 int i = 0;
86 int c = Arguments.Length;
87 string Host = c < 3 ? null : GetHost(Arguments[i++].AssociatedObjectValue, this);
88 string Name = ToString(Arguments[i++]);
89 object DefaultValue = Arguments[i].AssociatedObjectValue;
90 object Result;
91
92 if (string.IsNullOrEmpty(Host))
93 Result = await RuntimeSettings.GetAsync(Name, DefaultValue);
94 else
95 Result = await HostSettings.GetAsync(Host, Name, DefaultValue);
96
97 return Expression.Encapsulate(Result);
98 }
99
106 public static string GetHost(object Value, ScriptNode Node)
107 {
108 if (Value is IHostReference Ref)
109 return Ref.Host;
110 else if (Value is string s)
111 return s;
112 else if (Value is null)
113 return null;
114 else
115 throw new ScriptRuntimeException("Expected a host reference, or null.", Node);
116 }
117
124 public static async Task<string> GetUser(object Value, ScriptNode Node)
125 {
126 if (Value is IUser Ref)
127 return Ref.UserName;
128 else if (Value is IRequestOrigin Ref2)
129 {
130 RequestOrigin Origin = await Ref2.GetOrigin();
131 return Origin.From;
132 }
133 else if (Value is string s)
134 return s;
135 else if (Value is null)
136 return null;
137 else
138 throw new ScriptRuntimeException("Expected a user reference, or null.", Node);
139 }
140 }
141}
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:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
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:18
static string GetHost(object Value, ScriptNode Node)
Gets the host name from a value.
Definition: GetSetting.cs:106
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetSetting.cs:83
override string[] DefaultArgumentNames
Default Argument names
Definition: GetSetting.cs:58
override string FunctionName
Name of the function
Definition: GetSetting.cs:53
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetSetting.cs:72
static async Task< string > GetUser(object Value, ScriptNode Node)
Gets the host name from a value.
Definition: GetSetting.cs:124
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:29
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: GetSetting.cs:64
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:45
Collection of variables.
Definition: Variables.cs:25
Tokens available in request.
Definition: RequestOrigin.cs:9
string From
Address of caller.
Task< RequestOrigin > GetOrigin()
Origin of request.
Interface for objects that contain a reference to a host.
Basic interface for all types of elements.
Definition: IElement.cs:21
Basic interface for a user.
Definition: IUser.cs:7
Interface for requestors that can act as an origin for distributed requests.