[Feature] Add value sanitizers to [ObservableProperty]
まだ誰も着手していません。
評価
調査の方向性
[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 ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
CommunityToolkit/dotnet のほかの issue
-
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 ·
-
feature request :mailbox_with_mail:
難易度 5/5 1週間以上 初心者へのやさしさ 45/100
CommunityToolkit/dotnet#1214 ·
CommunityToolkit/dotnet の issue をすべて見る
似ている issue
-
:watch: Not Triaged dotnet-target-version
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
copilot documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 2 日以内に返信
-
untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
dotnet/dotnet-api-docs#13124 ·
メンテナーはふだん 1 日以内に返信
-
agentic-workflows
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
BHoM/MidasCivil_Toolkit#441 ·