예제 #1
0
        private static void TestInitialization <T>(
            T value,
            Func <T, JsonNumber> ctor,
            Action <JsonNumber, T> setter,
            Func <JsonNumber, T> getter,
            TryGetValue <T> tryGetter,
            Func <T, JsonNode> implicitCaster)
        {
            // Default constructor:
            JsonNumber number = new JsonNumber();

            setter(number, value);
            AssertValue(value, number, getter, tryGetter);

            // Numeric type constructor:
            number = ctor(value);
            AssertValue(value, number, getter, tryGetter);

            // String constructor:
            number = value switch
            {
                // Adding CultureInfo.InvariantCulture to support tests on platforms where `,` is a  default decimal separator instead of `.`
                double doubleValue => new JsonNumber(doubleValue.ToString(CultureInfo.InvariantCulture)),
                float floatValue => new JsonNumber(floatValue.ToString(CultureInfo.InvariantCulture)),
                decimal decimalValue => new JsonNumber(decimalValue.ToString(CultureInfo.InvariantCulture)),
                _ => new JsonNumber(value.ToString()),
            };
            AssertValue(value, number, getter, tryGetter);

            // Implicit cast:
            number = (JsonNumber)implicitCaster(value);
            AssertValue(value, number, getter, tryGetter);
        }
        // <summary>
        // Produces a list of all GroupAggregateVarInfos, each of which represents a single group aggregate
        // and it candidate function aggregates. It also produces a delegate that given a child node returns the parent node
        // </summary>
        internal static IEnumerable<GroupAggregateVarInfo> Process(Command itree, out TryGetValue tryGetParent)
        {
            var groupRefComputingVisitor = new GroupAggregateRefComputingVisitor(itree);
            groupRefComputingVisitor.VisitNode(itree.Root);
            tryGetParent = groupRefComputingVisitor._childToParent.TryGetValue;

            return groupRefComputingVisitor._groupAggregateVarInfoManager.GroupAggregateVarInfos;
        }
예제 #3
0
 private Lazy <string> GetValueOrNullLazy(TryGetValue tryGet)
 {
     return(ExceptionWrappedLazy(
                () =>
     {
         string value;
         return tryGet(handle, out value) ? value : null;
     }));
 }
예제 #4
0
        // <summary>
        // Produces a list of all GroupAggregateVarInfos, each of which represents a single group aggregate
        // and it candidate function aggregates. It also produces a delegate that given a child node returns the parent node
        // </summary>
        internal static IEnumerable <GroupAggregateVarInfo> Process(Command itree, out TryGetValue tryGetParent)
        {
            var groupRefComputingVisitor = new GroupAggregateRefComputingVisitor(itree);

            groupRefComputingVisitor.VisitNode(itree.Root);
            tryGetParent = groupRefComputingVisitor._childToParent.TryGetValue;

            return(groupRefComputingVisitor._groupAggregateVarInfoManager.GroupAggregateVarInfos);
        }
예제 #5
0
 private static void AssertValue <T>(
     T value,
     JsonNumber number,
     Func <JsonNumber, T> getter,
     TryGetValue <T> tryGetter)
 {
     Assert.Equal(value, getter(number));
     Assert.True(tryGetter(number, out T result));
     Assert.Equal(value, result);
 }
    public static T DefaultIfNull <T>(this string value, TryGetValue <T> evaluator, T defaultValue) where T : struct
    {
        T result;

        if (evaluator(value, out result))
        {
            return(result);
        }

        return(defaultValue);
    }
예제 #7
0
        public static TOutput GetTryValue <TInput, TOutput>(this TInput input, TryGetValue <TInput, TOutput> tryGetValue)
        {
            TOutput value;

            if (!tryGetValue(input, out value))
            {
                value = default(TOutput);
            }

            return(value);
        }
 /// <summary>
 /// make a fast serializer handler
 /// </summary>
 /// <param name="_EncodingGetBytes"></param>
 /// <param name="_RemoveLastCommaCharacter"></param>
 /// <param name="_Append"></param>
 /// <param name="_AppendByte"></param>
 /// <param name="_Serializer"></param>
 /// <param name="_AddSerializedObjects"></param>
 /// <param name="_TryGetValueOfSerializedObjects"></param>
 public JsonBinarySerializeHandler(RefFuncChar _EncodingGetBytes, Action _RemoveLastCommaCharacter, RefActionByte _Append, RefOneByte _AppendByte
                                   , Serializer _Serializer, Action <object, int> _AddSerializedObjects, TryGetValue <object> _TryGetValueOfSerializedObjects) : this()
 {
     EncodingGetBytes = _EncodingGetBytes;
     //RemoveLastCommaCharacter = _RemoveLastCommaCharacter;
     Append                         = _Append;
     AppendByte                     = _AppendByte;
     Serializer                     = _Serializer;
     AddSerializedObjects           = _AddSerializedObjects;
     TryGetValueOfSerializedObjects = _TryGetValueOfSerializedObjects;
 }
예제 #9
0
        static string?ReadOperation(TryGetValue tryGetValue)
        {
            if (!tryGetValue("operation", out var operationValues))
            {
                return(null);
            }

            if (operationValues.Count == 1)
            {
                return(operationValues.ToString());
            }

            throw new("Expected 'operation' to have a single value.");
        }
예제 #10
0
        static (string query, Inputs inputs, string?operation) ReadParams(TryGetValue tryGetValue)
        {
            if (!tryGetValue("query", out var queryValues))
            {
                throw new("Expected to find a form value named 'query'.");
            }

            if (queryValues.Count != 1)
            {
                throw new("Expected 'query' to have a single value.");
            }

            var operation = ReadOperation(tryGetValue);

            return(queryValues.ToString(), GetInputs(tryGetValue), operation);
        }
예제 #11
0
        static Inputs GetInputs(TryGetValue tryGetValue)
        {
            if (!tryGetValue("variables", out var values))
            {
                return(new Inputs());
            }

            if (values.Count != 1)
            {
                throw new Exception("Expected 'variables' to have a single value.");
            }

            var json = values.ToString();

            if (json.Length == 0)
            {
                return(new Inputs());
            }
            var variables = JObject.Parse(json);

            return(variables.ToInputs());
        }
예제 #12
0
        static Inputs GetInputs(TryGetValue tryGetValue)
        {
            if (!tryGetValue("variables", out var values))
            {
                return(Inputs.Empty);
            }

            if (values.Count != 1)
            {
                throw new("Expected 'variables' to have a single value.");
            }

            var json = values.ToString();

            if (json.Length == 0)
            {
                return(Inputs.Empty);
            }
            var variables = json.ToDictionary();

            return(variables.ToInputs());
        }
예제 #13
0
		public AsyncDocumentKeyGeneration(InMemoryDocumentSessionOperations session,TryGetValue tryGetValue, ModifyObjectId modifyObjectId)
		{
			this.session = session;
			this.tryGetValue = tryGetValue;
			this.modifyObjectId = modifyObjectId;
		}
        public async Task <bool> Ask <T>(string question, T defaultValue, Action <T> apply, TryGetValue <T> getValue)
        {
            if (defaultValue != null)
            {
                question += $" (Default {defaultValue})";
            }

            await ReplyAsync(question);

            SocketMessage response;
            T             result;

            do
            {
                response = await Interactive.NextMessageAsync(Context, true, true);

                if (defaultValue != null && response.Content.Length == 1 && response.Content[0] == SkipCharacter)
                {
                    return(false);
                }
            }while (!getValue(response, out result));

            apply(result);

            return(true);
        }
 public static T DefaultIfNull <T>(this string value, TryGetValue <T> evaluator) where T : struct
 {
     return(value.DefaultIfNull(evaluator, default(T)));
 }
예제 #16
0
 public NameValueCollectionReader(TryGetValue <string, string> tryGetValue)
 {
     ArgumentNullException.ThrowIfNull(tryGetValue);
     _tryGetValue = tryGetValue;
 }
예제 #17
0
 public AsyncDocumentIdGeneration(InMemoryDocumentSessionOperations session, TryGetValue tryGetValue, ModifyObjectId modifyObjectId)
 {
     _session        = session;
     _tryGetValue    = tryGetValue;
     _modifyObjectId = modifyObjectId;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="tryGetValue"></param>
 public NameValueCollectionReader(TryGetValue <string, string> tryGetValue)
 {
     Assert.IsNotNull(tryGetValue);
     _tryGetValue = tryGetValue;
 }
예제 #19
0
 public AsyncDocumentKeyGeneration(InMemoryDocumentSessionOperations session, TryGetValue tryGetValue, ModifyObjectId modifyObjectId)
 {
     this.session        = session;
     this.tryGetValue    = tryGetValue;
     this.modifyObjectId = modifyObjectId;
 }