パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.0.0

global_assert

定数に関するビルド時アサーションを簡単に実行できるマイクロライブラリで、誤設定されたアプリが実行時ではなくビルド時に失敗するようにします。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
9570
いいね数
7
Pub ポイント
160

パッケージをインストール

flutter pub add global_assert

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

GlobalAssert は、定数に対するビルド時アサーションを可能にする軽量なDartパッケージであり、実行時ではなくビルドプロセス中に構成エラーを早期に検出するのに役立ちます。重要な値をコンパイル時に検証することで、アプリの信頼性を向上させ、本番環境での静黙的な失敗のリスクを低減します。静的データに対する不変条件を強制するのに最適で、すべての主要プラットフォームにおけるFlutterプロジェクトにシームレスに統合できます。

用途

  • 設定定数の検証
  • 誤った設定によるランタイムエラーの防止
  • ビルド中に不変条件のチェックを強制する
  • テストの信頼性の向上
  • 早期に無効な状態を検出する
  • 安全な初期化の確保

主な機能

  • ビルド時アサーションの評価
  • 定数検証用の簡単な API
  • 実行時オーバーヘッドなし
  • クロスプラットフォーム対応
  • Flutter ビルドシステムとの統合
  • 最小の依存関係フットプリント

適している対象

  • ビルド時セキュリティを最優先する開発者
  • 品質管理に静的解析を使用するチーム
  • 厳格な構成要件を備えたプロジェクト
  • 堅牢な初期化チェックを必要とする Flutter アプリ
  • 早期の失敗検出を必要とする CI/CD パイプライン

注意事項

  • コンパイル時定数でのみ動作します
  • 単純なブール条件に限定
  • 実行時検証を置き換えない
  • ビルド時実行についての理解が必要です
  • ビルド時間をわずかに増加させる可能性があります
  • 動的またはユーザー提供のデータには適していません

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

A micro-library for asserting at build time.

pub package Build Status Coverage

It's a common practice to configure a Dart app using const (build-time) values. There are benefits to this approach, such as enabling tree shaking of unused code.

This library makes it easy to do build-time assertions about such constants so that misconfigured apps fail at build-time instead of failing at runtime.

Usage

Use this library to validate your app's const values at build time. Anything can be annotated with @Assert.

//
// This app will fail compilation with '--define=MY_VAL=foo'
//

@Assert(
  myBuildTimeVal == 'val1' || myBuildTimeVal == 'val2',
  'myBuildTimeVal must be one of "val1" or "val2"',
)
const myBuildTimeVal = String.fromEnvironment('MY_VAL');

void main() {
  print('myBuildTimeVal is "$myBuildTimeVal"');
}

FAQ

🤔 How does this even work?!

The Dart compiler invokes annotation constructors at compile-time. This micro-library takes advantage of that to "trick" the compiler into allowing build-time asserts that can be placed (almost) anywhere. Special thanks to Kyle Turney for the inspiration behind this approach.

Why not allow an assert without a message?

Unfortunately, Dart doesn't treat an assert with a null message the same as an assert without any message at all. This means that we would need two constructors for @Assert to enable both messageless asserts and asserts with messages. Instead, we made the design choice when creating this micro-library that @Assert should require a non-null message. This enforces more meaningful compilation failure messages.

Will this always be needed?

We expect that when Dart supports metaprogramming, this library will be deprecated.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。