Example #1
0
        public static int InjectProperties <T>(T toObj, IPropertyGetter from, string from_prefix = null)
        {
            // inject all the properties from 'from' into an object toObj that match.
            // NOTE: This does not know how to handle child objects with props. e.g. Dtr[Start]
            // Just skip those that don't match
            // NOTE: We intentionally DON'T use toObj.GetType() here because we want explicit caller control of the type. (could just be a child type)

            if (from == null)
            {
                return(0);
            }
            int  propsCopied = 0;
            Type toType      = typeof(T);

            foreach (PropertyInfo propTo in toType.GetProperties())
            {
                if (!propTo.CanWrite)
                {
                    continue;
                }
                object valFrom = from.GetPropertyValue(string.Concat(from_prefix, propTo.Name));    // has a matching prop?
                if (valFrom == null)
                {
                    continue;
                }

                object valTo = Converter.ChangeType(valFrom, propTo.PropertyType);

                propTo.SetValue(toObj, valTo, null); // ChangeType probably not needed?
                propsCopied++;
            }
            return(propsCopied);
        }
        public static PocoToTableEntityConverter <TInput> Create()
        {
            IPropertyGetter <TInput, string>         partitionKeyGetter = GetGetter <string>("PartitionKey");
            IPropertyGetter <TInput, string>         rowKeyGetter       = GetGetter <string>("RowKey");
            IPropertyGetter <TInput, DateTimeOffset> timestampGetter    = GetGetter <DateTimeOffset>("Timestamp");
            IPropertyGetter <TInput, string>         eTagGetter         = GetGetter <string>("ETag");
            Dictionary <string, IPropertyGetter <TInput, EntityProperty> > otherPropertyGetters =
                new Dictionary <string, IPropertyGetter <TInput, EntityProperty> >();

            PropertyInfo[] properties = typeof(TInput).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            Debug.Assert(properties != null);

            foreach (PropertyInfo property in properties)
            {
                Debug.Assert(property != null);

                if (TableClient.IsSystemProperty(property.Name) || !property.CanRead ||
                    property.GetIndexParameters().Length != 0 || !property.HasPublicGetMethod())
                {
                    continue;
                }

                IPropertyGetter <TInput, EntityProperty> otherPropertyGetter = GetOtherGetter(property);
                otherPropertyGetters.Add(property.Name, otherPropertyGetter);
            }

            return(new PocoToTableEntityConverter <TInput>(partitionKeyGetter, rowKeyGetter, timestampGetter, eTagGetter,
                                                           otherPropertyGetters));
        }
 public JsClr(IJintVisitor visitor)
 {
     this.global         = visitor.Global;
     this.propertyGetter = visitor.PropertyGetter;
     this.methodGetter   = visitor.MethodGetter;
     this.fieldGetter    = visitor.FieldGetter;
     value = null;
 }
        private static IPropertyGetter <TInput, EntityProperty> GetOtherGetterGeneric <TProperty>(PropertyInfo property)
        {
            IPropertyGetter <TInput, TProperty> propertyGetter =
                PropertyAccessorFactory <TInput> .CreateGetter <TProperty>(property);

            IConverter <TProperty, EntityProperty> converter = TToEntityPropertyConverterFactory.Create <TProperty>();

            return(new ConverterPropertyGetter <TInput, TProperty, EntityProperty>(propertyGetter, converter));
        }
Example #5
0
        public JsClr(IJintVisitor visitor)
        {
            _global = visitor.Global;
            _propertyGetter = visitor.PropertyGetter;
            _fieldGetter = visitor.FieldGetter;
            _entityAccessor = visitor.EntityAccessor;

            value = null;
        }
Example #6
0
        public JsClr(IJintVisitor visitor)
        {
            _global         = visitor.Global;
            _propertyGetter = visitor.PropertyGetter;
            _fieldGetter    = visitor.FieldGetter;
            _entityAccessor = visitor.EntityAccessor;

            value = null;
        }
Example #7
0
        public JsClr(IJintVisitor visitor)
        {
            this.global = visitor.Global;
            this.propertyGetter = visitor.PropertyGetter;
            this.methodGetter = visitor.MethodGetter;
            this.fieldGetter = visitor.FieldGetter;
            value = null;

        }
        public void GetValue_IfInstanceIsNull_Throws()
        {
            // Arrange
            IPropertyGetter <Poco, PocoProperty> product = CreateProductUnderTest <Poco, PocoProperty>(
                typeof(Poco).GetProperty("Value"));
            Poco instance = null;

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => product.GetValue(instance), "instance");
        }
Example #9
0
        public void CreateGetter_IfClass_ReturnsInstance()
        {
            // Arrange
            PropertyInfo propertyInfo = typeof(Poco).GetProperty("Value");

            // Act
            IPropertyGetter <Poco, string> manager = PropertyAccessorFactory <Poco> .CreateGetter <string>(propertyInfo);

            // Assert
            Assert.NotNull(manager);
        }
        public void Create_ReturnsInstance()
        {
            // Arrange
            PropertyInfo property = typeof(Poco).GetProperty("Value");

            // Act
            IPropertyGetter <Poco, PocoProperty> getter = ClassPropertyGetter <Poco, PocoProperty> .Create(property);

            // Assert
            Assert.NotNull(getter);
        }
Example #11
0
        public ConfigInfoBase(IPropertyGetter configSource, string connectionStringName = null)
        {
            // Assign my config source. (file?)
            _ConfigSource = configSource;
            EnvironMode   = GetSetting(kAppsEnvironMode);
            ValidState.ThrowIfWhiteSpace(EnvironMode, nameof(EnvironMode));  // MUST have EnvironMode

            if (connectionStringName != null)
            {
                ConnectionStringDef = GetSetting(connectionStringName);
            }
        }
Example #12
0
        public ExecutionVisitor(Options options)
        {
            this.methodInvoker = new CachedMethodInvoker(this);
            this.propertyGetter = new CachedReflectionPropertyGetter(methodInvoker);
            this.constructorInvoker = new CachedConstructorInvoker(methodInvoker);
            this.typeResolver = new CachedTypeResolver();
            this.fieldGetter = new CachedReflectionFieldGetter(methodInvoker);

            GlobalScope = new JsObject();
            Global = new JsGlobal(this, options);
            GlobalScope.Prototype = Global as JsDictionaryObject;
            EnterScope(GlobalScope);
            CallStack = new Stack<string>();
        }
        private PocoToTableEntityConverter(
            IPropertyGetter <TInput, string> partitionKeyGetter,
            IPropertyGetter <TInput, string> rowKeyGetter,
            IPropertyGetter <TInput, DateTimeOffset> timestampGetter,
            IPropertyGetter <TInput, string> eTagKeyGetter,
            IReadOnlyDictionary <string, IPropertyGetter <TInput, EntityProperty> > otherPropertyGetters)
        {
            Debug.Assert(otherPropertyGetters != null);

            _partitionKeyGetter   = partitionKeyGetter;
            _rowKeyGetter         = rowKeyGetter;
            _timestampGetter      = timestampGetter;
            _eTagKeyGetter        = eTagKeyGetter;
            _otherPropertyGetters = otherPropertyGetters;
        }
Example #14
0
        public ConverterPropertyGetter(IPropertyGetter <TReflected, TProperty> propertyGetter,
                                       IConverter <TProperty, TConvertedProperty> converter)
        {
            if (propertyGetter == null)
            {
                throw new ArgumentNullException("propertyGetter");
            }

            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }

            _converter      = converter;
            _propertyGetter = propertyGetter;
        }
Example #15
0
        public ExecutionVisitor(Options options, IScriptEngineContext context)
        {
            this.scriptEngineContext = context;
            this.methodInvoker = context.GetMethodInvoker(this);//new CachedMethodInvoker(this);
            this.propertyGetter = new CachedReflectionPropertyGetter(methodInvoker);
            this.constructorInvoker = new CachedConstructorInvoker(methodInvoker);
            this.typeResolver = context.GetTypeResolver();//new CachedTypeResolver();
            this.fieldGetter = new CachedReflectionFieldGetter(methodInvoker);
            _entitiyAccessor = new EntityAccessor();

            GlobalScope = new JsObject();
            Global = new JsGlobal(this, options);
            GlobalScope.Prototype = Global as JsDictionaryObject;
            EnterScope(GlobalScope);
            CallStack = new Stack<string>();
        }
Example #16
0
        public void GetValue_IfPrivateProperty_ReturnsValue()
        {
            // Arrange
            IPropertyGetter <PocoStruct, PocoProperty> product = CreateProductUnderTest <PocoStruct, PocoProperty>(
                typeof(PocoStruct).GetProperty("PrivateValue", BindingFlags.NonPublic | BindingFlags.Instance));
            PocoProperty expected = new PocoProperty();
            PocoStruct   instance = new PocoStruct
            {
                PrivateValueAsPublic = expected
            };

            // Act
            PocoProperty actual = product.GetValue(instance);

            // Assert
            Assert.Same(expected, actual);
        }
Example #17
0
        public void GetValue_ReturnsValue()
        {
            // Arrange
            IPropertyGetter <PocoStruct, PocoProperty> product = CreateProductUnderTest <PocoStruct, PocoProperty>(
                typeof(PocoStruct).GetProperty("Value"));
            PocoProperty expected = new PocoProperty();
            PocoStruct   instance = new PocoStruct
            {
                Value = expected
            };

            // Act
            PocoProperty actual = product.GetValue(instance);

            // Assert
            Assert.Same(expected, actual);
        }
        public ITableEntity Convert(TInput input)
        {
            if (input == null)
            {
                return(null);
            }

            DynamicTableEntity result = new DynamicTableEntity();

            if (_partitionKeyGetter != null)
            {
                result.PartitionKey = _partitionKeyGetter.GetValue(input);
            }

            if (_rowKeyGetter != null)
            {
                result.RowKey = _rowKeyGetter.GetValue(input);
            }

            if (_timestampGetter != null)
            {
                result.Timestamp = _timestampGetter.GetValue(input);
            }

            IDictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty>();

            foreach (KeyValuePair <string, IPropertyGetter <TInput, EntityProperty> > pair in _otherPropertyGetters)
            {
                string propertyName = pair.Key;
                IPropertyGetter <TInput, EntityProperty> getter = pair.Value;
                Debug.Assert(getter != null);
                EntityProperty propertyValue = getter.GetValue(input);
                properties.Add(propertyName, propertyValue);
            }

            result.ReadEntity(properties, operationContext: null);

            if (_eTagKeyGetter != null)
            {
                result.ETag = _eTagKeyGetter.GetValue(input);
            }

            return(result);
        }
Example #19
0
        public static string ReplaceTokenX(string body, IPropertyGetter props, string errorStr = null)
        {
            // Replace a set of possible {{tokens}} from IPropertyGetter

            var  sb     = new StringBuilder();
            int  i0     = 0;
            int  i      = 0;
            bool ifTrue = true;     // part of some {{if(x)}} block ?

            while (true)
            {
                int b1 = body.IndexOf(kBlockStart, i);
                if (b1 < 0)
                {
                    break;
                }
                i = b1;
                int n1 = b1 + kBlockStart.Length;
                int n2 = body.IndexOf(kBlockEnd, n1);
                if (n2 < 0)
                {
                    continue;
                }
                int b2 = n2 + kBlockEnd.Length;
                i = b2;

                string name = body.Substring(n1, n2 - n1);          // get the token.
                if (name.StartsWith("if(") && name.EndsWith(")"))   // {{if(xxx)}}
                {
                    // special token is used to switch lines.
                    name   = name.Substring(3, name.Length - 4).Trim();
                    ifTrue = Converter.ToBool(props.GetPropertyValue(name));
                }
                else if (name == "else")
                {
                    ifTrue = !ifTrue;
                }
                else if (name == "endif")
                {
                    ifTrue = true;
                }

                if (ifTrue)
                {
                    object val = props.GetPropertyValue(name);
                    if (val == null)
                    {
                        if (errorStr == null) // just leave errors.
                        {
                            continue;
                        }
                        val = errorStr;   // replace error with something.
                    }

                    // replace text
                    sb.Append(body.Substring(i0, b1 - i0));
                    sb.Append(val.ToString());
                }

                i0 = b2;
            }

            if (i0 > 0)
            {
                sb.Append(body.Substring(i0));
                return(sb.ToString());
            }
            return(body);
        }
Example #20
0
        public IValidatorT <string> AllowedFilter = null;    // Filter who we can and cannot send emails to. White list email addresses.

        public void Init(IPropertyGetter config)
        {
            // like config._Configuration.Bind(this);
            PropertyUtil.InjectProperties(this, config, ConfigInfoBase.kSmtp);
        }
Example #21
0
 public ClrPropertyDescriptor(IPropertyGetter getter, IGlobal global, JsDictionaryObject owner, string propertyName)
     : base(owner, propertyName)
 {
     this.global = global;
     this.getter = getter;
 }
 public ClrPropertyDescriptor(IPropertyGetter getter, IGlobal global, JsDictionaryObject owner, string propertyName)
     : base(owner, propertyName)
 {
     this.global = global;
     this.getter = getter;
 }
Example #23
0
 public void OnDeserialization(object sender)
 {
     this.methodInvoker = new CachedMethodInvoker(this);
     this.propertyGetter = new CachedReflectionPropertyGetter(methodInvoker);
     this.constructorInvoker = new CachedConstructorInvoker(methodInvoker);
     this.typeResolver = new CachedTypeResolver();
     this.fieldGetter = new CachedReflectionFieldGetter(methodInvoker);
 }