[Feature] Add value sanitizers to [ObservableProperty]
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
Bắt đầu từ trình tạo mã nguồn [ObservableProperty] và setter được tạo của nó, sử dụng các ví dụ trong issue này làm cơ sở hành vi. Trước tiên, hãy xác định API của sanitizer, các thao tác được hỗ trợ và phạm vi của setter/getter; được xem là hoàn tất khi thiết kế đã thống nhất hỗ trợ các trường hợp sanitizer được yêu cầu và các property được tạo áp dụng chúng chính xác.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Overview
In many of my properties I must Clamp, Round, Max or Min the setter value.
This auto defeats the [ObservableProperty] which make me implement the raw property.
Example:
public double TimeoutSeconds
{
get;
set => SetProperty(ref field, Math.Round(
Math.Clamp(value, 0, 10000),
2,
MidpointRounding.AwayFromZero)
);
} = 5000;
Example of what I would like:
[ObservableProperty]
[ObservablePropertySetterClamp(0, 10000)]
[ObservablePropertySetterRound(2, MidpointRounding.AwayFromZero)]
public partial double TimeoutSeconds { get; set; }
API breakdown
Not sure how to make the API but I provide a sample of generated code:
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.4.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial global::System.Double TimeoutSeconds
{
get => field;
set
{
// The new sanitizer magic here
value = Math.Clamp(value, 0, 10000);
value = Math.Round(value, 2, MidpointRounding.AwayFromZero);
// End
// Current implementation, unchanged.
if (!global::System.Collections.Generic.EqualityComparer<global::System.Double>.Default.Equals(field, value))
{
OnLastExecutedDateTimeChanging(value);
OnLastExecutedDateTimeChanging(default, value);
field = value;
OnLastExecutedDateTimeChanged(value);
OnLastExecutedDateTimeChanged(default, value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.LastExecutedDateTime);
}
}
}
Usage example
[ObservableProperty]
[ObservablePropertySetterClamp(0, 10000)]
[ObservablePropertySetterRound(2, MidpointRounding.AwayFromZero)]
public partial double TimeoutSeconds { get; set; }
[ObservableProperty]
[ObservablePropertySetterMin(0)]
public partial int LapCount { get; set; }
[ObservableProperty]
[ObservablePropertySetterStringTrim]
[ObservablePropertySetterStringUpperCase]
public partial string Country { get; set; }
[ObservableProperty]
[ObservablePropertySetterCustom(MyStaticSanitizerCallback)]
public partial string MySecretString { get; set; }
Breaking change?
No
Alternatives
The team can find a better way and/or names to do such, right now I don't see another alternative than using atributes.
Additional context
- Also implement other sanitizers useful to other uses and types
- If doing for setter we can also do for getter side?
Help us help you
Yes, I'd like to be assigned to work on this item
- Ngôn ngữ chính
- C#
- Star
- 3.8k
- Fork
- 401
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của CommunityToolkit/dotnet
-
bug :bug:
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
CommunityToolkit/dotnet#1206 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
CommunityToolkit/dotnet#648 ·
-
bug :bug:
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
CommunityToolkit/dotnet#1215 ·
-
feature request :mailbox_with_mail:
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 45/100
CommunityToolkit/dotnet#1214 ·
Tất cả issue của CommunityToolkit/dotnet
Issue tương tự
-
:watch: Not Triaged dotnet-target-version
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
copilot documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 2 ngày
-
untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
dotnet/dotnet-api-docs#13124 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
agentic-workflows
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
type:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
BHoM/MidasCivil_Toolkit#441 ·