Reescrita dos geradores: biblioteca IGenerator, minimal API e novo frontend
- GeradoresService: cada tipo passa a ser um IGenerator (NIF, NIPC, NISS, CC, passaporte, IBAN, NIB, cartões, referência MB, pessoa, telefone, código postal, email, matrícula, EAN, UUID) registado em GeneratorRegistry; remove DAL/EF - GeradoresWS: minimal APIs em /api/v1 com OpenAPI + Scalar e health check - UnitTest: testes de ida e volta, valores conhecidos, detecção e API - geradoresfe: UI construída a partir dos metadados da API, validador livre, exportação CSV/JSON/TXT, nginx com API_UPSTREAM configurável - Docker, docker-compose e Jenkinsfile actualizados Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
50
GeradoresService/Generators/Outros/UuidGenerator.cs
Normal file
50
GeradoresService/Generators/Outros/UuidGenerator.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using GeradoresService.Core;
|
||||
|
||||
namespace GeradoresService.Generators.Outros;
|
||||
|
||||
public sealed class UuidGenerator : GeneratorBase
|
||||
{
|
||||
public override string Id => "uuid";
|
||||
public override string Name => "UUID / GUID";
|
||||
public override string Category => Categories.Outros;
|
||||
public override string Description => "Identificadores únicos UUID v4 (aleatório) ou v7 (ordenado por tempo).";
|
||||
public override bool HasChecksum => false;
|
||||
|
||||
public override IReadOnlyList<GeneratorOption> Options =>
|
||||
[
|
||||
Option("versao", "Versão", "v4", ("v4", "v4 — aleatório"), ("v7", "v7 — ordenado por tempo")),
|
||||
Option("formato", "Formato", "minusculas",
|
||||
("minusculas", "minúsculas com hífens"), ("maiusculas", "MAIÚSCULAS com hífens"), ("compacto", "sem hífens")),
|
||||
];
|
||||
|
||||
public override GeneratedItem Generate(GeneratorContext context)
|
||||
{
|
||||
var guid = context.Option(Options[0]) == "v7" ? Guid.CreateVersion7() : NewV4(context.Random);
|
||||
var text = context.Option(Options[1]) switch
|
||||
{
|
||||
"maiusculas" => guid.ToString("D").ToUpperInvariant(),
|
||||
"compacto" => guid.ToString("N"),
|
||||
_ => guid.ToString("D"),
|
||||
};
|
||||
return new GeneratedItem(text);
|
||||
}
|
||||
|
||||
/// <summary>UUID v4 a partir de <paramref name="random"/> (permite resultados reprodutíveis com seed).</summary>
|
||||
private static Guid NewV4(Random random)
|
||||
{
|
||||
Span<byte> bytes = stackalloc byte[16];
|
||||
random.NextBytes(bytes);
|
||||
// Versão 4 (nibble alto do byte 7 no layout do System.Guid) e variante RFC 9562.
|
||||
bytes[7] = (byte)((bytes[7] & 0x0F) | 0x40);
|
||||
bytes[8] = (byte)((bytes[8] & 0x3F) | 0x80);
|
||||
return new Guid(bytes);
|
||||
}
|
||||
|
||||
public override ValidationResult Validate(string value)
|
||||
{
|
||||
var v = value.Trim().Trim('{', '}');
|
||||
if (v.Length is not (32 or 36) || !Guid.TryParse(v, out var guid))
|
||||
return ValidationResult.WrongFormat("Não é um UUID válido.");
|
||||
return ValidationResult.Valid(guid.ToString("D"), $"UUID válido (versão {guid.Version}).", [$"Versão: {guid.Version}", $"Variante: {guid.Variant}"]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user