Esempio n. 1
0
        private GenerateResult Process()
        {
            foreach (var resource in definition.ResourceDefinitions)
            {
                var descriptor = resource.Descriptor;
                var putBody    = resource.DeclaringMethod.Body?.ModelType as CompositeType;
                var getBody    = (resource.GetMethod?.Responses.GetValueOrDefault(HttpStatusCode.OK)?.Body as CompositeType) ??
                                 (resource.GetMethod?.DefaultResponse?.Body as CompositeType) ??
                                 putBody;

                var(success, failureReason, resourceName) = ParseNameSchema(resource, definition);
                if (!success)
                {
                    CodeModelProcessor.LogWarning($"Skipping resource type {descriptor.FullyQualifiedType} under path '{resource.DeclaringMethod.Url}': {failureReason}");
                    continue;
                }

                if (putBody == null)
                {
                    CodeModelProcessor.LogWarning($"Skipping resource type {descriptor.FullyQualifiedType} under path '{resource.DeclaringMethod.Url}': No resource body defined");
                    continue;
                }

                var resourceProperties = GetStandardizedResourceProperties(resource.Descriptor, resourceName);
                var resourceDefinition = CreateObject(descriptor.FullyQualifiedType, putBody, resourceProperties);

                resource.Type = factory.Create(() => new ResourceType
                {
                    Name = $"{descriptor.FullyQualifiedType}@{descriptor.ApiVersion}",
                    Body = factory.GetReference(resourceDefinition),
                });

                var putProperties = putBody.ComposedProperties
                                    .Where(p => p.SerializedName != null)
                                    .Where(p => !resourceProperties.ContainsKey(p.SerializedName))
                                    .ToDictionary(p => p.SerializedName, StringComparer.OrdinalIgnoreCase);

                var getProperties = getBody.ComposedProperties
                                    .Where(p => p.SerializedName != null)
                                    .Where(p => !resourceProperties.ContainsKey(p.SerializedName))
                                    .ToDictionary(p => p.SerializedName, StringComparer.OrdinalIgnoreCase);

                foreach (var property in putProperties.Keys.Concat(getProperties.Keys.Where(x => !putProperties.ContainsKey(x))))
                {
                    var isWritable = putProperties.TryGetValue(property, out var putProperty);
                    var isReadable = getProperties.TryGetValue(property, out var getProperty);

                    var propertyDefinition = ParseType(putProperty?.ModelType, getProperty?.ModelType);
                    if (propertyDefinition != null)
                    {
                        var flags = ParsePropertyFlags(putProperty, getProperty);
                        resourceProperties[property] = CreateObjectProperty(propertyDefinition, flags);
                    }
                }

                if (resourceDefinition is DiscriminatedObjectType discriminatedObjectType)
                {
                    HandlePolymorphicType(discriminatedObjectType, putBody, getBody);
                }
            }

            return(new GenerateResult(
                       definition.Namespace,
                       definition.ApiVersion,
                       factory,
                       definition.ResourceDefinitions.Select(x => x.Descriptor)));
        }