Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[Feature] Add value sanitizers to [ObservableProperty]

オープン
#1,067 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
csharp
領域
tooling

調査の方向性

[ObservableProperty] ソースジェネレーターと生成された setter から始め、この issue の例を動作上の基準として使用します。まず sanitizer API、サポートされる操作、setter/getter の適用範囲を確定します。合意した設計が要求された sanitizer のケースをサポートし、生成されたプロパティがそれらを正しく適用できれば完了です。

索引モデルが issue の本文から書いたものです。

説明

feature request :mailbox_with_mail:
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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

CommunityToolkit/dotnet のほかの issue

CommunityToolkit/dotnet の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。