diff --git a/src/TypeGen/TypeGen.Core/Generator/Generator.cs b/src/TypeGen/TypeGen.Core/Generator/Generator.cs index 35b56a69..be2aae81 100644 --- a/src/TypeGen/TypeGen.Core/Generator/Generator.cs +++ b/src/TypeGen/TypeGen.Core/Generator/Generator.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; @@ -588,21 +587,60 @@ private string GetClassPropertyText(Type type, MemberInfo memberInfo) isOptional = true; } + var defaultValue = GetClassPropertyDefaultValue(memberInfo, typeName); + + if (defaultValue == null) + return _templateService.FillClassPropertyTemplate(modifiers, name, typeName, typeUnions, isOptional, tsDoc); + + var enumType = GetEnumType(memberInfo); + if (enumType != null) + defaultValue = GetEnumMemberReference(enumType, defaultValue); + + return _templateService.FillClassPropertyTemplate(modifiers, name, typeName, typeUnions, isOptional, tsDoc, defaultValue); + } + + private Type GetEnumType(MemberInfo memberInfo) + { + Type enumType = null; + + // Check if the memberInfo is a property or field + Type memberType = null; + if (memberInfo is PropertyInfo propertyInfo) + { + memberType = propertyInfo.PropertyType; + } + else if (memberInfo is FieldInfo fieldInfo) + { + memberType = fieldInfo.FieldType; + } + + // Check if the member type is an enum + if (memberType != null && memberType.IsEnum) + { + enumType = memberType; + return enumType; + } + + return null; + } + + private string GetClassPropertyDefaultValue(MemberInfo memberInfo, string typeName) + { // try to get default value from TsDefaultValueAttribute var defaultValueAttribute = _metadataReaderFactory.GetInstance().GetAttribute(memberInfo); if (defaultValueAttribute != null) - return _templateService.FillClassPropertyTemplate(modifiers, name, typeName, typeUnions, isOptional, tsDoc, defaultValueAttribute.DefaultValue); + return defaultValueAttribute.DefaultValue; // try to get default value from the member's default value string valueText = _tsContentGenerator.GetMemberValueText(memberInfo); if (!string.IsNullOrWhiteSpace(valueText)) - return _templateService.FillClassPropertyTemplate(modifiers, name, typeName, typeUnions, isOptional, tsDoc, valueText); + return valueText; // try to get default value from Options.DefaultValuesForTypes if (Options.DefaultValuesForTypes.Any() && Options.DefaultValuesForTypes.ContainsKey(typeName)) - return _templateService.FillClassPropertyTemplate(modifiers, name, typeName, typeUnions, isOptional, tsDoc, Options.DefaultValuesForTypes[typeName]); + return Options.DefaultValuesForTypes[typeName]; - return _templateService.FillClassPropertyTemplate(modifiers, name, typeName, typeUnions, isOptional, tsDoc); + return null; } private static void ThrowMemberTypeIsBlacklisted(MemberInfo memberInfo) @@ -725,6 +763,30 @@ private string GetInterfacePropertiesText(Type type) return RemoveLastLineEnding(propertiesText); } + /// + /// Converts an enumvalue that is a number to a reference to the enumvalue + /// if EnumStringInitializers are enabled for that enum. + /// + /// The type of the enum + /// The number value in the form of a string + /// A string representation of the reference to the enumvalue in the form {EnumType}.{StringRepresentation} + private string GetEnumMemberReference(Type enumType, string enumValue) + { + var stringInitializersAttribute = _metadataReaderFactory.GetInstance().GetAttribute(enumType); + if ((Options.EnumStringInitializers && (stringInitializersAttribute == null || stringInitializersAttribute.Enabled)) || + (stringInitializersAttribute != null && stringInitializersAttribute.Enabled)) + { + int index; + if(int.TryParse(enumValue, out index)) + { + var values = Enum.GetValues(enumType); + var value = values.GetValue(index).ToString(); + return $"{enumType.Name}.{value}"; + } + } + return enumValue; + } + /// /// Gets TypeScript enum member definition source code ///