[Feature] Add value sanitizers to [ObservableProperty]
还没有人认领这个 Issue。
评估
调研方向
从 [ObservableProperty] 源代码生成器及其生成的 setter 开始,并以此 issue 中的示例作为行为基准。首先确定 sanitizer API、支持的操作以及 setter/getter 的作用范围;当达成一致的设计支持所请求的 sanitizer 用例,并且生成的属性能正确应用这些操作时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- C#
- 星标
- 3.8k
- 派生
- 401
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
CommunityToolkit/dotnet 的其他 Issue
-
RelayCommand<T>.CanExecute(null) returns false for value types without a predicate可能已有人在做 @apachezy 于 83 天前认领。 未关闭bug :bug:
难度 2/5 1-3 小时 新手友好度 84/100
CommunityToolkit/dotnet#1206 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
难度 1/5 1 小时以内 新手友好度 68/100
CommunityToolkit/dotnet#648 ·
-
bug :bug:
难度 3/5 1-2 天 新手友好度 68/100
CommunityToolkit/dotnet#1215 ·
-
Add segmented buffering and sequence reads to `ArrayPoolBufferWriter<T>`可能已有人在做 @paulomorgado 于 21 天前认领。 未关闭feature request :mailbox_with_mail:
难度 5/5 一周以上 新手友好度 45/100
CommunityToolkit/dotnet#1214 ·
查看 CommunityToolkit/dotnet 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
dette technique priorité: basse zone: service
难度 2/5 1-3 小时 新手友好度 68/100
damiensabau/nexus-lobby#25 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
sebastienros/jint#4282 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 76/100
pyrevitlabs/pyRevit#3792 ·
维护者通常 1 天内回复