Beispiel #1
0
        TypeDef BuildStrategySpec(Types types, IEnumerable <FuncSpec> strategyFuncs)
        {
            TypeDef strategy = null;

            Func <Lst <NamedValueToken>, State <StrategyContext, Unit>[]> compose = items => items.Map(x => (State <StrategyContext, Unit>)x.Value.Value).ToArray();


            var oneForOne = FuncSpec.Property("one-for-one", () => strategy, () => strategy, value => Strategy.OneForOne((State <StrategyContext, Unit>)value));
            var allForOne = FuncSpec.Property("all-for-one", () => strategy, () => strategy, value => Strategy.AllForOne((State <StrategyContext, Unit>)value));

            var always   = FuncSpec.Property("always", () => strategy, () => types.Directive, value => Strategy.Always((Directive)value));
            var pause    = FuncSpec.Property("pause", () => strategy, () => types.Time, value => Strategy.Pause((Time)value));
            var retries1 = FuncSpec.Property("retries", () => strategy, () => types.Int, value => Strategy.Retries((int)value));

            var retries2 = FuncSpec.Attrs(
                "retries",
                () => strategy,
                locals => Strategy.Retries((int)locals["count"], (Time)locals["duration"]),
                new FieldSpec("count", () => types.Int),
                new FieldSpec("duration", () => types.Time)
                );

            var backoff1 = FuncSpec.Attrs(
                "backoff",
                () => strategy,
                locals => Strategy.Backoff((Time)locals["min"], (Time)locals["max"], (Time)locals["step"]),
                new FieldSpec("min", () => types.Time),
                new FieldSpec("max", () => types.Time),
                new FieldSpec("step", () => types.Time)
                );

            var backoff2 = FuncSpec.Property("backoff", () => strategy, () => types.Time, value => Strategy.Backoff((Time)value));

            // match
            // | exception -> directive
            // | _         -> directive
            var match = FuncSpec.Special("match", () => strategy);

            // redirect when
            // | directive -> message-directive
            // | _         -> message-directive
            var redirect = FuncSpec.Special("redirect", () => strategy);

            strategy = new TypeDef(
                "strategy",
                typeof(State <StrategyContext, Unit>),
                (_, s) => Strategy.Compose(compose((Lst <NamedValueToken>)s)),
                20,
                new[] { oneForOne, allForOne, always, pause, retries1, retries2, backoff1, backoff2, match, redirect }.Append(strategyFuncs).ToArray()
                );

            return(strategy);
        }
Beispiel #2
0
        /// <summary>
        /// Automatically defined record-type constructor
        /// </summary>
        /// <param name="type"></param>
        /// <param name="ctor"></param>
        /// <param name="assembly"></param>
        /// <param name="name"></param>
        /// <param name="nodeName"></param>
        public TypeDef(
            Type type,
            Func <Lst <NamedValueToken>, object> ctor,
            Types assembly,
            int order,
            string name     = null,
            string nodeName = ""
            )
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var info = type.GetTypeInfo();

            Order               = order;
            Ctor                = ctor ?? (x => x);
            MapsTo              = type;
            Name                = name ?? MakeName(info.Name);
            BinaryOperators     = LanguageExt.Map.empty <string, Func <ValueToken, ValueToken, ValueToken> >();
            PrefixOperators     = LanguageExt.Map.empty <string, Func <ValueToken, ValueToken> >();
            PostfixOperators    = LanguageExt.Map.empty <string, Func <ValueToken, ValueToken> >();
            ConversionOperators = LanguageExt.Map.empty <string, Func <object, object> >();
            ToLstValues         = x => x;
            ToMapValues         = x => x;
            GenericType         = null;
            NodeName            = nodeName;

            var props = from p in type.GetTypeInfo().GetProperties()
                        where assembly.Exists(p.PropertyType)
                        select FuncSpec.Property(MakeName(p.Name), () => assembly.Get(p.PropertyType));

            var fields = from p in type.GetTypeInfo().GetFields()
                         where assembly.Exists(p.FieldType)
                         select FuncSpec.Property(MakeName(p.Name), () => assembly.Get(p.FieldType));

            var methods = from m in type.GetTypeInfo().GetMethods()
                          where m.IsStatic &&
                          assembly.Exists(m.ReturnType) &&
                          m.GetParameters().Map(p => p.ParameterType).ForAll(assembly.Exists)
                          let ps = m.GetParameters().Map(p => new FieldSpec(p.Name, () => assembly.Get(p.ParameterType))).ToArray()
                                   select FuncSpec.Attrs(MakeName(m.Name), () => assembly.Get(m.ReturnType), locals => m.Invoke(null, locals.Values.ToArray()), ps);

            FuncSpecs = List.append(props, fields, methods).ToArray();


            ValueParser = BuildObjectParser(FuncSpecs).Memo();
        }