private void ParseValues(IErrorCache errorCache)
        {
            if (!SupportedType.HasValue)
            {
                return;
            }

            switch (SupportedType.Value)
            {
            case SupportedTypes.Integer:
                Value   = ParseInteger(Variable.Value, errorCache);
                Default = ParseInteger(Variable.Default, errorCache);
                Max     = ParseInteger(Variable.Max, errorCache);
                Min     = ParseInteger(Variable.Min, errorCache);
                break;

            case SupportedTypes.String:
                Value   = Variable.Value;
                Default = Variable.Default;
                break;

            default:
                errorCache.AddError($"Unable to parse unhandled type: {SupportedType.Value}");
                break;
            }
        }
Exemple #2
0
        public Bootstrap(IRepository repository, IScriptLoader scriptLoader, IErrorCache errorCache)
        {
            Repository   = repository;
            ScriptLoader = scriptLoader;
            ErrorCache   = errorCache;

            globalLoader = new GlobalLoader(errorCache);
            cache        = new PackageCache(errorCache);
        }
        private TEnum?Parse <TEnum>(string value, string fieldName, IErrorCache errorCache)
            where TEnum : struct, IConvertible
        {
            if (!string.IsNullOrWhiteSpace(value) && value.TryParseStringValueToEnum(out TEnum result))
            {
                return(result);
            }

            errorCache.AddError($"Error parsing {fieldName} for variable");
            return(null);
        }
        public static VariableInfo Extract(Variable variable, string packageId, IErrorCache errorCache)
        {
            var result   = new VariableInfo(variable, packageId);
            int preCount = errorCache.Count;

            result.SupportedType = result.Parse <SupportedTypes>(variable.Type, nameof(variable.Type), errorCache);
            result.Units         = result.Parse <Units>(variable.Units, nameof(variable.Units), errorCache);
            result.ParseValues(errorCache);
            result.HasErrors = errorCache.Count != preCount;

            return(result);
        }
        public T GetValueOrDefault <T>(IErrorCache errorCache = null)
        {
            var result = Value ?? Default;

            if (result == null || result.GetType() != typeof(T))
            {
                var message = $"Could not convert '{result}' to {typeof(T).Name} for variable: {Variable.Name}";
                errorCache?.AddError(message);
                throw new InvalidOperationException(message);
            }

            return((T)result);
        }
        private int?ParseInteger(string value, IErrorCache errorCache)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }
            if (int.TryParse(value, out var result))
            {
                return(result);
            }

            errorCache.AddError($"Could not parse '{value}' as an integer");
            return(null);
        }
        public virtual VariableInfo FirstVarOfName(string variableName, IErrorCache errorCache)
        {
            if (byLocalName.TryGetValue(variableName, out var result))
            {
                return(VariableInfo.Extract(result, result.PackageId, errorCache));
            }

            if (Scope?.Parent != null)
            {
                return(Scope.Parent.FirstVarOfName(variableName, errorCache));
            }

            errorCache.AddError($"Could not FirstVarOfName for variable called {variableName}");
            return(null);
        }
        public static IDictionary <string, NestedScope> BuildHeirarchy(IEnumerable <string> packageNames, IErrorCache errorCache, IDictionary <string, NestedScope> existing = null)
        {
            var orderedPairs = packageNames
                               .Distinct(StringComparer.OrdinalIgnoreCase)
                               .Where(name => existing == null || !existing.ContainsKey(name))
                               .Select(Parse)
                               .OrderBy(t => t.Item1.Length);

            var dict = existing ?? new Dictionary <string, NestedScope>(StringComparer.OrdinalIgnoreCase);

            foreach (var pair in orderedPairs)
            {
                NestedScope parent = null;
                if (pair.Item1 != "" && !dict.TryGetValue(pair.Item1, out parent))
                {
                    errorCache.AddError($"Could not locate parent '{pair.Item1}' when building heirarchy.");
                }

                var scope = new NestedScope(parent)
                {
                    Name = pair.Item2
                };
                dict.Add(pair.Item2, scope);
            }

            return(dict);
        }
        private static Tuple <string, object> ProvideSetting <T>(this VariableInfo varInfo, string settingName, IErrorCache errorCache)
        {
            var value = varInfo.GetValueOrDefault <T>(errorCache);

            return(Tuple.Create(settingName, (object)value));
        }
        private static Tuple <string, object> ProvideSetting <T>(this NestedScope scope, string settingName, IErrorCache errorCache)
        {
            var variable = scope.FirstVarOfName(settingName, errorCache);

            return(variable.ProvideSetting <T>(settingName, errorCache));
        }
        public const string Stable = "stable";      // data that isn't changing

        public static IEnumerable <Tuple <string, object> > SettingForLifestage(NestedScope scope, IErrorCache errorCache, bool active)
        {
            yield return(Tuple.Create(
                             $"{Constants.IndexRoutingAllocationInclude}{RoleTag}",
                             active ? (object)Active : Stable));

            if (active)
            {
                yield return(scope.ProvideSetting <int>(Constants.GlobalSettings.NoOfShards, errorCache));
            }

            // MAYBE want to be able to support different values for active and stable ???
            yield return(scope.ProvideSetting <int>(Constants.GlobalSettings.NoOfReplicas, errorCache));

            yield return(scope.ProvideSetting <string>(Constants.GlobalSettings.RefreshInterval, errorCache));
        }
 public PackageCache(IErrorCache errorCache)
 {
     ErrorCache = errorCache;
 }
 public static VariableInfo FirstVarOfName(this NestedScope scope, string variableName, IErrorCache errorCache)
 {
     return(scope.GetAspect <VariableSet>().FirstVarOfName(variableName, errorCache));
 }
 public GlobalLoader(IErrorCache errorCache)
 {
     ErrorCache = errorCache;
 }