protected async Task <CodeGeneratorResult> ValidateAttempt(int retry, string attempt)
        {
            var result = new CodeGeneratorResult
            {
                Reason  = FailureReasonType.None,
                Retries = retry,
                Value   = null
            };

            if (await stopWords.IsAllowedAsync(attempt))
            {
                if (await uniqueness.IsUniqueAsync(attempt))
                {
                    result.Value   = attempt;
                    result.Success = true;
                }
                else
                {
                    result.Reason = FailureReasonType.Uniqueness;
                }
            }
            else
            {
                result.Reason = FailureReasonType.Stopped;
            }

            if (transformer != null && result.Success)
            {
                result = await transformer.Transform(result);
            }

            return(result);
        }
Beispiel #2
0
        private static string BuildMessage(CodeGeneratorResult result)
        {
            switch (result.Reason)
            {
            case FailureReasonType.Uniqueness:
                return($"Failed due to uniqueness after {result.Retries} attempts.");

            case FailureReasonType.Stopped:
                return($"Failed due to stop words after {result.Retries} attempts.");

            case FailureReasonType.None:
            default:
                return("Unknown exception during code generation");
            }
        }
        public virtual async ValueTask <CodeGeneratorResult> TryGenerateAsync(int length)
        {
            CodeGeneratorResult result = null;

            for (var retry = 1; retry <= options.RetryLimit; retry++)
            {
                await OnBeforeAttempt(new BeforeAttemptEvent(retry));

                var attempt = await GenerateAttemptAsync(length);

                result = await ValidateAttempt(retry, attempt);

                await OnAfterAttempt(new AfterAttemptEvent(result));

                if (result.Success)
                {
                    return(result);
                }
            }

            return(result ?? new CodeGeneratorResult());
        }
Beispiel #4
0
 public CodeGeneratorException(CodeGeneratorResult result)
     : base(BuildMessage(result))
 {
     Result = result;
 }