using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace XFEExtension.NetCore.AutoConfig.Tests; [AutoLoadProfile(false)] public partial class MigratingJsonProfile : XFEProfile { [ProfileProperty("DisplayName")] private string displayName = "default"; [ProfileProperty] private int revision; public MigratingJsonProfile() => DefaultProfileOperationMode = ProfileOperationMode.Json; protected override int ProfileSchemaVersion => 2; protected override void ConfigureMigrations(ProfileMigrationBuilder migrations) => migrations .RenameProperty(0, "LegacyName", "DisplayName") .Transform(1, static context => { var root = JsonNode.Parse(context.Content)?.AsObject() ?? throw new ProfileMigrationException("JSON 根节点无效"); root["InstanceRevision"] = 7; return root.ToJsonString(context.JsonOptions); }); protected override ProfileValidationResult ValidateProfile() => string.IsNullOrWhiteSpace(InstanceDisplayName) ? ProfileValidationResult.Failure("DisplayName 不能为空") : ProfileValidationResult.Success; } [AutoLoadProfile(false)] public partial class VersionedXfeProfile : XFEProfile { [ProfileProperty] private string value = "default"; protected override int ProfileSchemaVersion => 1; protected override void ConfigureMigrations(ProfileMigrationBuilder migrations) => migrations.NoOp(0); } [AutoLoadProfile(false)] public partial class VersionedXmlProfile : XFEProfile { [ProfileProperty] private string value = "default"; public VersionedXmlProfile() => DefaultProfileOperationMode = ProfileOperationMode.Xml; protected override int ProfileSchemaVersion => 1; protected override void ConfigureMigrations(ProfileMigrationBuilder migrations) => migrations.NoOp(0); } public enum ProfileTheme { Light, Dark } [AutoLoadProfile(false)] public partial class JsonOptionsProfile : XFEProfile { [ProfileProperty] private ProfileTheme theme; public JsonOptionsProfile() { DefaultProfileOperationMode = ProfileOperationMode.Json; JsonOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; JsonOptions.WriteIndented = true; JsonOptions.AllowTrailingCommas = true; JsonOptions.ReadCommentHandling = JsonCommentHandling.Skip; JsonOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); } } [AutoLoadProfile(false)] public partial class TenantProfile : XFEProfile { [ProfileProperty] private string name = "default"; [ProfileProperty] private ProfileList values = []; public TenantProfile() => DefaultProfileOperationMode = ProfileOperationMode.Json; } [AutoLoadProfile(false)] public partial class AutoBindingProfile : XFEProfile { [ProfileProperty] private ProfileList numbers = []; [ProfileProperty] private ProfileDictionary lookup = []; }