Beispiel #1
0
 public void CamelCaseTest()
 {
     Assert.Equal("", StringEx.CamelCase(""));
     Assert.Equal("cpKey", StringEx.CamelCase("CPKey"));
     Assert.Equal("mySQL", StringEx.CamelCase("MySQL"));
     Assert.Equal("gate2Name", StringEx.CamelCase("gate2Name"));
     Assert.Equal("dawnxV2", StringEx.CamelCase("DAWNXV2"));
     Assert.Throws <ArgumentException>(() => StringEx.CamelCase("Exception\0"));
 }
Beispiel #2
0
 public void CamelCaseTest()
 {
     Assert.Equal("", StringEx.CamelCase(""));
     Assert.Equal("abc", StringEx.CamelCase("ABC"));
     Assert.Equal("cpKey", StringEx.CamelCase("CPKey"));
     Assert.Equal("mySQL", StringEx.CamelCase("MySQL"));
     Assert.Equal("gate2Name", StringEx.CamelCase("gate2Name"));
     Assert.Equal("dawnxV2", StringEx.CamelCase("DAWNXV2"));
 }
Beispiel #3
0
        public TsType(TypeScriptModelBuilder builder, Type clrType, bool cacheProperties)
        {
            var tsTypes = builder.TsTypes;

            ClrType = clrType;

            TsProperties = new Cache <TsProperty[]>
            {
                CacheMethod = () =>
                {
                    if (cacheProperties)
                    {
                        var allProps = clrType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                       .Where(x => !x.GetCustomAttributes().Any(attr => attr.GetType().FullName == $"{nameof(TypeSharp)}.{nameof(TypeScriptIgnoreAttribute)}"));
                        var declaredProps  = allProps.Where(x => x.DeclaringType == clrType);
                        var duplicateNames = allProps.Select(x => x.Name).Intersect(declaredProps.Select(x => x.Name)).ToArray();
                        var props          = allProps.Where(x => !duplicateNames.Contains(x.Name)).Concat(declaredProps);

                        if (clrType.IsGenericType)
                        {
                            if (clrType.IsGenericTypeDefinition)
                            {
                                return(props.Select(prop =>
                                {
                                    var propType = prop.PropertyType;
                                    var required = prop.HasAttributeViaName("System.ComponentModel.DataAnnotations.RequiredAttribute");
                                    if (propType.IsGenericParameter)
                                    {
                                        return new TsProperty
                                        {
                                            ClrName = prop.Name,
                                            PropertyName = StringEx.CamelCase(prop.Name),
                                            PropertyTypeDefinition = propType.Name,
                                            Required = required,
                                        };
                                    }
                                    else
                                    {
                                        return new TsProperty
                                        {
                                            ClrName = prop.Name,
                                            PropertyName = StringEx.CamelCase(prop.Name),
                                            PropertyType = tsTypes[propType].Value,
                                            Required = required,
                                        };
                                    }
                                }).ToArray());
                            }
                            else
                            {
                                return(new TsProperty[0]);
                            }
                        }
                        else
                        {
                            return(props.Select(prop =>
                            {
                                var propType = prop.PropertyType;
                                var required = prop.HasAttributeViaName("System.ComponentModel.DataAnnotations.RequiredAttribute");

                                return new TsProperty
                                {
                                    ClrName = prop.Name,
                                    PropertyName = StringEx.CamelCase(prop.Name),
                                    PropertyType = tsTypes[propType].Value,
                                    Required = required,
                                };
                            }).ToArray());
                        }
                    }
                    else
                    {
                        return(new TsProperty[0]);
                    }
                },
            };
        }