Пример #1
0
 /// <summary>
 /// Adds the xml file to the pipe
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="xPath">the xpath to retrieve</param>
 /// <param name="file">the optional file to use</param>
 /// <returns></returns>
 public static IEnvironmentBuilder WithXml(this IEnvironmentBuilder builder, string xPath, string file = null)
 {
     return(builder.WithSource(cfg =>
     {
         if (!cfg.HasValue(typeof(XmlFileParser).FullName))
         {
             return null;//no xml file parser was registered
         }
         var reqType = cfg.GetBuildType();
         var parser = cfg.GetValue <XmlFileParser>(typeof(XmlFileParser).FullName);
         return parser.Value(xPath, reqType, file);
     }, cfg => cfg.WithTrace(xPath, "xml")));
 }
Пример #2
0
        /// <summary>
        /// Adds the json file to the pipeline
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="jPath">the json path to extract</param>
        /// <returns></returns>
        public static IEnvironmentBuilder WithJson(this IEnvironmentBuilder builder, string jPath)
        {
            //next line wil trigger an exception on malformed syntax
            var expr = JSegment.Parse(jPath).ToList();

            return(builder.WithSource(cfg =>
            {
                if (!cfg.HasValue(typeof(JsonFileParser).FullName))
                {
                    return null;//no json file parser was registered
                }
                var reqType = cfg.GetBuildType();
                var parser = cfg.GetValue <JsonFileParser>(typeof(JsonFileParser).FullName);
                return parser.Value(expr, reqType);
            }, cfg => cfg.WithTrace(jPath, "json")));
        }
        // this method will be used to get the next randomly incremented number
        public static IEnvironmentBuilder Random(this IEnvironmentBuilder builder)
        {
            if (!builder.Configuration.HasValue(SaverKey))
            {
                builder.WithConfiguration(cfg => cfg.SetValue(SaverKey, new NumberSaver()));
            }

            return(builder.WithSource(config =>
            {
                //just a simple logic to increment and save values
                var nextValue = config.GetFactoryValue <int>(RngKey);
                var saver = config.GetValue <NumberSaver>(SaverKey);
                var oldKey = saver.Prev;
                saver.Prev += nextValue;
                return oldKey;
            }, config => config.WithTrace("my random value", "my random source")));
        }
        /// <summary>
        /// Adds the environment variable source to te pipe
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="name">the name of the variable</param>
        /// <param name="configuration">the scoped configuration to add</param>
        /// <returns></returns>
        public static IEnvironmentBuilder WithEnvironmentVariable(this IEnvironmentBuilder builder, string name, Action <IEnvironmentConfiguration> configuration = null)
        {
            if (!builder.Configuration.HasValue(typeof(EnvironmentVariableParser).FullName))
            {
                builder.WithConfiguration(cfg => cfg.SetValue(typeof(EnvironmentVariableParser).FullName,
                                                              new EnvironmentVariableParser()));
            }

            return(builder.WithSource(cfg =>
            {
                var parser = cfg.GetValue <EnvironmentVariableParser>(typeof(EnvironmentVariableParser).FullName);
                var requiredType = cfg.GetBuildType();
                var target = cfg.GetEnvironmentTarget();
                var prefix = cfg.GetEnvironmentVariablePrefix();
                return parser.Value(name, requiredType, target, prefix);
            }, cfg =>
            {
                configuration?.Invoke(cfg);
                cfg.WithTrace(name, "environment");
            }));
        }
 /// <summary>
 /// Adds the default value source to the pipe
 /// </summary>
 /// <typeparam name="T">the type of value</typeparam>
 /// <param name="builder"></param>
 /// <param name="value">the value to add</param>
 /// <returns></returns>
 public static IEnvironmentBuilder WithDefaultValue <T>(this IEnvironmentBuilder builder, T value)
 {
     return(builder.WithSource(_ => value, cfg => cfg.WithTrace(value?.ToString(), "default")));
 }
Пример #6
0
 public IEnvironmentBuilder WithSource <T>(Func <IReadonlyEnvironmentConfiguration, T> source)
 {
     _builder.WithSource(source);
     return(this);
 }