예제 #1
0
        /// <summary>
        /// Initializes static members of the <see cref="EnumResolver{T}"/> class.
        /// </summary>
        static EnumResolver()
        {
            var type = typeof(T);

            if (!type.GetTypeInfo().IsSubclassOf(typeof(Enum)))
            {
                throw new Exception($"{type.Name} should be enum");
            }

            var names        = Enum.GetNames(type);
            var descriptions = new Dictionary <string, string>();

            foreach (var name in names)
            {
                var property  = type.GetTypeInfo().GetMember(name).FirstOrDefault();
                var attribute = property?.GetCustomAttribute <ApiDescriptionAttribute>();
                if (attribute != null)
                {
                    descriptions[name] = attribute.Description;
                }
            }

            GeneratedType = new ApiEnumType(ApiDescriptionAttribute.GetTypeName(type), names, descriptions);
            hasFlags      = type.GetTypeInfo().GetCustomAttribute <FlagsAttribute>() != null;
        }
        public async Task EnumApiTest()
        {
            var enumType = new ApiEnumType("EnumType", new[] { "item1", "item2" });

            var api = new ApiDescription(
                "TestApi1",
                "0.0.0.1",
                new ApiType[] { enumType },
                new[] { ApiField.Object("enumField", enumType.TypeName) });

            var provider = new MoqProvider {
                Description = api, Data = "{\"enumField\": \"item2\"}"
            };

            var schema = SchemaGenerator.Generate(new List <ApiProvider> {
                provider
            });

            using (var printer = new SchemaPrinter(schema))
            {
                var description = printer.Print();
                this.output.WriteLine("-------- Schema -----------");
                this.output.WriteLine(description);
                Assert.False(string.IsNullOrWhiteSpace(description));
            }

            Assert.NotNull(schema.Query);
            Assert.Equal(3, schema.Query.Fields.Count());
            Assert.True(schema.Query.HasField("api"));

            var result = await new DocumentExecuter().ExecuteAsync(
                r =>
            {
                r.Schema = schema;
                r.Query  = "query { api { enumField } } ";
            }).ConfigureAwait(true);

            this.output.WriteLine("-------- Response -----------");
            var response = new DocumentWriter(true).Write(result);

            this.output.WriteLine(response);

            var expectedResponse = @"{
                                      ""data"": {
                                        ""api"": {
                                            ""enumField"": ""item2"" 
                                        }
                                     }                                      
                                    }";

            Assert.Equal(CleanResponse(expectedResponse), CleanResponse(response));
        }
예제 #3
0
        /// <summary>
        /// Initializes static members of the <see cref="CollectionResolver{T}"/> class.
        /// </summary>
        static CollectionResolver()
        {
            FilterType = new ApiObjectType($"{ApiDescriptionAttribute.GetTypeName(typeof(T))}_Filter");
            SortType   = new ApiEnumType($"{ApiDescriptionAttribute.GetTypeName(typeof(T))}_Sort");

            var param = Expression.Parameter(typeof(T));

            GetIdValue =
                Expression.Lambda <Func <T, object> >(
                    Expression.Convert(Expression.Property(param, NodeMetaData.KeyProperty), typeof(object)),
                    param).Compile();

            InitializeType();
        }
예제 #4
0
        /// <summary>
        /// Creates the enum type definition
        /// </summary>
        /// <param name="apiEnumType">Api enum type definition</param>
        /// <param name="provider">The api provider</param>
        /// <param name="typesCreated">The list of types created to fill</param>
        /// <returns>The enum type</returns>
        private static MergedType CreateEnumType(
            ApiEnumType apiEnumType,
            ApiProvider provider,
            Dictionary <string, MergedType> typesCreated)
        {
            MergedType createdType;
            var        mergedEnumType = new MergedEnumType(apiEnumType, provider);

            if (typesCreated.TryGetValue(mergedEnumType.ComplexTypeName, out createdType))
            {
                return(createdType);
            }

            typesCreated[mergedEnumType.ComplexTypeName] = mergedEnumType;

            return(mergedEnumType);
        }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MergedEnumType"/> class.
 /// </summary>
 /// <param name="apiEnumType">
 /// The api enum type.
 /// </param>
 /// <param name="provider">
 /// The provider.
 /// </param>
 public MergedEnumType(ApiEnumType apiEnumType, ApiProvider provider)
     : base(apiEnumType.TypeName)
 {
     this.apiEnumType = apiEnumType;
     this.Provider    = provider;
 }