92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
using Core.Dto.Yaml;
|
|
using Core.Exceptions;
|
|
using Core.SpecConfig;
|
|
using Core.Yaml;
|
|
|
|
namespace Generator.Mappers;
|
|
|
|
public static class OpenApiYamlMapper
|
|
{
|
|
public static OpenApiYaml ToModel(this OpenApiYamlDto dto, string loc)
|
|
{
|
|
ISet<string> models = new HashSet<string>();
|
|
if (dto.Components is not null && dto.Components.TryGetValue("schemas", out var component))
|
|
{
|
|
models = component.Keys.ToHashSet();
|
|
}
|
|
|
|
return new OpenApiYaml(loc)
|
|
{
|
|
Info = dto.Info,
|
|
Openapi = dto.Openapi,
|
|
Tags = dto.Tags,
|
|
Models = models,
|
|
IgnoredModels = new HashSet<string>(),
|
|
};
|
|
}
|
|
|
|
public static Dictionary<string, YamlConfig> Map(this SpecConfigDto dto)
|
|
{
|
|
var result = new Dictionary<string, YamlConfig>();
|
|
|
|
foreach (var config in dto.Packages)
|
|
{
|
|
var yaml = new YamlConfig
|
|
{
|
|
Priority = config.Priority,
|
|
ModelSuffix = dto.ModelSuffix,
|
|
JavaGroup = dto.JavaGroup,
|
|
NugetPackage = config.DotnetPackage,
|
|
NpmPackage = config.JavascriptPackage,
|
|
KeepModels = config.KeepModels,
|
|
Type = ConvertSpecType(config.Type),
|
|
PackageTypes = ConvertPackagesTypes(dto.PackageTypes)
|
|
};
|
|
result.Add(config.Name, yaml);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static PackageTypes ConvertPackagesTypes(Dictionary<string, List<string>> dto)
|
|
{
|
|
var res = new PackageTypes();
|
|
foreach (var d in dto)
|
|
{
|
|
var language = d.Key.ToLower() switch
|
|
{
|
|
"dotnet" => Language.Dotnet,
|
|
"java" => Language.Java,
|
|
"javascript" => Language.Javascript,
|
|
_ => throw new ConfigException($"Currently supported languages are dotnet, java and javascript. {d.Key} is not one of them. Fix it or consequences")
|
|
};
|
|
|
|
var generationTypes = d.Value.Select(t => t.ToLower() switch
|
|
{
|
|
"server" => GenerationType.Server,
|
|
"common" => GenerationType.Common,
|
|
"client" => GenerationType.Client,
|
|
_ => throw new ConfigException($"Currently supported generation types are server, common and client. {t} is not one of them. Fix it or consequences")
|
|
}).ToList();
|
|
|
|
generationTypes.ForEach(gt =>
|
|
{
|
|
if (!res.TryAdd(language, gt))
|
|
res.Add(language, [gt]);
|
|
});
|
|
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
private static SpecType ConvertSpecType(string type)
|
|
{
|
|
return type.ToLower() switch
|
|
{
|
|
"api" => SpecType.Api,
|
|
"model" => SpecType.Model,
|
|
_ => throw new ConfigException($"Currently supported spec types are api and model. {type} is not one of them. Fix it or consequences")
|
|
};
|
|
}
|
|
} |