public void TranslateReturnsInputIfNoSettings() { var input = "MyNamespace.Test"; var actual = NamespaceTranslator.Translate(input, new List <NamespaceSettings>()); Assert.That(actual, Is.EqualTo(input)); }
public TypescriptInterface Convert(Type type) { if (!type.IsClass && !type.IsInterface && !type.IsStruct()) { throw new ArgumentException($"Type '{type.FullName}' is neither a class, struct nor an interface"); } var effectiveType = type; if (type.IsGenericType) { effectiveType = type.GetGenericTypeDefinition(); } var typescriptProperties = effectiveType.GetProperties() .Where(ShouldIncludeProperty) .Select(propertyConverter.Convert) .ToList(); var dependencies = typescriptProperties .SelectMany(x => x.Dependencies) .ToList(); var translatedNamespace = NamespaceTranslator.Translate(effectiveType.Namespace, namespaceSettings); var typeName = type.IsGenericType ? GetGenericTypeName(type) : type.Name; return(new TypescriptInterface( effectiveType.Namespace, translatedNamespace, typeName, // TODO: Apply transforms typescriptProperties, dependencies, settings.Modifiers)); }
public void TranslateOnlyReplacesLeadingPart() { var input = "MyNamespace.MyNamespace.Test"; var translation = "Models"; var settings = new NamespaceSettings("MyNamespace") { Translation = translation }; var actual = NamespaceTranslator.Translate(input, new List <NamespaceSettings> { settings }); Assert.That(actual, Is.EqualTo("Models.MyNamespace.Test")); }
public void TranslateReturnsTranslatedForExactMatch() { var input = "MyNamespace.Test"; var translation = "Models"; var settings = new NamespaceSettings(input) { Translation = translation }; var actual = NamespaceTranslator.Translate(input, new List <NamespaceSettings> { settings }); Assert.That(actual, Is.EqualTo(translation)); }
public TypescriptEnum Convert(Type type) { if (!type.IsEnum) { throw new ArgumentException($"Type '{type.FullName}' is not an enum"); } var translatedNamespace = settings.EnumsIntoSeparateFile ? "Enums" : NamespaceTranslator.Translate(type.Namespace, namespaceSettings); return(new TypescriptEnum( type.Namespace, translatedNamespace, type.Name, // TODO: Apply transforms settings.Modifiers, Enum.GetNames(type).ToList())); }
public void TranslateReturnsNullForNull() { var actual = NamespaceTranslator.Translate(null, new List <NamespaceSettings>()); Assert.That(actual, Is.Null); }
public TypeDeterminerResult Determine(Type propertyType) { var typeAttribute = propertyType.GetCustomAttribute <TypescriptTypeAttribute>(); if (typeAttribute != null) { return(new TypeDeterminerResult(typeAttribute.TypeString, typeAttribute.Dependencies)); } var matchingTypeConverter = settings.TypeConverters.FirstOrDefault(x => x.IsMatchingType(propertyType)); if (matchingTypeConverter != null) { return(new TypeDeterminerResult(matchingTypeConverter.Convert(propertyType), new List <Type>())); } if (propertyType.IsNullable()) { var underlyingType = Nullable.GetUnderlyingType(propertyType); var underlyingTypeResult = Determine(underlyingType); return(new TypeDeterminerResult( $"{underlyingTypeResult.FormattedType} | null", underlyingTypeResult.Dependencies)); } if (propertyType.IsGenericParameter) { return(new TypeDeterminerResult(propertyType.Name, new List <Type>())); } if (IsPrimitiveType(propertyType)) { return(new TypeDeterminerResult(PrimitiveTypes[propertyType], new List <Type>())); } if (propertyType.IsEnum && enumSettings.EnumsIntoSeparateFile) { return(new TypeDeterminerResult("Enums." + propertyType.Name, new List <Type> { propertyType })); } if (propertyType.IsDictionary(out var keyType, out var valueType)) { var keyTypeResult = Determine(keyType); var valueTypeResult = Determine(valueType); return(new TypeDeterminerResult( $"{{ [key: {keyTypeResult.FormattedType}]: {valueTypeResult.FormattedType} }}", keyTypeResult.Dependencies.Concat(valueTypeResult.Dependencies).ToList() )); } if (propertyType.IsCollection(out var itemType)) { var itemTypeResult = Determine(itemType); var formattedCollectionType = itemTypeResult.FormattedType.Contains('|') ? $"({itemTypeResult.FormattedType})[]" : $"{itemTypeResult.FormattedType}[]"; return(new TypeDeterminerResult( formattedCollectionType, itemTypeResult.Dependencies )); } var translatedNamespace = NamespaceTranslator.Translate(propertyType.Namespace, namespaceSettings); if (propertyType.IsGenericType) { var genericResults = propertyType.GetGenericArguments() .Select(Determine) .ToList(); var generics = $"<{string.Join(",", genericResults.Select(x => x.FormattedType))}>"; return(new TypeDeterminerResult( $"{translatedNamespace}.{propertyType.Name.StripGenericTypeSuffix()}{generics}", genericResults.SelectMany(x => x.Dependencies).Concat(new [] { propertyType.GetGenericTypeDefinition() }).ToList() )); } return(new TypeDeterminerResult( $"{translatedNamespace}.{propertyType.Name}", new List <Type> { propertyType } )); }