示例#1
0
        public static string FormatPropertyType(TsProperty property, string memberTypeName)
        {
            if (property.MemberInfo.DeclaringType.GetProperty(property.MemberInfo.Name).PropertyType == typeof(IDictionary <string, string>))
            {
                return("{ [key: string]: string }");
            }

            if (property.MemberInfo.DeclaringType.GetProperty(property.MemberInfo.Name).PropertyType == typeof(Guid))
            {
                return("string");
            }

            if (property.MemberInfo.DeclaringType.GetProperty(property.MemberInfo.Name).PropertyType == typeof(Stream))
            {
                return("any");
            }

            if (property.MemberInfo.DeclaringType.GetProperty(property.MemberInfo.Name).PropertyType.Name.StartsWith(nameof(IEnumerable), StringComparison.Ordinal))
            {
                return(memberTypeName + "[]");
            }

            if (property.MemberInfo.DeclaringType.GetProperty(property.MemberInfo.Name).PropertyType.Name.StartsWith(nameof(ICollection), StringComparison.Ordinal))
            {
                return(memberTypeName + "[]");
            }

            return(memberTypeName);
        }
示例#2
0
        public string DefaultMemberTypeFormatter(TsProperty tsProperty, string memberTypeName)
        {
            var asCollection = tsProperty.PropertyType as TsCollection;
            var isCollection = asCollection != null;

            return(memberTypeName + (isCollection ? string.Concat(Enumerable.Repeat("[]", asCollection.Dimension)) : ""));
        }
示例#3
0
        public static string FormatPropertyName(TsProperty property)
        {
            // These are mapped as arguments from the client side.
            if (property.Name == nameof(RequestPacket.ArgumentsStream))
            {
                return("Arguments");
            }

            var declaringType = property.MemberInfo.DeclaringType;

            // Request type arguments are optional
            // TODO: Leverage [Required] to know what is needed and not?
            if (!declaringType.Name.Contains(nameof(Packet)) &&
                declaringType.Name.Contains(nameof(Request)))
            {
                return($"{property.Name}?");
            }

            if (declaringType.Name == nameof(Packet) &&
                declaringType.GetProperty(property.MemberInfo.Name).Name == nameof(Packet.Type))
            {
                return($"{property.Name}?");
            }

            return(property.Name);
        }
        private static string FormatMember(TsProperty property)
        {
            var declaringType = property.MemberInfo.DeclaringType;
            var propertyName  = property.MemberInfo.Name;

            if (declaringType == null)
            {
                return(propertyName.SnakeCase().QuoteMaybe());
            }
            var iface         = declaringType.GetInterfaces().FirstOrDefault(ii => ii.Name == "I" + declaringType.Name);
            var ifaceProperty = iface?.GetProperty(propertyName);

            var attributes = new List <Attribute>();

            if (ifaceProperty != null)
            {
                attributes.AddRange(ifaceProperty.GetCustomAttributes());
            }
            attributes.AddRange(property.MemberInfo.GetCustomAttributes());
            if (attributes.Any(a => a.TypeId.ToString() == "Nest.Json.JsonIgnoreAttribute"))
            {
                property.IsIgnored = true;
            }

            var jsonPropertyAttribute = attributes.FirstOrDefault(a => a.TypeId.ToString() == "Nest.Json.JsonPropertyAttribute");

            if (jsonPropertyAttribute != null)
            {
                var v = jsonPropertyAttribute.GetType().GetProperty("PropertyName").GetGetMethod().Invoke(jsonPropertyAttribute, new object[] {});
                return(((string)v ?? propertyName.SnakeCase()).QuoteMaybe());
            }
            return(propertyName.SnakeCase().QuoteMaybe());
        }
示例#5
0
        public void WhenInitializedAndIsAnnotatedWithIgnoreAttribute_IsIgnoresIsSetToTrue()
        {
            var propertyInfo = typeof(Product).GetProperty("Ignored");

            var target = new TsProperty(propertyInfo);

            Assert.True(target.IsIgnored);
        }
示例#6
0
        public void WhenInitializedAndHasCustomNameInAttribute_CustomNameIsUsed()
        {
            var propertyInfo = typeof(CustomClassName).GetProperty("CustomPorperty");

            var target = new TsProperty(propertyInfo);

            Assert.Equal("MyProperty", target.Name);
        }
示例#7
0
        public void WhenInitialized_PropertyTypeIsSet()
        {
            var propertyInfo = typeof(Person).GetProperty("Name");

            var target = new TsProperty(propertyInfo);

            Assert.Equal(propertyInfo.PropertyType, target.PropertyType.Type);
        }
示例#8
0
        public void WhenInitialized_IsOptionalIsFalse()
        {
            var propertyInfo = typeof(Person).GetProperty("Name");

            var target = new TsProperty(propertyInfo);

            Assert.False(target.IsOptional);
        }
示例#9
0
        public void WhenInitialized_PropertyInfoIsSet()
        {
            var propertyInfo = typeof(Person).GetProperty("Name");

            var target = new TsProperty(propertyInfo);

            Assert.Same(propertyInfo, target.MemberInfo);
        }
示例#10
0
        public static string MemberTypeFormatter(TsProperty prop, string typeName)
        {
            /*if(typeName.Contains("IObjectId")) {
             *  typeName = "string";
             * }*/

            return(DefaultMemberTypeFormatter(prop, typeName));
        }
示例#11
0
        public void WhenCreateFromPropertyInfo_TypeIsSetToValueProvidedByTypeResolver()
        {
            this.SetupTypeResolverFor <ClassWithProperty>();
            var type = this.SetupTypeResolverFor <int>();
            var propertyConfiguration = this.SetupConfigurationForMember <ClassWithProperty>("Property");
            var propertyInfo          = typeof(ClassWithProperty).GetTypeInfo().DeclaredProperties.Where(o => o.Name == "Property").Single();

            var property = TsProperty.CreateFrom(propertyInfo, _typeResolverMock.Object, _configurationProviderMock.Object);

            Assert.Same(type, property.Type);
        }
示例#12
0
 /// <summary>
 /// Resolves references in the property.
 /// </summary>
 /// <param name="property"></param>
 public override void VisitProperty(TsProperty property, TsPropertyVisibilityFormatter propertyVisibilityFormatter)
 {
     property.PropertyType = this.ResolveType(property.PropertyType, propertyVisibilityFormatter);
     if (property.GenericArguments != null)
     {
         for (int i = 0; i < property.GenericArguments.Count; i++)
         {
             property.GenericArguments[i] = this.ResolveType(property.GenericArguments[i], propertyVisibilityFormatter);
         }
     }
 }
示例#13
0
 /// <summary>
 /// Resolves references in the property.
 /// </summary>
 /// <param name="property"></param>
 public override void VisitProperty(TsProperty property)
 {
     property.PropertyType = this.ResolveType(property.PropertyType);
     if (property.GenericArguments != null)
     {
         for (int i = 0; i < property.GenericArguments.Count; i++)
         {
             property.GenericArguments[i] = this.ResolveType(property.GenericArguments[i]);
         }
     }
 }
示例#14
0
        /// <summary>
        /// Gets property name in the TypeScript
        /// </summary>
        /// <param name="property">The property to get name of</param>
        /// <returns>name of the property</returns>
        public string GetPropertyName(TsProperty property)
        {
            var name = _memberFormatter(property);

            if (property.IsOptional)
            {
                name += "?";
            }

            return(name);
        }
示例#15
0
            public override string GetTypescriptType(TsProperty property)
            {
                if (property.Type == "System.Guid")
                {
                    return("Guid");
                }

                if (property.Type == "System.Guid[]")
                {
                    return("Guid[]");
                }

                return(base.GetTypescriptType(property));
            }
示例#16
0
        public string DefaultMemberTypeFormatter(TsProperty tsProperty, string memberTypeName)
        {
            var asCollection = tsProperty.PropertyType as TsCollection;
            var isCollection = asCollection != null;

            var result = new StringBuilder(memberTypeName);

            if (isCollection)
            {
                for (var i = 0; i < asCollection.Dimension; ++i)
                {
                    result.Append("[]");
                }
            }

            return(result.ToString());
        }
示例#17
0
        private void AddDocCommentForCustomJsonConverter(ScriptBuilder sb, TsProperty property)
        {
            var declaringType = property.MemberInfo.DeclaringType;
            var propertyName  = property.MemberInfo.Name;

            var iface         = declaringType.GetInterfaces().FirstOrDefault(ii => ii.Name == "I" + declaringType.Name);
            var ifaceProperty = iface?.GetProperty(propertyName);

            var attributes = new List <Attribute>();

            if (ifaceProperty != null)
            {
                attributes.AddRange(ifaceProperty.GetCustomAttributes());
            }
            attributes.AddRange(property.MemberInfo.GetCustomAttributes());

            var isRequest          = declaringType.Name.Contains("Request");
            var nonGenericTypeName = ClientTypesExporter.RemoveGeneric.Replace(declaringType.Name, "$1");

            if (ClientTypesExporter.InterfaceRegex.IsMatch(nonGenericTypeName))
            {
                nonGenericTypeName = nonGenericTypeName.Substring(1);
            }

            if (isRequest && this._typeInfoProvider.RequestParameters.ContainsKey(nonGenericTypeName))
            {
                var rp   = this._typeInfoProvider.RequestParameters[nonGenericTypeName];
                var prop = rp.GetProperty(propertyName);
                if (prop != null)
                {
                    sb.AppendLineIndented("@request_parameter()");
                }
            }

            var converter = attributes.FirstOrDefault(a => a.TypeId.ToString() == "Nest.Json.JsonConverterAttribute");

            if (converter != null)
            {
                if (GetConverter(converter, out var type))
                {
                    return;
                }
                sb.AppendLineIndented($"@prop_serializer(\"{type.Name}\")");
            }
        }
示例#18
0
        private static string FormatMemberType(TsProperty tsProperty, string memberTypeName)
        {
            if (memberTypeName == nameof(Error))
            {
                return(nameof(MainError));
            }

            var asCollection = tsProperty.PropertyType as TsCollection;
            var isCollection = asCollection != null;

            if (typeof(IIsADictionary).IsAssignableFrom(tsProperty.PropertyType.Type))
            {
                return(memberTypeName);
            }

            return(memberTypeName.StartsWith("Dictionary<")
                                ? memberTypeName
                                : memberTypeName + (isCollection ? string.Concat(Enumerable.Repeat("[]", asCollection.Dimension)) : ""));
        }
示例#19
0
 public void AppendConstantModuleDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType)
 {
 }
示例#20
0
 public void AppendPropertyDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType)
 {
 }
示例#21
0
 public void AppendConstantModuleDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType)
 {
     AppendMemberDoc(sb, property.MemberInfo);
 }
示例#22
0
        /// <summary>
        /// Gets property type in the TypeScript
        /// </summary>
        /// <param name="property">The property to get type of</param>
        /// <returns>type of the property</returns>
        public string GetPropertyType(TsProperty property)
        {
            var fullyQualifiedTypeName = GetFullyQualifiedTypeName(property.PropertyType);

            return(_memberTypeFormatter(property, fullyQualifiedTypeName));
        }
示例#23
0
        /// <summary>
        /// Gets property constant value in TypeScript format
        /// </summary>
        /// <param name="property">The property to get constant value of</param>
        /// <returns>constant value of the property</returns>
        public string GetPropertyConstantValue(TsProperty property)
        {
            var quote = property.PropertyType.Type == typeof(string) ? "\"" : "";

            return(quote + property.ConstantValue.ToString() + quote);
        }
示例#24
0
 string GetJsonAssignment(TsProperty prop)
 {
     return(GetAssignmentExpression(prop.TsType, "json." + CamelCase(prop.Name)));
 }
 public void AppendPropertyDoc(IndentedStringBuilder sb, TsProperty property, string propertyName, string propertyType)
 {
 }
示例#26
0
 public string DefaultMemberFormatter(TsProperty identifier)
 {
     return(identifier.Name);
 }
示例#27
0
 /// <summary>
 /// When overridden in a derived class, it can examine or modify the property model.
 /// </summary>
 /// <param name="property">The model property being visited.</param>
 public virtual void VisitProperty(TsProperty property)
 {
 }
 private bool IsCollection(TsProperty property)
 => typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType.Type) &&
 property.PropertyType.Type != typeof(string);
 public void AppendConstantModuleDoc(IndentedStringBuilder sb, TsProperty property, string propertyName, string propertyType)
 {
 }
示例#30
0
        public static string FormatPropertyType(TsProperty property, string memberTypeName)
        {
            Type propertyType = property.MemberInfo.DeclaringType.GetProperty(property.MemberInfo.Name).PropertyType;

            if (propertyType == typeof(IDictionary <string, string>) || propertyType == typeof(Dictionary <string, string>))
            {
                return("{ [key: string]: string; }");
            }

            if (propertyType.IsGenericType && (propertyType.GetGenericTypeDefinition() == typeof(IDictionary <,>) || propertyType.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                var valueType   = propertyType.GetGenericArguments()[1];
                var valueString = propertyType.FullName;
                if (valueType.Name.StartsWith(nameof(IEnumerable), StringComparison.Ordinal))
                {
                    var v2 = valueType.GetGenericArguments()[0];
                    if (v2 == typeof(string))
                    {
                        valueString = "string[]";
                    }
                    else
                    {
                        valueString = v2.FullName + "[]";
                    }
                }
                return($"{{ [key: string]: {valueString}; }}");
            }

            if (propertyType == typeof(Guid))
            {
                return("string");
            }

            if (propertyType == typeof(Stream))
            {
                return("any");
            }

            if (propertyType.Name.EndsWith("[]", StringComparison.Ordinal))
            {
                return(memberTypeName + "[]");
            }

            if (propertyType.Name.StartsWith(nameof(IEnumerable), StringComparison.Ordinal))
            {
                return(memberTypeName + "[]");
            }

            if (propertyType.Name.StartsWith(nameof(IList), StringComparison.Ordinal))
            {
                return(memberTypeName + "[]");
            }

            if (propertyType.Name.StartsWith(nameof(HashSet <object>), StringComparison.Ordinal))
            {
                return(memberTypeName + "[]");
            }

            if (propertyType.Name.StartsWith(nameof(ICollection), StringComparison.Ordinal))
            {
                return(memberTypeName + "[]");
            }

            return(memberTypeName);
        }