public void TestPickTypeAlways()
 {
     Assert.True(CherryPicking.IsCherryType(typeof(DemoWebApi.Models.ExternalLoginViewModel), CherryPickingMethods.All));
 }
Пример #2
0
        /// <summary>
        /// Create TypeScript CodeDOM for POCO types.
        /// For an enum type, all members will be processed regardless of EnumMemberAttribute.
        /// </summary>
        /// <param name="types">POCO types.</param>
        /// <param name="methods"></param>
        public void CreateCodeDom(Type[] types, CherryPickingMethods methods)
        {
            if (types == null)
            {
                throw new ArgumentNullException("types", "types is not defined.");
            }

            this.pendingTypes.AddRange(types);
            var typeGroupedByNamespace = types
                                         .GroupBy(d => d.Namespace)
                                         .OrderBy(k => k.Key); // order by namespace
            var namespacesOfTypes = typeGroupedByNamespace.Select(d => d.Key).ToArray();

            foreach (var groupedTypes in typeGroupedByNamespace)
            {
                var clientNamespaceText = (groupedTypes.Key + ".Client").Replace('.', '_');
                var clientNamespace     = new CodeNamespace(clientNamespaceText);
                targetUnit.Namespaces.Add(clientNamespace);                //namespace added to Dom

                Debug.WriteLine("Generating types in namespace: " + groupedTypes.Key + " ...");
                groupedTypes.OrderBy(t => t.Name).Select(type =>
                {
                    var tsName = type.Name;
                    Debug.WriteLine("tsClass: " + clientNamespace + "  " + tsName);

                    CodeTypeDeclaration typeDeclaration;
                    if (TypeHelper.IsClassOrStruct(type))
                    {
                        if (type.IsGenericType)
                        {
                            typeDeclaration = PodGenHelper.CreatePodClientGenericInterface(clientNamespace, type);
                        }
                        else
                        {
                            typeDeclaration = PodGenHelper.CreatePodClientInterface(clientNamespace, tsName);
                        }

                        if (!type.IsValueType)
                        {
                            if (namespacesOfTypes.Contains(type.BaseType.Namespace))
                            {
                                typeDeclaration.BaseTypes.Add(RefineCustomComplexTypeText(type.BaseType));
                            }
                            else
                            {
                                typeDeclaration.BaseTypes.Add(type.BaseType);
                            }
                        }

                        CreateTypeDocComment(type, typeDeclaration);

                        var typeProperties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).OrderBy(p => p.Name).ToArray();
                        foreach (var propertyInfo in typeProperties)
                        {
                            var cherryType = CherryPicking.GetMemberCherryType(propertyInfo, methods);
                            if (cherryType == CherryType.None)
                            {
                                continue;
                            }
                            string tsPropertyName;


                            var isRequired = cherryType == CherryType.BigCherry;
                            var customName = CherryPicking.GetFieldCustomName(propertyInfo, methods);
                            if (String.IsNullOrEmpty(customName))
                            {
                                tsPropertyName = Fonlow.TypeScriptCodeDom.TsCodeGenerationOptions.Instance.CamelCase ? Fonlow.Text.StringExtensions.ToCamelCase(propertyInfo.Name) : propertyInfo.Name;
                            }
                            else
                            {
                                tsPropertyName = customName;
                            }

                            Debug.WriteLine(String.Format("{0} : {1}", tsPropertyName, propertyInfo.PropertyType.Name));
                            var clientField = new CodeMemberField()                            //Yes, clr property translated to ts field
                            {
                                Name = tsPropertyName + (isRequired ? String.Empty : "?"),
                                Type = TranslateToClientTypeReference(propertyInfo.PropertyType),
                                //                     Attributes = MemberAttributes.Public,
                            };

                            CreatePropertyDocComment(propertyInfo, clientField);

                            typeDeclaration.Members.Add(clientField);
                        }

                        var typeFields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).OrderBy(f => f.Name).ToArray();
                        foreach (var fieldInfo in typeFields)
                        {
                            var cherryType = CherryPicking.GetMemberCherryType(fieldInfo, methods);
                            if (cherryType == CherryType.None)
                            {
                                continue;
                            }
                            string tsPropertyName;


                            var isRequired = (cherryType == CherryType.BigCherry) || !type.IsClass;                            //public fields in struct should all be value types, so required
                            var customName = CherryPicking.GetFieldCustomName(fieldInfo, methods);
                            if (String.IsNullOrEmpty(customName))
                            {
                                tsPropertyName = Fonlow.TypeScriptCodeDom.TsCodeGenerationOptions.Instance.CamelCase ? Fonlow.Text.StringExtensions.ToCamelCase(fieldInfo.Name) : fieldInfo.Name;
                            }
                            else
                            {
                                tsPropertyName = customName;
                            }

                            Debug.WriteLine(String.Format("{0} : {1}", tsPropertyName, fieldInfo.FieldType.Name));

                            var clientField = new CodeMemberField()
                            {
                                Name = tsPropertyName + (isRequired ? String.Empty : "?"),
                                Type = TranslateToClientTypeReference(fieldInfo.FieldType),
                            };

                            CreateFieldDocComment(fieldInfo, clientField);

                            typeDeclaration.Members.Add(clientField);
                        }
                    }
                    else if (type.IsEnum)
                    {
                        typeDeclaration = PodGenHelper.CreatePodClientEnum(clientNamespace, tsName);

                        CreateTypeDocComment(type, typeDeclaration);

                        int k = 0;
                        foreach (var fieldInfo in type.GetFields(BindingFlags.Public | BindingFlags.Static))
                        {
                            var name     = fieldInfo.Name;
                            var intValue = (int)Convert.ChangeType(fieldInfo.GetValue(null), typeof(int));
                            Debug.WriteLine(name + " -- " + intValue);
                            var isInitialized = intValue != k;

                            var clientField = new CodeMemberField()
                            {
                                Name           = name,
                                Type           = new CodeTypeReference(fieldInfo.FieldType),
                                InitExpression = isInitialized ? new CodePrimitiveExpression(intValue) : null,
                            };

                            CreateFieldDocComment(fieldInfo, clientField);

                            typeDeclaration.Members.Add(clientField);
                            k++;
                        }
                    }
                    else
                    {
                        Trace.TraceWarning("Not yet supported: " + type.Name);
                        typeDeclaration = null;
                    }

                    return(typeDeclaration);
                }
                                                         ).ToArray();//add classes into the namespace
            }
        }
 public void TestReadSerializableAttribute()
 {
     Assert.True(CherryPicking.IsCherryType(typeof(DemoWebApi.Models.ExternalLoginViewModel), CherryPickingMethods.Serializable));
 }
 public void TestReadJsonObjectAttributePickNone()
 {
     Assert.False(CherryPicking.IsCherryType(typeof(DemoWebApi.DemoData.Address), CherryPickingMethods.NewtonsoftJson));
 }
 public void TestReadJsonObjectAttribute()
 {
     Assert.True(CherryPicking.IsCherryType(typeof(DemoWebApi.Models.ChangePasswordBindingModel), CherryPickingMethods.NewtonsoftJson));
 }
 public void TestReadDataContractAttributePickNone()
 {
     Assert.False(CherryPicking.IsCherryType(typeof(DemoWebApi.Models.ChangePasswordBindingModel), CherryPickingMethods.DataContract));
 }
 public void TestReadDataContractAttribute()
 {
     Assert.True(CherryPicking.IsCherryType(typeof(DemoWebApi.DemoData.Address), CherryPickingMethods.DataContract));
 }