Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions src/TypeGen/TypeGen.Core/Generator/Generator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -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<TsDefaultValueAttribute>(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)
Expand Down Expand Up @@ -725,6 +763,30 @@ private string GetInterfacePropertiesText(Type type)
return RemoveLastLineEnding(propertiesText);
}

/// <summary>
/// Converts an enumvalue that is a number to a reference to the enumvalue
/// if EnumStringInitializers are enabled for that enum.
/// </summary>
/// <param name="enumType">The type of the enum</param>
/// <param name="enumValue">The number value in the form of a string</param>
/// <returns>A string representation of the reference to the enumvalue in the form {EnumType}.{StringRepresentation}</returns>
private string GetEnumMemberReference(Type enumType, string enumValue)
{
var stringInitializersAttribute = _metadataReaderFactory.GetInstance().GetAttribute<TsStringInitializersAttribute>(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;
}

/// <summary>
/// Gets TypeScript enum member definition source code
/// </summary>
Expand Down