예제 #1
0
 internal string AsString(Log log, ITikReSentence tikSentence, Dictionary <string, string> variables)
 {
     if (ParamType == ParamType.String)
     {
         if (PreprocessValue(log, tikSentence, variables, out var word))
         {
             return(word);
         }
         else
         {
             return(Substitute(Default, variables) ?? "");
         }
     }
     else
     {
         // TryGetValue also handles default values for non-string parameters
         if (TryGetValue(log, tikSentence, variables, out var value))
         {
             return(value.ToString(CultureInfo.InvariantCulture));
         }
         else
         {
             return("");
         }
     }
 }
예제 #2
0
 private static string GetValueFromSentence(ITikReSentence sentence, TikEntityPropertyAccessor property)
 {
     //Read field value (or get default value)
     if (property.IsMandatory)
     {
         return(sentence.GetResponseField(property.FieldName));
     }
     else
     {
         return(sentence.GetResponseFieldOrDefault(property.FieldName, property.DefaultValue));
     }
 }
예제 #3
0
        private static TEntity CreateObject <TEntity>(ITikReSentence sentence)
            where TEntity : new()
        {
            var metadata = TikEntityMetadataCache.GetMetadata <TEntity>();

            TEntity result = new TEntity();

            foreach (var property in metadata.Properties)
            {
                property.SetEntityValue(result, GetValueFromSentence(sentence, property));
            }

            return(result);
        }
예제 #4
0
        internal bool PreprocessValue(Log log, ITikReSentence tikSentence, Dictionary <string, string> variables, out string word)
        {
            log.Debug2("try to get value");

            if (Value != null)
            {
                log.Debug2("static parameter");
                word = Substitute(Value, variables);
            }
            else if (!tikSentence.TryGetResponseField(Name, out word))
            {
                log.Debug1($"field not found in API response");
                return(false);
            }

            string remappedWord = null;

            if (RemapValues != null && RemapValues.TryGetValue(word, out remappedWord))
            {
                word = remappedWord;

                if (word == null)
                {
                    log.Debug2("remapped to null");
                    return(false);
                }
                log.Debug2($"remapped to '{word}'");
            }
            else
            {
                var tmpWord    = word;
                var firstMatch = RemapValuesRegex?.Where(kvp => kvp.Item1.IsMatch(tmpWord))?.FirstOrDefault();
                if (firstMatch != null)
                {
                    if (firstMatch.Item2 == null)
                    {
                        log.Debug2("regex remapped to null");
                        word = null;
                        return(false);
                    }
                    word = firstMatch.Item1.Replace(word, firstMatch.Item2);
                    log.Debug2($"regex remapped to '{word}'");
                }
            }

            return(true);
        }
예제 #5
0
        internal bool TryGetValue(Log log, ITikReSentence tikSentence, Dictionary <string, string> variables, out double value)
        {
            if (PreprocessValue(log, tikSentence, variables, out var word))
            {
                log.Debug2($"parse as {ParamType}");

                switch (ParamType)
                {
                case ParamType.Int:
                    if (!TryParseDouble(word, out value))
                    {
                        log.Error($"failed to parse value '{word}' to double");
                        return(false);
                    }
                    return(true);

                case ParamType.Bool:
                    var @bool = string.Equals(word, "true", StringComparison.OrdinalIgnoreCase) || string.Equals(word, "yes", StringComparison.OrdinalIgnoreCase);
                    value = (@bool ^ Negate) ? 1 : 0;
                    return(true);

                case ParamType.Timespan:
                    Match match = regexTimepan.Match(word);
                    if (match.Success)
                    {
                        value = 0;
                        var enumerator = match.Groups.Values.Where(group => group.Length > 0).GetEnumerator();

                        // skip first group which contains entire expression
                        enumerator.MoveNext();

                        while (enumerator.MoveNext())
                        {
                            double groupValue = double.Parse(enumerator.Current.Value, CultureInfo.InvariantCulture);
                            enumerator.MoveNext();
                            string groupSuffix = enumerator.Current.Value;

                            value += groupSuffix switch
                            {
                                "w" => groupValue * 604800,
                                "d" => groupValue * 86400,
                                "h" => groupValue * 3600,
                                "m" => groupValue * 60,
                                "s" => groupValue,
                                _ => throw new NotImplementedException()
                            };
                        }
                        return(true);
                    }
                    else
                    {
                        log.Error($"failed to parse Timespan '{word}'");
                        value = 0;
                        return(false);
                    }

                case ParamType.DateTime:
                    if (DateTime.TryParseExact(word, "MMM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out var dateTime))
                    {
                        var now      = DateTime.Now;
                        var timeSpan = DateTimeType.Value switch
                        {
                            Configuration.DateTimeType.FromNow => now - dateTime,
                            Configuration.DateTimeType.ToNow => dateTime - now,
                            _ => throw new NotImplementedException($"unknown DateTimeType: {DateTimeType}"),
                        };
                        value = timeSpan.TotalSeconds;
                        return(true);
                    }
                    else
                    {
                        log.Error($"failed to parse DateTime '{word}'");
                        value = 0;
                        return(false);
                    }

                default:
                    throw new NotImplementedException();
                }
            }
            else
            {
                if (Default == null)
                {
                    log.Debug2($"no default value set");
                    value = 0;
                    return(false);
                }

                word = Substitute(Default, variables);
                if (!TryParseDouble(word, out value))
                {
                    log.Error($"failed to parse default '{word}' to double");
                    return(false);
                }

                return(true);
            }
        }