Beispiel #1
0
        private (string, EndpointSchema, IEnumerable <Type>) GenerateEndpoint(PropertyInfo property, SchemaGenerationOptions options, ServiceSchema serviceSchema)
        {
            var endpointAttr = property.GetCustomAttribute <EndpointAttribute>();
            var endpointName = endpointAttr?.Name ?? options.MemberNameToSchemaName?.Invoke(property.Name, false);

            if (endpointName == null)
            {
                throw new ServiceInterfaceException($"Unknown endpoint name '{property.Name}' of {typeof(T)}");
            }
            var propertyTypeInfo = property.PropertyType.GetTypeInfo();

            if (!propertyTypeInfo.IsGenericType)
            {
                throw new ServiceInterfaceException($"Unknown property type '{property.Name}' of {typeof(T)}");
            }
            var            genericType = propertyTypeInfo.GetGenericTypeDefinition();
            EndpointSchema endpointSchema;
            List <Type>    types = new List <Type>();

            if (genericType == typeof(IEvent <>))
            {
                var(type, schema) = GenerateContract(propertyTypeInfo.GenericTypeArguments[0], options);
                if (type != null)
                {
                    types.Add(type);
                }
                endpointSchema = new EventEndpointSchema
                {
                    Event = schema
                };
            }
            else
            if (genericType == typeof(ICommand <>))
            {
                var(type, schema) = GenerateContract(propertyTypeInfo.GenericTypeArguments[0], options);
                if (type != null)
                {
                    types.Add(type);
                }
                endpointSchema = new CommandEndpointSchema
                {
                    Command = schema
                };
            }
            else
            if (genericType == typeof(ICallable <,>))
            {
                var(intype, inschema) = GenerateContract(propertyTypeInfo.GenericTypeArguments[0], options);
                if (intype != null)
                {
                    types.Add(intype);
                }
                var(outtype, outschema) = GenerateContract(propertyTypeInfo.GenericTypeArguments[1], options);
                if (outtype != null)
                {
                    types.Add(outtype);
                }
                endpointSchema = new CallableEndpointSchema
                {
                    Request  = inschema,
                    Response = outschema
                };
            }
            else
            {
                throw new ServiceInterfaceException($"Unknown property type '{property.Name}' of {typeof(T)}");
            }
            endpointSchema.Title      = property.Name;
            endpointSchema.Transports = GetTransports(property);
            endpointSchema.Owner      = serviceSchema;
            options.Extensions.Iter(p => p.ExtendEndpoint(property, endpointSchema));
            return(endpointName, endpointSchema, types);
        }