Ejemplo n.º 1
0
        protected override IReadOnlyCollection <ResourceFieldAttribute> OnResolveFieldChain(string path, FieldChainRequirements chainRequirements)
        {
            if (chainRequirements == FieldChainRequirements.EndsInToMany)
            {
                return(ChainResolver.ResolveToOneChainEndingInToMany(_resourceContextInScope, path, _validateSingleFieldCallback));
            }

            if (chainRequirements == FieldChainRequirements.EndsInAttribute)
            {
                return(ChainResolver.ResolveToOneChainEndingInAttribute(_resourceContextInScope, path, _validateSingleFieldCallback));
            }

            if (chainRequirements.HasFlag(FieldChainRequirements.EndsInAttribute) && chainRequirements.HasFlag(FieldChainRequirements.EndsInToOne))
            {
                return(ChainResolver.ResolveToOneChainEndingInAttributeOrToOne(_resourceContextInScope, path, _validateSingleFieldCallback));
            }

            throw new InvalidOperationException($"Unexpected combination of chain requirement flags '{chainRequirements}'.");
        }
Ejemplo n.º 2
0
        protected ComparisonExpression ParseComparison(string operatorName)
        {
            var comparisonOperator = Enum.Parse <ComparisonOperator>(operatorName.Pascalize());

            EatText(operatorName);
            EatSingleCharacterToken(TokenKind.OpenParen);

            // Allow equality comparison of a HasOne relationship with null.
            FieldChainRequirements leftChainRequirements = comparisonOperator == ComparisonOperator.Equals
                ? FieldChainRequirements.EndsInAttribute | FieldChainRequirements.EndsInToOne
                : FieldChainRequirements.EndsInAttribute;

            QueryExpression leftTerm = ParseCountOrField(leftChainRequirements);

            EatSingleCharacterToken(TokenKind.Comma);

            QueryExpression rightTerm = ParseCountOrConstantOrNullOrField(FieldChainRequirements.EndsInAttribute);

            EatSingleCharacterToken(TokenKind.CloseParen);

            if (leftTerm is ResourceFieldChainExpression leftChain)
            {
                if (leftChainRequirements.HasFlag(FieldChainRequirements.EndsInToOne) && !(rightTerm is NullConstantExpression))
                {
                    // Run another pass over left chain to have it fail when chain ends in relationship.
                    OnResolveFieldChain(leftChain.ToString(), FieldChainRequirements.EndsInAttribute);
                }

                PropertyInfo leftProperty = leftChain.Fields.Last().Property;

                if (leftProperty.Name == nameof(Identifiable.Id) && rightTerm is LiteralConstantExpression rightConstant)
                {
                    string id = DeObfuscateStringId(leftProperty.ReflectedType, rightConstant.Value);
                    rightTerm = new LiteralConstantExpression(id);
                }
            }

            return(new ComparisonExpression(comparisonOperator, leftTerm, rightTerm));
        }