コード例 #1
0
ファイル: GenericParser.cs プロジェクト: ironcev-forks/Rhetos
        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));
        }
コード例 #2
0
        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));
        }