// Provide a strong typed wrapper.
        public static FuncAsyncConverter <TSource, TDestination> GetConverter <TSource, TDestination, TAttribute>(this IConverterManager converterManager)
            where TAttribute : Attribute
        {
            var func = converterManager.GetConverter <TAttribute>(typeof(TSource), typeof(TDestination));

            return(func.AsTyped <TSource, TDestination>());
        }
        private static Func <TAttribute, IValueProvider> BuildICollectorArgument <TAttribute, TMessage, TMessageSrc>(
            IConverterManager cm,
            Func <TAttribute, IAsyncCollector <TMessage> > buildFromAttribute,
            AttributeCloner <TAttribute> cloner)
            where TAttribute : Attribute
        {
            // Other
            Func <TMessageSrc, TAttribute, TMessage> convert = cm.GetConverter <TMessageSrc, TMessage, TAttribute>();

            if (convert == null)
            {
                ThrowMissingConversionError(typeof(TMessageSrc));
            }
            Func <TAttribute, IValueProvider> argumentBuilder = (attrResolved) =>
            {
                IAsyncCollector <TMessage>    raw = buildFromAttribute(attrResolved);
                IAsyncCollector <TMessageSrc> obj = new TypedAsyncCollectorAdapter <TMessageSrc, TMessage, TAttribute>(
                    raw, convert, attrResolved);
                ICollector <TMessageSrc> obj2 = new SyncAsyncCollectorAdapter <TMessageSrc>(obj);
                string invokeString           = cloner.GetInvokeString(attrResolved);
                return(new AsyncCollectorValueProvider <ICollector <TMessageSrc>, TMessage>(obj2, raw, invokeString));
            };

            return(argumentBuilder);
        }
        private static bool HasConverterWorker <TAttribute, TSrc, TDest>(IConverterManager converterManager)
            where TAttribute : Attribute
        {
            var func = converterManager.GetConverter <TSrc, TDest, TAttribute>();

            return(func != null);
        }
        protected string ConvertToString(TMessage eventData)
        {
            var convert = _converterManager.GetConverter <TMessage, string, Attribute>();
            var result  = convert(eventData, null);

            return(result);
        }
            private static FuncAsyncConverter <DirectInvokeString, TTriggerValue> GetDirectInvoker(IConverterManager cm)
            {
                var direct = cm.GetConverter <DirectInvokeString, TTriggerValue, TAttribute>();

                if (direct != null)
                {
                    return(direct);
                }

                var str = cm.GetConverter <string, TTriggerValue, TAttribute>();

                if (str != null)
                {
                    return((input, attr, ctx) => str(input.Value, attr, ctx));
                }
                return(null);
            }
Exemplo n.º 6
0
 public SimpleTriggerArgumentBinding(ITriggerBindingStrategy <TMessage, TTriggerValue> hooks, IConverterManager converterManager, bool isSingleDispatch = true)
 {
     this._hooks       = hooks;
     this.Contract     = Hooks.GetBindingContract(isSingleDispatch);
     this.ElementType  = typeof(TMessage);
     _converterManager = converterManager;
     _stringConverter  = _converterManager.GetConverter <TMessage, string, Attribute>();
 }
            public BlobCollectionConverter(BlobsExtensionConfigProvider parent)
            {
                IConverterManager cm = parent._converterManager;

                _converter = cm.GetConverter <BlobBaseClient, T, BlobAttribute>();
                if (_converter == null)
                {
                    throw new InvalidOperationException($"Can't convert blob to {typeof(T).FullName}.");
                }
            }
Exemplo n.º 8
0
        GetDirectTriggerBindingWorker <TMessage, TTriggerValue, TUserType>(
            ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
            IConverterManager converterManager)
        {
            var directConvert = converterManager.GetConverter <TMessage, TUserType, Attribute>();

            if (directConvert != null)
            {
                var argumentBinding = new CustomTriggerArgumentBinding <TMessage, TTriggerValue, TUserType>(
                    bindingStrategy, converterManager, directConvert);
                return(argumentBinding);
            }
            return(null);
        }
 // Check if a conversion exists.
 internal static bool HasConverter <TAttribute>(this IConverterManager converterManager, Type typeSource, Type typeDest)
     where TAttribute : Attribute
 {
     try
     {
         var converter = converterManager.GetConverter <TAttribute>(typeSource, typeDest);
         return(converter != null);
     }
     catch (InvalidOperationException)
     {
         // OpenType match could have thrown.
         return(false);
     }
 }
        // Helper to build an argument binder for IAsyncCollector<TMessageSrc>
        private static Func <TContext, ValueBindingContext, IValueProvider> BuildIAsyncCollectorArgument <TContext, TMessageSrc, TMessage>(
            IConverterManager cm,
            Func <TContext, ValueBindingContext, IAsyncCollector <TMessage> > builder,
            string invokeString)
        {
            Func <TMessageSrc, TMessage> convert = cm.GetConverter <TMessageSrc, TMessage>();
            Func <TContext, ValueBindingContext, IValueProvider> argumentBuilder = (context, valueBindingContext) =>
            {
                IAsyncCollector <TMessage>    raw = builder(context, valueBindingContext);
                IAsyncCollector <TMessageSrc> obj = new TypedAsyncCollectorAdapter <TMessageSrc, TMessage>(raw, convert);
                return(new AsyncCollectorValueProvider <IAsyncCollector <TMessageSrc>, TMessage>(obj, raw, invokeString));
            };

            return(argumentBuilder);
        }
Exemplo n.º 11
0
        public static IArgumentBinding <TableEntityContext> TryCreatePocoBinding(ParameterInfo parameter, IConverterManager converterManager)
        {
            if (parameter.ParameterType.IsByRef)
            {
                return(null);
            }

            if (parameter.ParameterType.ContainsGenericParameters)
            {
                return(null);
            }

            var pocoToEntityConverter = converterManager.GetConverter <TableAttribute>(parameter.ParameterType, typeof(TableEntity));
            var entityToPocoConverter = converterManager.GetConverter <TableAttribute>(typeof(TableEntity), parameter.ParameterType);

            if (pocoToEntityConverter == null || entityToPocoConverter == null)
            {
                return(null);
            }

            Type genericType = typeof(PocoEntityArgumentBinding <>).MakeGenericType(parameter.ParameterType);

            return((IArgumentBinding <TableEntityContext>)Activator.CreateInstance(genericType, entityToPocoConverter, pocoToEntityConverter));
        }
            private static FuncAsyncConverter <TTriggerValue, DirectInvokeString> GetDirectInvokeString(IConverterManager cm)
            {
                var converter = cm.GetConverter <TTriggerValue, DirectInvokeString, TAttribute>();

                if (converter != null)
                {
                    return(converter);
                }

                var strConverter = cm.GetConverter <TTriggerValue, string, TAttribute>();

                if (strConverter != null)
                {
                    return(async(input, attr, ctx) =>
                    {
                        var str = await strConverter(input, attr, ctx);

                        return new DirectInvokeString(str);
                    });
                }

                // No converter. Provide something to display in UI
                return((input, attr, ctx) => Task.FromResult(DirectInvokeString.None));
            }
Exemplo n.º 13
0
        // Provide a strong typed wrapper.
        public static FuncAsyncConverter <TSource, TDestination> GetConverter <TSource, TDestination, TAttribute>(this IConverterManager converterManager)
            where TAttribute : Attribute
        {
            var func = converterManager.GetConverter <TAttribute>(typeof(TSource), typeof(TDestination));

            if (func == null)
            {
                return(null);
            }
            return(async(src, attr, ctx) =>
            {
                var result = await func((TSource)src, attr, ctx);

                return (TDestination)result;
            });
        }
Exemplo n.º 14
0
        // Provide a sync wrapper for convenience in calling.
        public static FuncConverter <TSource, TAttribute, TDestination> GetSyncConverter <TSource, TDestination, TAttribute>(
            this IConverterManager converterManager)
            where TAttribute : Attribute
        {
            var func = converterManager.GetConverter <TAttribute>(typeof(TSource), typeof(TDestination));

            if (func == null)
            {
                return(null);
            }
            return((src, attr, ctx) =>
            {
                var result = Task.Run(() => func((TSource)src, attr, ctx)).GetAwaiter().GetResult();
                return (TDestination)result;
            });
        }
Exemplo n.º 15
0
        static Func <TContext, ValueBindingContext, IValueProvider> BuildOutArgument <TContext, TMessageSrc, TMessage>(
            IConverterManager cm,
            Func <TContext, ValueBindingContext, IFlushCollector <TMessage> > builder,
            string invokeString
            )
        {
            // Other
            Func <TMessageSrc, TMessage> convert = cm.GetConverter <TMessageSrc, TMessage>();
            Func <TContext, ValueBindingContext, IValueProvider> argumentBuilder = (context, valueBindingContext) =>
            {
                IFlushCollector <TMessage>    raw = builder(context, valueBindingContext);
                IFlushCollector <TMessageSrc> obj = new TypedAsyncCollectorAdapter <TMessageSrc, TMessage>(raw, convert);
                return(new OutValueProvider <TMessageSrc>(obj, invokeString));
            };

            return(argumentBuilder);
        }
        private static Func <TAttribute, IValueProvider> BuildOutArrayArgument <TAttribute, TMessage, TMessageSrc>(
            IConverterManager cm,
            Func <TAttribute, IAsyncCollector <TMessage> > buildFromAttribute,
            AttributeCloner <TAttribute> cloner)
            where TAttribute : Attribute
        {
            // Other
            Func <TMessageSrc, TAttribute, TMessage> convert         = cm.GetConverter <TMessageSrc, TMessage, TAttribute>();
            Func <TAttribute, IValueProvider>        argumentBuilder = (attrResolved) =>
            {
                IAsyncCollector <TMessage>    raw = buildFromAttribute(attrResolved);
                IAsyncCollector <TMessageSrc> obj = new TypedAsyncCollectorAdapter <TMessageSrc, TMessage, TAttribute>(
                    raw, convert, attrResolved);
                string invokeString = cloner.GetInvokeString(attrResolved);
                return(new OutArrayValueProvider <TMessageSrc>(obj, invokeString));
            };

            return(argumentBuilder);
        }
            public static ExactBinding <TUserType> TryBuild(
                TriggerAdapterBindingProvider <TAttribute, TTriggerValue> parent,
                BindingProviderContext context)
            {
                IConverterManager cm = parent._converterManager;

                var converter = cm.GetConverter <TTriggerValue, TUserType, TAttribute>();

                if (converter == null)
                {
                    return(null);
                }

                var parameter       = context.Parameter;
                var attributeSource = TypeUtility.GetResolvedAttribute <TAttribute>(parameter);

                return(new ExactBinding <TUserType>
                {
                    _converter = converter,
                    _directInvoker = GetDirectInvoker(cm),
                    _getInvokeString = GetDirectInvokeString(cm),
                    _attribute = attributeSource
                });
            }
            public static IBinding TryBuild <TUserType>(
                BindToStreamBindingProvider <TAttribute> parent,
                BindingProviderContext context)
            {
                // Allowed Param types:
                //  Stream
                //  any T with a Stream --> T conversion
                // out T, with a Out<Stream,T> --> void conversion

                var parameter     = context.Parameter;
                var parameterType = parameter.ParameterType;


                var attributeSource = TypeUtility.GetResolvedAttribute <TAttribute>(parameter);

                // Stream is either way; all other types are known.
                FileAccess?declaredAccess = GetFileAccessFromAttribute(attributeSource);

                Type argHelperType;
                bool isRead;

                IConverterManager cm     = parent._converterManager;
                INameResolver     nm     = parent._nameResolver;
                IConfiguration    config = parent._configuration;

                object converterParam = null;

                {
                    if (parameter.IsOut)
                    {
                        var outConverter = cm.GetConverter <ApplyConversion <TUserType, Stream>, object, TAttribute>();
                        if (outConverter != null)
                        {
                            converterParam = outConverter;
                            isRead         = false;
                            argHelperType  = typeof(OutArgBaseValueProvider <>).MakeGenericType(typeof(TAttribute), typeof(TUserType));
                        }
                        else
                        {
                            throw new InvalidOperationException($"No stream converter to handle {typeof(TUserType).FullName}.");
                        }
                    }
                    else
                    {
                        var converter = cm.GetConverter <Stream, TUserType, TAttribute>();
                        if (converter != null)
                        {
                            converterParam = converter;

                            if (parameterType == typeof(Stream))
                            {
                                if (!declaredAccess.HasValue)
                                {
                                    throw new InvalidOperationException("When binding to Stream, the attribute must specify a FileAccess direction.");
                                }
                                switch (declaredAccess.Value)
                                {
                                case FileAccess.Read:
                                    isRead = true;

                                    break;

                                case FileAccess.Write:
                                    isRead = false;
                                    break;

                                default:
                                    throw new NotImplementedException("ReadWrite access is not supported. Pick either Read or Write.");
                                }
                            }
                            else
                            {
                                // For backwards compat, we recognize TextWriter as write;
                                // anything else should explicitly set the FileAccess flag.
                                if (typeof(TextWriter).IsAssignableFrom(typeof(TUserType)))
                                {
                                    isRead = false;
                                }
                                else
                                {
                                    isRead = true;
                                }
                            }


                            argHelperType = typeof(ValueProvider <>).MakeGenericType(typeof(TAttribute), typeof(TUserType));
                        }
                        else
                        {
                            // This rule can't bind.
                            // Let another try.
                            context.BindingErrors.Add(String.Format(Resource.BindingAssemblyConflictMessage, typeof(Stream).AssemblyQualifiedName, typeof(TUserType).AssemblyQualifiedName));
                            return(null);
                        }
                    }
                }

                VerifyAccessOrThrow(declaredAccess, isRead);
                if (!parent.IsSupportedByRule(isRead))
                {
                    return(null);
                }

                var cloner = new AttributeCloner <TAttribute>(attributeSource, context.BindingDataContract, config, nm);

                ParameterDescriptor param;

                if (parent.BuildParameterDescriptor != null)
                {
                    param = parent.BuildParameterDescriptor(attributeSource, parameter, nm);
                }
                else
                {
                    param = new ParameterDescriptor
                    {
                        Name         = parameter.Name,
                        DisplayHints = new ParameterDisplayHints
                        {
                            Description = isRead ? "Read Stream" : "Write Stream"
                        }
                    };
                }

                var      fileAccess = isRead ? FileAccess.Read : FileAccess.Write;
                IBinding binding    = new StreamBinding(cloner, param, parent, argHelperType, parameterType, fileAccess, converterParam);

                return(binding);
            }