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); }
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)) : "")); }
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()); }
public void WhenInitializedAndIsAnnotatedWithIgnoreAttribute_IsIgnoresIsSetToTrue() { var propertyInfo = typeof(Product).GetProperty("Ignored"); var target = new TsProperty(propertyInfo); Assert.True(target.IsIgnored); }
public void WhenInitializedAndHasCustomNameInAttribute_CustomNameIsUsed() { var propertyInfo = typeof(CustomClassName).GetProperty("CustomPorperty"); var target = new TsProperty(propertyInfo); Assert.Equal("MyProperty", target.Name); }
public void WhenInitialized_PropertyTypeIsSet() { var propertyInfo = typeof(Person).GetProperty("Name"); var target = new TsProperty(propertyInfo); Assert.Equal(propertyInfo.PropertyType, target.PropertyType.Type); }
public void WhenInitialized_IsOptionalIsFalse() { var propertyInfo = typeof(Person).GetProperty("Name"); var target = new TsProperty(propertyInfo); Assert.False(target.IsOptional); }
public void WhenInitialized_PropertyInfoIsSet() { var propertyInfo = typeof(Person).GetProperty("Name"); var target = new TsProperty(propertyInfo); Assert.Same(propertyInfo, target.MemberInfo); }
public static string MemberTypeFormatter(TsProperty prop, string typeName) { /*if(typeName.Contains("IObjectId")) { * typeName = "string"; * }*/ return(DefaultMemberTypeFormatter(prop, typeName)); }
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); }
/// <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); } } }
/// <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]); } } }
/// <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); }
public override string GetTypescriptType(TsProperty property) { if (property.Type == "System.Guid") { return("Guid"); } if (property.Type == "System.Guid[]") { return("Guid[]"); } return(base.GetTypescriptType(property)); }
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()); }
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}\")"); } }
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)) : "")); }
public void AppendConstantModuleDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType) { }
public void AppendPropertyDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType) { }
public void AppendConstantModuleDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType) { AppendMemberDoc(sb, property.MemberInfo); }
/// <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)); }
/// <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); }
string GetJsonAssignment(TsProperty prop) { return(GetAssignmentExpression(prop.TsType, "json." + CamelCase(prop.Name))); }
public void AppendPropertyDoc(IndentedStringBuilder sb, TsProperty property, string propertyName, string propertyType) { }
public string DefaultMemberFormatter(TsProperty identifier) { return(identifier.Name); }
/// <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) { }
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); }