private void CreateTypeBuilderConfig() { var typeMappings = new Dictionary <string, TsTypeReference>(); void AddTypeMappingsFromAssembly(Assembly asm) { foreach (var attribute in TypeUtils.GetAssemblyCustomAttributesData(asm).Where(a => a.AttributeType.Name == Constants.DefineTypeScriptTypeForExternalTypeAttributeName && a.ConstructorArguments.Count == 2 && a.ConstructorArguments[1].Value is string)) { string key; if (attribute.ConstructorArguments[0].Value is Type type) { key = TypeUtils.GetFullNameWithGenericArguments(type); } else if (attribute.ConstructorArguments[0].Value is string s) { key = s; } else { continue; } typeMappings[key] = TsTypeReference.Simple((string)attribute.ConstructorArguments[1].Value); } } foreach (var assembly in _generatorContext.Assemblies) { AddTypeMappingsFromAssembly(assembly); } // override mappings from config foreach (var mapping in _config.TypeMappings) { var reference = TsTypeReference.Simple(mapping.Value); typeMappings[mapping.Key] = reference; } var mappings = ImmutableDictionary.CreateRange(typeMappings); _typeBuilderConfig = new TypeBuilderConfig(mappings, _config.CustomTypeScriptIgnoreAttributeFullName, _config.WrapConstEnumsInTemplateStrings); }
private static TsTypeReference BuildTsTypeReference(Type type, TypeBuilderConfig config, string currentTsNamespace, bool returnTheMainTypeEvenIfItHasADerivedTypesUnion) { var isOptional = false; var underlyingNullableType = GetUnderlyingNullableType(type); if (underlyingNullableType != null) { type = underlyingNullableType; isOptional = true; } if (type.IsGenericParameter) { return(TsTypeReference.Simple(type.Name, isOptional)); } var typeFullName = TypeUtils.GetFullNameWithGenericArguments(type); if (typeFullName != null && config.TypeMappings.TryGetValue(typeFullName, out var mappedType)) { return(TsTypeReference.Wrap(mappedType, isOptional)); } var typeScriptTypeAttribute = GetTypeScriptTypeAttribute(type); if (typeScriptTypeAttribute != null) { if (typeScriptTypeAttribute.ConstructorArguments[0].Value is string typeString) { return(TsTypeReference.Simple(typeString)); } else if (typeScriptTypeAttribute.ConstructorArguments[0].Value is Type replaceWithType) { type = replaceWithType; } } if ( TypeUtils.Is <byte>(type) || TypeUtils.Is <sbyte>(type) || TypeUtils.Is <int>(type) || TypeUtils.Is <uint>(type) || TypeUtils.Is <long>(type) || TypeUtils.Is <ulong>(type) || TypeUtils.Is <short>(type) || TypeUtils.Is <ushort>(type) || TypeUtils.Is <double>(type) || TypeUtils.Is <decimal>(type) || TypeUtils.Is <float>(type) ) { return(TsTypeReference.Simple("number", isOptional)); } if (TypeUtils.Is <string>(type)) { return(TsTypeReference.Simple("string", isOptional)); } if (TypeUtils.Is <bool>(type)) { return(TsTypeReference.Simple("boolean", isOptional)); } var dictionaryTypes = GetDictionaryUnderlyingTypes(type); if (dictionaryTypes != null) { return(TsTypeReference.Dictionary(BuildTsTypeReference(dictionaryTypes.Item1, config, currentTsNamespace, false), BuildTsTypeReference(dictionaryTypes.Item2, config, currentTsNamespace, false))); } var enumerableUnderlyingType = GetEnumerableUnderlyingType(type); if (enumerableUnderlyingType != null) { return(TsTypeReference.Array(BuildTsTypeReference(enumerableUnderlyingType, config, currentTsNamespace, false))); } if (typeFullName != null && (type.IsClass || type.IsInterface || type.IsValueType)) { var namespaceName = GetTypescriptNamespace(type); if (!string.IsNullOrEmpty(namespaceName)) { var derivedTypesUnionName = returnTheMainTypeEvenIfItHasADerivedTypesUnion ? null : GetDerivedTypesUnionName(type); var typeName = TypeUtils.GetNameWithoutGenericArity(type); var prefix = ""; var suffix = ""; if (type.IsEnum && config.WrapConstEnumsInTemplateStrings) { prefix = "`${"; suffix = "}`"; } var result = default(TsTypeReference); if (namespaceName == currentTsNamespace) { result = TsTypeReference.Simple(prefix + (derivedTypesUnionName ?? typeName) + suffix, isOptional); } else { result = TsTypeReference.Simple(prefix + namespaceName + "." + (derivedTypesUnionName ?? typeName) + suffix, isOptional); } if (result != null) { if (type.GenericTypeArguments.Length > 0) { var typeArgs = type.GenericTypeArguments.Select(t => BuildTsTypeReference(t, config, currentTsNamespace, false)); result = TsTypeReference.Generic(result, typeArgs); } return(result); } } } return(TsTypeReference.Simple("unknown", isOptional)); }