Skip to content

Commit 912600d

Browse files
add nullable struct types for method arguments
1 parent 8de3883 commit 912600d

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

Code/ArgumentSystem/ProvidedArguments.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public bool GetBool(string argName)
118118
return GetValue<bool, BoolArgument>(argName);
119119
}
120120

121+
public bool? GetNullableBool(string argName)
122+
{
123+
return GetValueNullableStruct<bool, BoolArgument>(argName);
124+
}
125+
121126
public Func<bool> GetBoolFunc(string argName)
122127
{
123128
var evaluator = GetEvaluators<bool, BoolArgument>(argName).First();
@@ -195,20 +200,46 @@ public CollectionVariable GetCollectionVariable(string argName)
195200
/// Retrieves a list of remaining arguments based on the specified argument name.
196201
/// The method resolves provided arguments into a typed list of values.
197202
/// </summary>
198-
public TValue[] GetRemainingArguments<TValue, TArg>(string argName) where TArg : Argument
203+
public TValue[] GetRemainingArguments<TValue, TArg>(string argName)
204+
where TArg : Argument
199205
{
200206
return GetEvaluators<TValue, TArg>(argName).Select(dtg => dtg.Invoke().Value!).ToArray();
201207
}
202208

203-
public TValue GetValue<TValue, TArg>(string argName) where TArg : Argument
209+
public TValue GetValue<TValue, TArg>(string argName)
210+
where TArg : Argument
204211
{
205212
return GetEvaluators<TValue, TArg>(argName).First().Invoke().Value!;
206213
}
214+
215+
public TValue? GetValueNullableStruct<TValue, TArg>(string argName)
216+
where TArg : Argument
217+
where TValue : struct
218+
{
219+
var evaluator = GetValueInternal<TValue?, TArg>(argName).First();
220+
221+
if (evaluator.Result.HasErrored(out var error))
222+
{
223+
throw new CustomScriptRuntimeError(
224+
$"Fetching argument '{argName}' for method '{method.Name}' failed.".AsError()
225+
+ error
226+
);
227+
}
228+
229+
return evaluator switch
230+
{
231+
DynamicTryGet<TValue> strict => strict.Invoke().Value,
232+
DynamicTryGet<TValue?> nullable => nullable.Invoke().Value,
233+
_ => throw new AndrzejFuckedUpException(
234+
$"Argument '{argName}' evaluator type mismatch. " +
235+
$"Got {evaluator.GetType().AccurateName}, expected {typeof(TValue).AccurateName} or {typeof(TValue?).AccurateName}.")
236+
};
237+
}
207238

208239
private List<DynamicTryGet<TValue>> GetEvaluators<TValue, TArg>(string argName)
240+
where TArg : Argument
209241
{
210-
Result mainErr =
211-
$"Fetching argument '{argName}' for method '{method.Name}' failed.";
242+
Result mainErr = $"Fetching argument '{argName}' for method '{method.Name}' failed.";
212243

213244
var evaluators = GetValueInternal<TValue, TArg>(argName);
214245

@@ -221,16 +252,21 @@ private List<DynamicTryGet<TValue>> GetEvaluators<TValue, TArg>(string argName)
221252
}
222253

223254
if (evaluator is not DynamicTryGet<TValue> argEvalRes)
255+
{
224256
throw new AndrzejFuckedUpException(
225-
mainErr + $"Argument value is not of type {typeof(TValue).Name}, evaluator: {evaluator.GetType().AccurateName}.");
257+
mainErr +
258+
$"Argument value is not of type {typeof(TValue).Name}, evaluator: {evaluator.GetType().AccurateName}."
259+
);
260+
}
226261

227262
resultList.Add(argEvalRes);
228263
}
229264

230265
return resultList;
231266
}
232267

233-
private List<DynamicTryGet> GetValueInternal<TValue, TArg>(string argName)
268+
private List<DynamicTryGet> GetValueInternal<TValue, TArg>(string argName)
269+
where TArg : Argument
234270
{
235271
if (ArgumentValues.TryGetValue((argName, typeof(TArg)), out var value))
236272
{

Code/MethodSystem/Methods/DiscordMethods/DiscordEmbedMethod.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Newtonsoft.Json.Linq;
33
using SER.Code.ArgumentSystem.Arguments;
44
using SER.Code.ArgumentSystem.BaseArguments;
5-
using SER.Code.Helpers;
65
using SER.Code.MethodSystem.BaseMethods.Synchronous;
76
using SER.Code.MethodSystem.MethodDescriptors;
87
using UnityEngine;

Code/MethodSystem/Methods/TextMethods/SubtextMethod.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using SER.Code.ArgumentSystem.BaseArguments;
44
using SER.Code.Helpers.Extensions;
55
using SER.Code.MethodSystem.BaseMethods.Synchronous;
6-
using SER.Code.MethodSystem.MethodDescriptors;
76
using SER.Code.ValueSystem;
87

98
namespace SER.Code.MethodSystem.Methods.TextMethods;

Code/MethodSystem/Methods/TimeMethods/FormatDurationMethod.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using JetBrains.Annotations;
22
using SER.Code.ArgumentSystem.Arguments;
33
using SER.Code.ArgumentSystem.BaseArguments;
4-
using SER.Code.Helpers;
54
using SER.Code.MethodSystem.BaseMethods.Synchronous;
65
using SER.Code.MethodSystem.MethodDescriptors;
76
using SER.Code.ValueSystem;

0 commit comments

Comments
 (0)