예제 #1
0
        public void ValidType_FullyQualitifedTypeShouldBeCorrect(string value, string expectedFullyQualifiedType)
        {
            var actual = ResourceTypeReference.TryParse(value);

            actual.Should().NotBeNull();
            actual !.FullyQualifiedType.Should().Be(expectedFullyQualifiedType);
        }
예제 #2
0
        public TypeSymbol GetDeclaredType(ResourceScope targetScope, IResourceTypeProvider resourceTypeProvider)
        {
            var stringSyntax = this.TypeString;

            if (stringSyntax != null && stringSyntax.IsInterpolated())
            {
                // TODO: in the future, we can relax this check to allow interpolation with compile-time constants.
                // right now, codegen will still generate a format string however, which will cause problems for the type.
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).ResourceTypeInterpolationUnsupported()));
            }

            var stringContent = stringSyntax?.TryGetLiteralValue();

            if (stringContent == null)
            {
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceType()));
            }

            var typeReference = ResourceTypeReference.TryParse(stringContent);

            if (typeReference == null)
            {
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceType()));
            }

            return(resourceTypeProvider.GetType(targetScope, typeReference));
        }
예제 #3
0
        // TODO: This does not recognize non-resource named objects yet
        public TypeSymbol?GetTypeByName(string?typeName)
        {
            /*
             * Obtaining the type by name currently does not involve processing outgoing edges in the expression graph (function or variable refs)
             * This means that we do not need to check for cycles. However, if that ever changes, ensure that proper cycle detection is added here.
             */

            if (typeName == null)
            {
                return(null);
            }

            if (LanguageConstants.DeclarationTypes.TryGetValue(typeName, out TypeSymbol primitiveType))
            {
                return(primitiveType);
            }

            // TODO: This needs proper namespace, type, and version resolution logic in the future
            ResourceTypeReference?typeReference = ResourceTypeReference.TryParse(typeName);

            if (typeReference == null)
            {
                return(null);
            }

            // TODO: Construct/lookup type information based on JSON schema or swagger
            // for now assuming very basic resource schema
            return(new ResourceType(typeName, LanguageConstants.TopLevelResourceProperties, additionalPropertiesType: null, typeReference));
        }
예제 #4
0
        public override void VisitResourceDeclarationSyntax(ResourceDeclarationSyntax syntax)
        => AssignTypeWithDiagnostics(syntax, diagnostics => {
            var stringSyntax = syntax.TryGetType();

            if (stringSyntax != null && stringSyntax.IsInterpolated())
            {
                // TODO: in the future, we can relax this check to allow interpolation with compile-time constants.
                // right now, codegen will still generate a format string however, which will cause problems for the type.
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).ResourceTypeInterpolationUnsupported()));
            }

            var stringContent = stringSyntax?.TryGetLiteralValue();
            if (stringContent == null)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).InvalidResourceType()));
            }

            // TODO: This needs proper namespace, type, and version resolution logic in the future
            var typeReference = ResourceTypeReference.TryParse(stringContent);
            if (typeReference == null)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).InvalidResourceType()));
            }

            var declaredType = resourceTypeRegistrar.GetType(typeReference);

            return(TypeValidator.NarrowTypeAndCollectDiagnostics(typeManager, syntax.Body, declaredType, diagnostics));
        });
예제 #5
0
        public override void VisitResourceDeclarationSyntax(ResourceDeclarationSyntax syntax)
        => AssignTypeWithCaching(syntax, () => {
            var stringSyntax = syntax.TryGetType();

            if (stringSyntax != null && stringSyntax.IsInterpolated())
            {
                // TODO: in the future, we can relax this check to allow interpolation with compile-time constants.
                // right now, codegen will still generate a format string however, which will cause problems for the type.
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).ResourceTypeInterpolationUnsupported()));
            }

            var stringContent = stringSyntax?.GetLiteralValue();
            if (stringContent == null)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).InvalidResourceType()));
            }

            // TODO: This needs proper namespace, type, and version resolution logic in the future
            var typeReference = ResourceTypeReference.TryParse(stringContent);
            if (typeReference == null)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).InvalidResourceType()));
            }

            // TODO: Construct/lookup type information based on JSON schema or swagger
            // for now assuming very basic resource schema
            return(new ResourceType(stringContent, LanguageConstants.CreateResourceProperties(typeReference), additionalProperties: null, typeReference));
        });
        public void ValidType_ShouldReturnExpectedValue(string value, string expectedVersion, params string[] expectedTypes)
        {
            // local function
            void AssertExpectations(ResourceTypeReference?typeRef)
            {
                typeRef.Should().NotBeNull();

                typeRef !.ApiVersion.Should().Be(expectedVersion);
                typeRef.TypeSegments.Should().Equal(expectedTypes);
            }

            var actual = ResourceTypeReference.TryParse(value);

            AssertExpectations(actual);
        }
예제 #7
0
 public void InvalidType_ShouldBeRejected(string value)
 {
     ResourceTypeReference.TryParse(value).Should().BeNull();
 }
        /// <summary>
        /// Returns the declared type of the resource body (based on the type string).
        /// Returns the same value for single resource or resource loops declarations.
        /// </summary>
        /// <param name="resourceTypeProvider">resource type provider</param>
        public TypeSymbol GetDeclaredType(IBinder binder, IResourceTypeProvider resourceTypeProvider)
        {
            var stringSyntax = this.TypeString;

            if (stringSyntax != null && stringSyntax.IsInterpolated())
            {
                // TODO: in the future, we can relax this check to allow interpolation with compile-time constants.
                // right now, codegen will still generate a format string however, which will cause problems for the type.
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).ResourceTypeInterpolationUnsupported()));
            }

            var stringContent = stringSyntax?.TryGetLiteralValue();

            if (stringContent == null)
            {
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceType()));
            }

            // Before we parse the type name we need to determine if it's a top level resource or not.
            ResourceTypeReference?typeReference;
            var  hasParentDeclaration          = false;
            var  nestedParents                 = binder.GetAllAncestors <ResourceDeclarationSyntax>(this);
            bool isTopLevelResourceDeclaration = nestedParents.Length == 0;

            if (isTopLevelResourceDeclaration)
            {
                // This is a top level resource - the type is a fully-qualified type.
                typeReference = ResourceTypeReference.TryParse(stringContent);
                if (typeReference == null)
                {
                    return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceType()));
                }

                if (binder.GetSymbolInfo(this) is ResourceSymbol resourceSymbol &&
                    binder.TryGetCycle(resourceSymbol) is null &&
                    resourceSymbol.SafeGetBodyPropertyValue(LanguageConstants.ResourceParentPropertyName) is {} referenceParentSyntax&&
                    binder.GetSymbolInfo(referenceParentSyntax) is ResourceSymbol parentResourceSymbol)
                {
                    hasParentDeclaration = true;

                    var parentType = parentResourceSymbol.DeclaringResource.GetDeclaredType(binder, resourceTypeProvider);
                    if (parentType is not ResourceType parentResourceType)
                    {
                        // TODO should we raise an error, or just rely on the error on the parent?
                        return(ErrorType.Create(DiagnosticBuilder.ForPosition(referenceParentSyntax).ParentResourceTypeHasErrors(parentResourceSymbol.DeclaringResource.Name.IdentifierName)));
                    }

                    if (!parentResourceType.TypeReference.IsParentOf(typeReference))
                    {
                        return(ErrorType.Create(DiagnosticBuilder.ForPosition(referenceParentSyntax).ResourceTypeIsNotValidParent(
                                                    typeReference.FullyQualifiedType,
                                                    parentResourceType.TypeReference.FullyQualifiedType)));
                    }
                }
            }
            else
            {
                // This is a nested resource, the type name is a compound of all of the ancestor
                // type names.
                //
                // Ex: 'My.Rp/someType@2020-01-01' -> 'someChild' -> 'someGrandchild'

                // The top-most resource must have a qualified type name.
                hasParentDeclaration = true;
                var baseTypeStringContent = nestedParents[0].TypeString?.TryGetLiteralValue();
                if (baseTypeStringContent == null)
                {
                    return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(nestedParents[0].Name.IdentifierName)));
                }

                var baseTypeReference = ResourceTypeReference.TryParse(baseTypeStringContent);
                if (baseTypeReference == null)
                {
                    return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(nestedParents[0].Name.IdentifierName)));
                }

                // Collect each other ancestor resource's type.
                var typeSegments = new List <string>();
                for (var i = 1; i < nestedParents.Length; i++)
                {
                    var typeSegmentStringContent = nestedParents[i].TypeString?.TryGetLiteralValue();
                    if (typeSegmentStringContent == null)
                    {
                        return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(nestedParents[i].Name.IdentifierName)));
                    }

                    typeSegments.Add(typeSegmentStringContent);
                }

                // Add *this* resource's type
                typeSegments.Add(stringContent);

                // If this fails, let's walk through and find the root cause. This could be confusing
                // for people seeing it the first time.
                typeReference = ResourceTypeReference.TryCombine(baseTypeReference, typeSegments);
                if (typeReference == null)
                {
                    // We'll special case the last one since it refers to *this* resource. We don't
                    // want to cascade a bunch of noisy errors for parents, they get their own errors.
                    for (var j = 0; j < typeSegments.Count - 1; j++)
                    {
                        if (!ResourceTypeReference.TryParseSingleTypeSegment(typeSegments[j], out _, out _))
                        {
                            return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(nestedParents[j + 1].Name.IdentifierName)));
                        }
                    }

                    if (!ResourceTypeReference.TryParseSingleTypeSegment(stringContent, out _, out _))
                    {
                        // OK this resource is the one that's wrong.
                        return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceTypeSegment(stringContent)));
                    }

                    // Something went wrong, this should be unreachable.
                    throw new InvalidOperationException("Failed to find the root cause of an invalid compound resource type.");
                }
            }

            var flags = ResourceTypeGenerationFlags.None;

            if (IsExistingResource())
            {
                flags |= ResourceTypeGenerationFlags.ExistingResource;
            }

            if (!isTopLevelResourceDeclaration)
            {
                flags |= ResourceTypeGenerationFlags.NestedResource;
            }

            if (typeReference.IsRootType || hasParentDeclaration)
            {
                flags |= ResourceTypeGenerationFlags.PermitLiteralNameProperty;
            }

            return(resourceTypeProvider.GetType(typeReference, flags));
        }
예제 #9
0
        /// <summary>
        /// Returns the declared type of the resource body (based on the type string).
        /// Returns the same value for single resource or resource loops declarations.
        /// </summary>
        /// <param name="resourceTypeProvider">resource type provider</param>
        public TypeSymbol GetDeclaredType(IBinder binder, IResourceTypeProvider resourceTypeProvider)
        {
            var stringSyntax = this.TypeString;

            if (stringSyntax != null && stringSyntax.IsInterpolated())
            {
                // TODO: in the future, we can relax this check to allow interpolation with compile-time constants.
                // right now, codegen will still generate a format string however, which will cause problems for the type.
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).ResourceTypeInterpolationUnsupported()));
            }

            var stringContent = stringSyntax?.TryGetLiteralValue();

            if (stringContent == null)
            {
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceType()));
            }

            // Before we parse the type name we need to determine if it's a top level resource or not.
            ResourceTypeReference?typeReference;
            var ancestors = binder.GetAllAncestors <ResourceDeclarationSyntax>(this);

            if (ancestors.Length == 0)
            {
                // This is a top level resource - the type is a fully-qualified type.
                typeReference = ResourceTypeReference.TryParse(stringContent);
                if (typeReference == null)
                {
                    return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceType()));
                }
            }
            else
            {
                // This is a nested resource, the type name is a compound of all of the ancestor
                // type names.
                //
                // Ex: 'My.Rp/someType@2020-01-01' -> 'someChild' -> 'someGrandchild'

                // The top-most resource must have a qualified type name.
                var baseTypeStringContent = ancestors[0].TypeString?.TryGetLiteralValue();
                if (baseTypeStringContent == null)
                {
                    return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(ancestors[0].Name.IdentifierName)));
                }

                var baseTypeReference = ResourceTypeReference.TryParse(baseTypeStringContent);
                if (baseTypeReference == null)
                {
                    return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(ancestors[0].Name.IdentifierName)));
                }

                // Collect each other ancestor resource's type.
                var typeSegments = new List <string>();
                for (var i = 1; i < ancestors.Length; i++)
                {
                    var typeSegmentStringContent = ancestors[i].TypeString?.TryGetLiteralValue();
                    if (typeSegmentStringContent == null)
                    {
                        return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(ancestors[i].Name.IdentifierName)));
                    }

                    typeSegments.Add(typeSegmentStringContent);
                }

                // Add *this* resource's type
                typeSegments.Add(stringContent);

                // If this fails, let's walk through and find the root cause. This could be confusing
                // for people seeing it the first time.
                typeReference = ResourceTypeReference.TryCombine(baseTypeReference, typeSegments);
                if (typeReference == null)
                {
                    // We'll special case the last one since it refers to *this* resource. We don't
                    // want to cascade a bunch of noisy errors for parents, they get their own errors.
                    for (var j = 0; j < typeSegments.Count - 1; j++)
                    {
                        if (!ResourceTypeReference.TryParseSingleTypeSegment(typeSegments[j], out _, out _))
                        {
                            return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidAncestorResourceType(ancestors[j + 1].Name.IdentifierName)));
                        }
                    }

                    if (!ResourceTypeReference.TryParseSingleTypeSegment(stringContent, out _, out _))
                    {
                        // OK this resource is the one that's wrong.
                        return(ErrorType.Create(DiagnosticBuilder.ForPosition(this.Type).InvalidResourceTypeSegment(stringContent)));
                    }

                    // Something went wrong, this should be unreachable.
                    throw new InvalidOperationException("Failed to find the root cause of an invalid compound resource type.");
                }
            }

            return(resourceTypeProvider.GetType(typeReference, IsExistingResource()));
        }