public RepositoryBase(Interpeter interpeter, ObjectFactory objectFactory)
 {
     SetupDefaultConversions();
     Defaults      = new Dictionary <string, string>();
     Interpeter    = interpeter;
     ObjectFactory = objectFactory;
 }
 public RepositoryBase(Interpeter interpeter, ObjectFactory objectFactory)
 {
     SetupDefaultConversions();
     Defaults               = new Dictionary <string, string>();
     Interpeter             = interpeter;
     ObjectFactory          = objectFactory;
     DefaultInitialized     = false;
     FactoryMethods         = new Dictionary <Characteristics, Func <T, T> >();
     DefaultCharacteristics = Characteristics.None;
 }
Beispiel #3
0
        public static object MapTo(this Dictionary <string, KeyValuePair <string, string> > values, Type desiredType, Interpeter interpeter, ObjectFactory f)
        {
            var members      = desiredType.GetValueMembers();
            var constructors = desiredType.CachedGetConstructors();

            var groups    = values.Keys.Select(x => Split(x).First());
            var unmatched = groups.Except(members.Select(x => x.Name.ToUpper()));
            var errors    = unmatched.Except(constructors.SelectMany(c => c.GetParameters().Select(p => p.Name.ToUpper())));

            if (errors.Any())
            {
                var prefix = values[errors.First()].Key.Substring(0, values[errors.First()].Key.Length - errors.First().Length);
                var unused = members.Select(p => p.Name).Where(p => !groups.Contains(p.ToUpper()));
                throw new GherkinException($"The columns:{errors.LogFormat(x => values[x].Key)} are unmatched maybe it is one of these {unused.LogFormat(x => prefix + x)}");
            }

            object resolve(string name, Type t)
            {
                var    keys = values.Keys.Where(k => k.Split().First() == name);
                object item;

                if (keys.One() && keys.First().Split().One())
                {
                    item = interpeter.Get(t, values[name].Value);
                }
                else if (keys.Any(k => k.Split().One()))
                {
                    throw new GherkinException($"You can't specify the object and set properties on the object, columns {keys.LogFormat(x => values[x].Key)} are in conflict");
                }
                else
                {
                    item = keys.ToDictionary(x => x.Recurse(), x => values[x]).MapTo(t, interpeter, f);
                }

                foreach (var k in keys.ToList())
                {
                    values.Remove(k);
                }

                return(item);
            };

            object ret;

            if (unmatched.Any())
            {
                var possibles = constructors.Where(c => unmatched.Except(c.GetParameters().Select(p => p.Name.ToUpper())).None());
                var valid     = possibles.Where(c => c.GetParameters().Where(p => !p.IsOptional).Select(p => p.Name.ToUpper()).Except(groups).None());
                var ctor      = valid.OrderBy(c => c.GetParameters().Count()).Reverse().FirstOrDefault();

                if (ctor == null)
                {
                    throw new GherkinException($"the following fields '{unmatched.LogFormat()}' did not map to any constructor or property.");
                }

                ret = ctor.Invoke(ctor
                                  .GetParameters()
                                  .Select(p => groups.Contains(p.Name.ToUpper())?resolve(p.Name.ToUpper(), p.ParameterType): p.DefaultValue)
                                  .ToArray());
            }
            else
            {
                ret = f.CreateInstance(desiredType);
            }

            foreach (var key in values.Keys.Select(k => Split(k).First()).ToList())
            {
                var prop = members.First(p => p.Name.ToUpper() == key);
                prop.SetValue(ret, resolve(key, prop.Type));
            }

            return(ret);
        }
Beispiel #4
0
 public static T MapTo <T>(this Dictionary <string, KeyValuePair <string, string> > values, Interpeter interpeter, ObjectFactory f)
 => (T)values.MapTo(typeof(T), interpeter, f);
 public ExistingDataManager(Interpeter interpeter)
 {
     Interpeter = interpeter;
 }