public BaseRequest GetRequestForVerb(Noun noun)
        {
            using (LogContext.PushProperty("method", nameof(GetRequestForVerb)))
                using (LogContext.PushProperty("args", noun))
                {
                    var verbKeyValuePair = noun.Verbs.Single();
                    var verb             = verbKeyValuePair.Value;

                    var instance = (BaseRequest)Activator.CreateInstance(verb.RequestType);

                    var optionValues     = verb.Options;
                    var optionProperties = GetOptions(verb.RequestType);

                    var updatedOptions = optionValues.Keys.Intersect(optionProperties.Keys);
                    foreach (var updatedOption in updatedOptions)
                    {
                        var optionProperty = optionProperties[updatedOption];
                        var optionValue    = optionValues[updatedOption];

                        _logger.Information("setting {propertyName} which was mapped from {optionKey} to value '{propertyValue}'", optionProperty.Item2.Name, optionValue.GetFullNameFormatted, optionValue.Value);
                        optionProperty.Item2.SetValue(instance, optionValue.Value);
                    }

                    _logger.Information("successfully mapped {@instance}", instance);
                    return(instance);
                }
        }
Example #2
0
        private static Verb ExtractCandidateVerb(string[] candidateArguments, Noun candidateNoun)
        {
            foreach (var candidateArgument in candidateArguments)
            {
                foreach (var candidateVerb in candidateNoun.Verbs.Values)
                {
                    if (candidateVerb.IsNamed(candidateArgument))
                    {
                        return(candidateVerb);
                    }
                }
            }

            return(null);
        }
Example #3
0
        public Noun ParseAndPopulateOperation(IEnumerable <Noun> allNouns, string[] args)
        {
            Noun nounVerbCombination = null;

            using (LogContext.PushProperty("method", nameof(ParseAndPopulateOperation)))
                using (LogContext.PushProperty("args", args))
                {
                    Verb candidateVerb = null;

                    var candidateArguments = args.Take(2).ToArray();

                    _logger.Information("found {@numberOfCandidateArguments}; specifically {@candidateArguments}", candidateArguments.Length, candidateArguments);

                    var candidateNoun = FindCandidateNoun(allNouns, candidateArguments);

                    if (candidateNoun != null)
                    {
                        candidateVerb = ExtractCandidateVerb(candidateArguments, candidateNoun);
                    }

                    if (candidateNoun == null || candidateVerb == null)
                    {
                        var outOfRangeException = new ArgumentOutOfRangeException("nounVerbPair", string.Join(",", candidateArguments), $"Cannot identify either a Noun or a Verb; what we got was '{string.Join(" ", candidateArguments)}'");
                        _logger.Error(outOfRangeException, "Cannot identify either a Noun or a Verb; what we got was '{candidateArguments}'", string.Join(" ", candidateArguments));

                        throw outOfRangeException;
                    }
                    else
                    {
                        _logger.Information("identified {@identifiedNoun}, {@identifiedVerb}", candidateNoun, candidateVerb);

                        candidateNoun.Verbs.Clear();
                        candidateNoun.Verbs.Add(candidateVerb.GetDefaultName, candidateVerb);
                        nounVerbCombination = candidateNoun;
                    }
                }

            return(nounVerbCombination);
        }
Example #4
0
        public Dictionary <string, Noun> GetNounMetadata(Assembly assembly, string named = null)
        {
            var results = new Dictionary <string, Noun>();

            var nounKvps = _attributeReflector.ReflectNouns(assembly);

            foreach (var nounKvp in nounKvps)
            {
                var currentNounAttribute = nounKvp.Value;

                var currentNoun = new Noun()
                {
                    Names = currentNounAttribute.Names
                };

                currentNoun.Verbs = GetVerbMetadata(assembly, currentNounAttribute.GetDefaultName);

                results.Add(currentNoun.GetDefaultName, currentNoun);
            }

            return(results);
        }
Example #5
0
        public void ParseAndPopulateOptions(ref Noun noun, string[] args)
        {
            using (LogContext.PushProperty("method", nameof(ParseAndPopulateOptions)))
                using (LogContext.PushProperty("args", new { noun, args }))
                {
                    var verbKeyValuePair = noun.Verbs.Single();
                    var verb             = verbKeyValuePair.Value;

                    var candidateArguments = args.Skip(2).ToArray();

                    _logger.Information("found {@numberOfCandidateArguments}; specifically {@candidateArguments}", candidateArguments.Length, candidateArguments);

                    var ordinals      = verb.GetOrdinalOptions;
                    var parameterized = verb.GetParametrizedOptions;

                    for (var i = 0; i < candidateArguments.Length; i++)
                    {
                        var argument = candidateArguments.ElementAtOrDefault(i);
                        // todo nullcheck return

                        // check ordinal fields
                        var ordinal = ordinals.ElementAtOrDefault(i);
                        if (ordinal != null)
                        {
                            var ordinalLikelyValue = string.Empty;

                            // has the ordinal been named
                            if (ordinal.IsNamed(argument))
                            {
                                ordinalLikelyValue = candidateArguments.ElementAtOrDefault(i + 1);
                                i++;
                            }
                            else
                            {
                                ordinalLikelyValue = argument;
                            }

                            ordinal.Value = ordinalLikelyValue;

                            // todo log found

                            continue;
                        }

                        // now parametrized
                        var optionNamed       = verb.GetOptionNamed(argument);
                        var optionLikelyValue = candidateArguments.ElementAtOrDefault(i + 1);
                        if (optionNamed != null)
                        {
                            if (optionLikelyValue != null)
                            {
                                optionNamed.Value = optionLikelyValue;
                                i++;
                            }

                            continue;
                        }
                        else
                        {
                            var outOfRangeException = new ArgumentOutOfRangeException(argument, optionLikelyValue, $"'{noun.GetDefaultName} {verb.GetDefaultName}' Doesn't understand an argument of '{argument}'");
                            _logger.Error(outOfRangeException, "'{@noun.GetFullNameFormatted} {@verb.GetFullNameFormatted}' Doesn't understand an argument of '{@argument}'", noun.GetDefaultName, verb.GetDefaultName, verb.GetDefaultName, argument);

                            throw outOfRangeException;
                        }
                    }
                }
        }
Example #6
0
 public Verb(Noun noun)
 {
     _noun = noun;
 }