Example #1
0
        private ValueOrError <IConceptInfo> ParseMembers(ITokenReader tokenReader, IConceptInfo lastConcept, bool readingAReference)
        {
            IConceptInfo conceptInfo = (IConceptInfo)Activator.CreateInstance(ConceptInfoType);
            bool         firstMember = true;
            bool         lastPropertyWasInlineParent = false;
            bool         lastConceptUsed             = false;

            var listOfMembers = readingAReference ? Members.Where(m => m.IsKey) : Members.Where(m => m.IsParsable);

            foreach (ConceptMember member in listOfMembers)
            {
                var valueOrError = ReadMemberValue(member, tokenReader, lastConcept, firstMember, ref lastPropertyWasInlineParent, ref lastConceptUsed, readingAReference);

                if (valueOrError.IsError)
                {
                    return(ValueOrError <IConceptInfo> .CreateError(string.Format(CultureInfo.InvariantCulture,
                                                                                  "Cannot read the value of {0} in {1}. {2}",
                                                                                  member.Name, ConceptInfoType.Name, valueOrError.Error)));
                }

                member.SetMemberValue(conceptInfo, valueOrError.Value);
                firstMember = false;
            }

            if (!lastConceptUsed && lastConcept != null)
            {
                return(ValueOrError <IConceptInfo> .CreateError(string.Format(
                                                                    "This concept cannot be enclosed within {0}. Trying to read {1}.",
                                                                    lastConcept.GetType().Name, ConceptInfoType.Name)));
            }

            return(ValueOrError <IConceptInfo> .CreateValue(conceptInfo));
        }
Example #2
0
        private async Task <ValueOrError <TResponse> > SendRequestWithCustomParser <TParameters, TResponse>(TParameters parameters, string method, Func <string, TResponse> parseResponse)
        {
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");
            var results = await HttpClient.PostAsync($"Resources/AspNetFormsAuth/Authentication/{method}", content);

            var responseContent = await results.Content.ReadAsStringAsync();

            if (results.StatusCode == HttpStatusCode.OK)
            {
                return(ValueOrError <TResponse> .CreateValue(parseResponse(responseContent)));
            }
            else if (results.StatusCode == HttpStatusCode.BadRequest || results.StatusCode == HttpStatusCode.Unauthorized)
            {
                string errorMessage = "";
                if (results.StatusCode == HttpStatusCode.BadRequest)
                {
                    errorMessage = JsonSerializer.Deserialize <RhetosErrorResponse>(responseContent).UserMessage;
                }
                return(ValueOrError <TResponse> .CreateError($"({(int)results.StatusCode}) {errorMessage}"));
            }
            else
            {
                throw new Exception("Response from server: " + responseContent);
            }
        }
Example #3
0
        public void ValueOrError_CreateValue()
        {
            var val = ValueOrError.CreateValue <int>(42);

            Assert.AreEqual(42, val.Value);
            Assert.AreEqual(ValueOrErrorKind.Value, val.Kind);
            Assert.ThrowsException <InvalidOperationException>(() => val.Exception);
        }
Example #4
0
        private ValueOrError <IConceptInfo> ParseMembers(ITokenReader tokenReader, IConceptInfo useLastConcept, bool readingAReference, ref bool parsedFirstReferenceElement)
        {
            IConceptInfo conceptInfo = (IConceptInfo)Activator.CreateInstance(ConceptInfoType);
            bool         firstMember = true;

            var listOfMembers = readingAReference ? Members.Where(m => m.IsKey) : Members.Where(m => m.IsParsable);

            var parentProperty = listOfMembers.LastOrDefault(member => member.IsParentNested)
                                 ?? (listOfMembers.First().IsConceptInfo ? listOfMembers.First() : null);

            if (useLastConcept != null && parentProperty == null)
            {
                return(ValueOrError <IConceptInfo> .CreateError($"This concept cannot be nested within {useLastConcept.GetType().Name}. Trying to read {ConceptInfoType.Name}."));
            }

            foreach (ConceptMember member in listOfMembers)
            {
                if (!readingAReference)
                {
                    parsedFirstReferenceElement = false; // Reset a reference elements group, that should separated by dot.
                }
                var valueOrError = ReadMemberValue(member, tokenReader, member == parentProperty ? useLastConcept : null, firstMember, ref parsedFirstReferenceElement, readingAReference);
                OnMemberRead?.Invoke(tokenReader, conceptInfo, member, valueOrError);

                if (valueOrError.IsError)
                {
                    return(ValueOrError <IConceptInfo> .CreateError(string.Format(CultureInfo.InvariantCulture,
                                                                                  "Cannot read the value of {0} in {1}. {2}",
                                                                                  member.Name, ConceptInfoType.Name, valueOrError.Error)));
                }

                member.SetMemberValue(conceptInfo, valueOrError.Value);
                firstMember = false;
            }

            return(ValueOrError <IConceptInfo> .CreateValue(conceptInfo));
        }
        private ValueOrError <ConceptSyntaxNode> ParseMembers(ITokenReader tokenReader, ConceptSyntaxNode useLastConcept, bool readingAReference, ref bool parsedFirstReferenceElement)
        {
            ConceptSyntaxNode node = new ConceptSyntaxNode(_conceptType);
            bool firstMember       = true;

            var listOfMembers = readingAReference ? _conceptType.Members.Where(m => m.IsKey) : _conceptType.Members.Where(m => m.IsParsable);

            var parentProperty = GetParentProperty(listOfMembers);

            if (useLastConcept != null && parentProperty == null)
            {
                return(ValueOrError <ConceptSyntaxNode> .CreateError($"This concept cannot be nested within {useLastConcept.Concept.TypeName}. Trying to read {_conceptType.TypeName}."));
            }

            foreach (ConceptMemberSyntax member in listOfMembers)
            {
                if (!readingAReference)
                {
                    parsedFirstReferenceElement = false; // Reset a reference elements group, that should separated by dot.
                }
                var valueOrError = ReadMemberValue(member, tokenReader, member == parentProperty ? useLastConcept : null, firstMember, ref parsedFirstReferenceElement, readingAReference);
                OnMemberRead?.Invoke(tokenReader, node, member, valueOrError);

                if (valueOrError.IsError)
                {
                    return(ValueOrError <ConceptSyntaxNode> .CreateError(string.Format(CultureInfo.InvariantCulture,
                                                                                       "Cannot read the value of {0} in {1}. {2}",
                                                                                       member.Name, _conceptType.TypeName, valueOrError.Error)));
                }

                member.SetMemberValue(node, valueOrError.Value);
                firstMember = false;
            }

            return(ValueOrError <ConceptSyntaxNode> .CreateValue(node));
        }