Beispiel #1
0
        private static RuleExpr <T, RuleExprContext <string> > GetValueImpl <T>(string key)
        {
            return(context =>
            {
                var applicant = context.Applicants[context.Selector];
                if (applicant.KeyValueMap.TryGetValue(key, out var value))
                {
                    if (!(value is T))
                    {
                        throw new Exception($"Failed to retrieve value for key {key} for applicant {applicant.Id} due to type mismatch. Got {value.GetType().Name}, expected {typeof(T).Name}");
                    }

                    return (Option <T> .Some((T)value), context);
                }

                if (!applicant.Loaders.Any())
                {
                    throw new Exception($"Failed to load value for key {key} for applicant {applicant.Id}");
                }

                var newContext = context with
                {
                    Applicants = context.Applicants.SetItem(context.Selector, applicant with
                    {
                        Loaders = applicant.Loaders.Skip(1),
                        KeyValueMap = applicant.Loaders.First().Load(applicant.Id, key, applicant.KeyValueMap)
                    })
                };

                return GetValueImpl <T>(key)(newContext);
            });
        }
Beispiel #2
0
 public static RuleExprAst <T, RuleExprContext <string> > GetValue <T>(string key)
 {
     return
         (new RuleExprAst <T, RuleExprContext <string> >
     {
         Expression = context => GetValueImpl <T>(key)(context)
     });
 }
Beispiel #3
0
        /// <summary>
        /// Provides strongly-typed access to the value of the field
        /// identified by ordinal position.
        /// </summary>
        /// <remarks>
        /// If <typeparamref name="T"/> is a reference type and the field
        /// value is <see cref="DBNull.Value"/> then the returned value is
        /// a null reference. If <typeparamref name="T"/> is a value type
        /// and the field value is <see cref="DBNull.Value"/> then
        /// <see cref="InvalidCastException"/> is raised unless
        /// <typeparamref name="T"/> is nullable (<see cref="Nullable{T}"/>).
        /// </remarks>

        public static T GetValue <T>(this IDataRecord record, int i)
        {
            if (record == null)
            {
                throw new ArgumentNullException("record");
            }
            return(GetValueImpl <T> .Impl(record[i]));
        }
Beispiel #4
0
        private static RuleExpr <IEnumerable <T>, RuleExprContext <Unit> > GetValuesImpl <T>(string key)
        {
            return(context =>
            {
                var result = new List <T>();
                var scontext = context.WithSelector <string>(null);
                foreach (var applicantId in context.Applicants.Keys)
                {
                    scontext = scontext.WithSelector(applicantId);
                    var(maybeValue, newSContext) = GetValueImpl <T>(key)(scontext);
                    if (!maybeValue.IsSome(out var value))
                    {
                        throw new Exception("Internal error. GetValue should never return nothing (it should throw instead).");
                    }
                    result.Add(value);
                }

                return (Option <IEnumerable <T> > .Some(result), scontext.WithSelector(Unit.Value));
            });
        }