Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
vdaron committed Sep 12, 2023
2 parents 7e5fb7d + 1467598 commit a4d4889
Show file tree
Hide file tree
Showing 19 changed files with 712 additions and 54 deletions.
1 change: 1 addition & 0 deletions StatesLanguage.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Batcher/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Retrier/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Retriers/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
28 changes: 26 additions & 2 deletions src/Internal/PropertyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* permissions and limitations under the License.
*/

using System;

namespace StatesLanguage.Internal
{
public static class PropertyNames
Expand All @@ -40,14 +42,16 @@ public static class PropertyNames

public const string HEARTBEAT_SECONDS = "HeartbeatSeconds";
public const string HEARTBEAT_SECONDS_PATH = "HeartbeatSecondsPath";
public const string CREDENTIALS = "Credentials";

// ParallelState property names
public const string BRANCHES = "Branches";

// FailState property names
public const string ERROR = "Error";

public const string CAUSE = "Cause";
public const string ERROR_PATH = "ErrorPath";
public const string CAUSE_PATH = "CausePath";

// ChoiceState property names
public const string DEFAULT_STATE = "Default";
Expand All @@ -60,6 +64,8 @@ public static class PropertyNames
public const string INTERVAL_SECONDS = "IntervalSeconds";
public const string MAX_ATTEMPTS = "MaxAttempts";
public const string BACKOFF_RATE = "BackoffRate";
public const string MAX_DELAY_SECONDS = "MaxDelaySeconds";
public const string JITTER_STRATEGY = "JitterStrategy";

// WaitState property names
public const string SECONDS = "Seconds";
Expand All @@ -69,9 +75,27 @@ public static class PropertyNames

// MapState property names
public const string MAX_CONCURENCY = "MaxConcurrency";
public const string MAX_CONCURENCY_PATH = "MaxConcurrencyPath";
[Obsolete("Replaced by ITEM PROCESSOR")]
public const string ITERATOR = "Iterator";
public const string ITEMS_PATH = "ItemsPath";

public const string TOLERATED_FAILURE_PERCENTAGE = "ToleratedFailurePercentage";
public const string TOLERATED_FAILURE_COUNT = "ToleratedFailureCount";
public const string TOLERATED_FAILURE_PERCENTAGE_PATH = "ToleratedFailurePercentagePath";
public const string TOLERATED_FAILURE_COUNT_PATH = "ToleratedFailureCountPath";
public const string ITEM_PROCESSOR = "ItemProcessor";
public const string ITEM_SELECTOR = "ItemSelector";
public const string ITEM_READER = "ItemReader";
public const string RESULT_WRITER = "ResultWriter";
public const string ITEM_BATCHER = "ItemBatcher";

// Batcher property names
public const string BATCH_INPUT = "BatchInput";
public const string MAX_ITEMS_PER_BATCH = "MaxItemsPerBatch";
public const string MAX_INPUT_BYTES_PER_BATCH = "MaxInputBytesPerBatch";
public const string MAX_ITEMS_PER_BATCH_PATH = "MaxItemsPerBatchPath";
public const string MAX_INPUT_BYTES_PER_BATCH_PATH = "MaxInputBytesPerBatchPath";

// Binary condition property names
public const string VARIABLE = "Variable";

Expand Down
50 changes: 29 additions & 21 deletions src/Internal/Validation/StateMachineValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ public StateMachine Validate()

ValidateStates(context, _stateMachine.States);

if (!string.IsNullOrEmpty(_stateMachine.StartAt))
if (!string.IsNullOrEmpty(_stateMachine.StartAt) && !_stateMachine.States.ContainsKey(_stateMachine.StartAt))
{
if (!_stateMachine.States.ContainsKey(_stateMachine.StartAt))
{
_problemReporter.Report(new Problem(context, $"{PropertyNames.START_AT} state does not exist."));
}
_problemReporter.Report(new Problem(context, $"{PropertyNames.START_AT} state does not exist."));
}

// If basic validation failed then the graph may not be in a good state to be able to Validate
Expand Down Expand Up @@ -253,7 +250,24 @@ public override int Visit(ChoiceState choiceState)

public override int Visit(FailState failState)
{
_currentContext.AssertStringNotEmpty(failState.Cause, PropertyNames.CAUSE);
if (string.IsNullOrWhiteSpace(failState.CausePath))
{
_currentContext.AssertStringNotEmpty(failState.Cause, PropertyNames.CAUSE);
}
else
{
_currentContext.AssertIsValidReferencePath(failState.CausePath, PropertyNames.CAUSE_PATH);
}

if (string.IsNullOrWhiteSpace(failState.ErrorPath))
{
_currentContext.AssertStringNotEmpty(failState.Error, PropertyNames.ERROR);
}
else
{
_currentContext.AssertIsValidReferencePath(failState.ErrorPath, PropertyNames.ERROR_PATH);
}

return 0;
}

Expand Down Expand Up @@ -307,13 +321,10 @@ public override int Visit(TaskState taskState)
_currentContext.AssertIsPositiveIfPresent(taskState.TimeoutSeconds, PropertyNames.TIMEOUT_SECONDS);
_currentContext.AssertIsPositiveIfPresent(taskState.HeartbeatSeconds, PropertyNames.HEARTBEAT_SECONDS);

if (taskState.HeartbeatSeconds != null)
if (taskState.HeartbeatSeconds != null && taskState.HeartbeatSeconds >= taskState.TimeoutSeconds)
{
if (taskState.HeartbeatSeconds >= taskState.TimeoutSeconds)
{
_problemReporter.Report(new Problem(_currentContext,
$"{PropertyNames.HEARTBEAT_SECONDS} must be smaller than {PropertyNames.TIMEOUT_SECONDS}"));
}
_problemReporter.Report(new Problem(_currentContext,
$"{PropertyNames.HEARTBEAT_SECONDS} must be smaller than {PropertyNames.TIMEOUT_SECONDS}"));
}

if (taskState.HeartbeatSeconds.HasValue && !string.IsNullOrEmpty(taskState.HeartbeatSecondsPath))
Expand Down Expand Up @@ -403,18 +414,15 @@ private void ValidateBinaryCondition<T>(ValidationContext context, BinaryConditi

private void ValidateIterator(MapState mapState)
{
_currentContext.AssertStringNotEmpty(mapState.Iterator.StartAt, PropertyNames.START_AT);
_currentContext.AssertNotEmpty(mapState.Iterator.States, PropertyNames.STATES);
_currentContext.AssertStringNotEmpty(mapState.ItemProcessor.StartAt, PropertyNames.START_AT);
_currentContext.AssertNotEmpty(mapState.ItemProcessor.States, PropertyNames.STATES);
var iteratorContext = _currentContext.Iterator();
ValidateStates(iteratorContext, mapState.Iterator.States);
ValidateStates(iteratorContext, mapState.ItemProcessor.States);

if (!string.IsNullOrEmpty(mapState.Iterator.StartAt))
if (!string.IsNullOrEmpty(mapState.ItemProcessor.StartAt) && !mapState.ItemProcessor.States.ContainsKey(mapState.ItemProcessor.StartAt))
{
if (!mapState.Iterator.States.ContainsKey(mapState.Iterator.StartAt))
{
_problemReporter.Report(new Problem(iteratorContext,
$"{PropertyNames.START_AT} references a non existent state."));
}
_problemReporter.Report(new Problem(iteratorContext,
$"{PropertyNames.START_AT} references a non existent state."));
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Serialization/MapStateSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
Converters = {new CatcherDeserializer()}
});

state.Add(PropertyNames.ITERATOR, JToken.FromObject(((MapState) value).Iterator, serializer));
var mapState = (MapState) value;

state.Add(PropertyNames.ITEM_PROCESSOR, JToken.FromObject(mapState.ItemProcessor, serializer));
state.Add(PropertyNames.ITEM_BATCHER, JToken.FromObject(mapState.ItemBatcher, serializer));

var transition = ((TransitionState) value).Transition;

Expand Down
63 changes: 60 additions & 3 deletions src/States/FailState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ private FailState()
/// </summary>
[JsonProperty(PropertyNames.CAUSE)]
public string Cause { get; protected set; }

/// <summary>
/// Json Path to the <see cref="Error"/>
/// </summary>
[JsonProperty(PropertyNames.ERROR_PATH)]
public string ErrorPath { get; protected set; }

/// <summary>
/// json path to the <see cref="Cause"/>
/// </summary>
[JsonProperty(PropertyNames.CAUSE_PATH)]
public string CausePath { get; protected set; }

public override StateType Type => StateType.Fail;
public override bool IsTerminalState => true;
Expand All @@ -59,21 +71,43 @@ public static Builder GetBuilder()
*/
public sealed class Builder : StateBuilder<FailState, Builder>
{
[JsonProperty(PropertyNames.CAUSE)] private string _cause;
[JsonProperty(PropertyNames.CAUSE)]
private string _cause;

[JsonProperty(PropertyNames.ERROR)] private string _error;
[JsonProperty(PropertyNames.ERROR)]
private string _error;

[JsonProperty(PropertyNames.CAUSE_PATH)]
private string _causePath;

[JsonProperty(PropertyNames.ERROR_PATH)]
private string _errorPath;

internal Builder()
{
}

public override FailState Build()
{
if (!string.IsNullOrWhiteSpace(_error) && !string.IsNullOrWhiteSpace(_errorPath))
throw new StatesLanguageException("You cannot specify Error and ErrorPath at the same time");

if (!string.IsNullOrWhiteSpace(_cause) && !string.IsNullOrWhiteSpace(_causePath))
throw new StatesLanguageException("You cannot specify Cause and CausePath at the same time");

if (string.IsNullOrWhiteSpace(_error) && string.IsNullOrWhiteSpace(_errorPath))
throw new StatesLanguageException("You have to specify at least Error or ErrorPath");

if (string.IsNullOrWhiteSpace(_cause) && string.IsNullOrWhiteSpace(_causePath))
throw new StatesLanguageException("You have to specify at least Cause and CausePath");

return new FailState
{
Comment = _comment,
Error = _error,
Cause = _cause
ErrorPath = _errorPath,
Cause = _cause,
CausePath = _causePath
};
}

Expand All @@ -100,6 +134,29 @@ public Builder Cause(string cause)
_cause = cause;
return this;
}


/// <summary>
/// Json Path to the <see cref="Error"/>
/// </summary>
/// <param name="errorPath">Error code value</param>
/// <returns>This object for method chaining.</returns>
public Builder ErrorPath(string errorPath)
{
_errorPath = errorPath;
return this;
}

/// <summary>
/// Json Path to the <see cref="Cause"/>
/// </summary>
/// <param name="causePath">Cause description.</param>
/// <returns>This object for method chaining.</returns>
public Builder CausePath(string causePath)
{
_causePath = causePath;
return this;
}
}
}
}
Loading

0 comments on commit a4d4889

Please sign in to comment.