Skip to content

Commit

Permalink
fix: Allow non-const ModelFieldValue to be specified with const.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Jul 27, 2023
1 parent 0105570 commit 9ad23b2
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 19 deletions.
82 changes: 82 additions & 0 deletions packages/katana_model/lib/src/model_field_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,41 @@ class ModelTimestamp extends ModelFieldValue<DateTime> {
/// これをサーバーに渡すことでサーバー側のタイムスタンプがデータとして保存されます。
const factory ModelTimestamp([DateTime? value]) = _ModelTimestamp;

/// Define the field as a timestamp.
///
/// The current time is returned in the same way as [DateTime.now].
///
/// By passing this to the server, the timestamp on the server side is stored as data.
///
/// フィールドをタイムスタンプとして定義します。
///
/// [DateTime.now]と同じ様に現在時刻が返されます。
///
/// これをサーバーに渡すことでサーバー側のタイムスタンプがデータとして保存されます。
const factory ModelTimestamp.now() = _ModelTimestampWithNow;

/// Define the field as a timestamp.
///
/// Returns the date and time specified in [year], [month], [day], [hour], [minute], [second], [millisecond], or [microsecond].
///
/// By passing this to the server, the timestamp on the server side is stored as data.
///
/// フィールドをタイムスタンプとして定義します。
///
/// [year][month][day][hour][minute][second][millisecond][microsecond]で指定した日時が返されます。
///
/// これをサーバーに渡すことでサーバー側のタイムスタンプがデータとして保存されます。
const factory ModelTimestamp.dateTime(
int year, [
int? month,
int? day,
int? hour,
int? minute,
int? second,
int? millisecond,
int? microsecond,
]) = _ModelTimestampWithDateTime;

/// Used to disguise the retrieval of data from the server.
///
/// Use for testing purposes.
Expand Down Expand Up @@ -849,6 +884,53 @@ class ModelTimestamp extends ModelFieldValue<DateTime> {
int get hashCode => _value.hashCode;
}

@immutable
class _ModelTimestampWithNow extends _ModelTimestamp {
const _ModelTimestampWithNow() : super();

@override
DateTime? get _value {
return DateTime.now();
}
}

@immutable
class _ModelTimestampWithDateTime extends _ModelTimestamp {
const _ModelTimestampWithDateTime(
this.year, [
this.month,
this.day,
this.hour,
this.minute,
this.second,
this.millisecond,
this.microsecond,
]) : super();

final int year;
final int? month;
final int? day;
final int? hour;
final int? minute;
final int? second;
final int? millisecond;
final int? microsecond;

@override
DateTime? get _value {
return DateTime(
year,
month ?? 1,
day ?? 1,
hour ?? 0,
minute ?? 0,
second ?? 0,
millisecond ?? 0,
microsecond ?? 0,
);
}
}

@immutable
class _ModelTimestamp extends ModelTimestamp
with ModelFieldValueAsMapMixin<DateTime> {
Expand Down
29 changes: 21 additions & 8 deletions packages/katana_model/lib/src/model_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,8 @@ class ModelRefBase<T> extends ModelFieldValue<T?> {
/// また[adapter]を指定してモデルアダプターを設定することが可能です。
///
/// ミュータブルクラスでかつ[DocumentBase]のインターフェースを備えているため[ModelRefBase]を実装し、[ModelRefMixin]をミックスインすることで[DocumentBase]で置き換えることが可能です。
factory ModelRefBase.fromPath(String path, [ModelAdapter? adapter]) {
return _ModelRefBase(
DocumentModelQuery(
path.trimQuery().trimString("/"),
adapter: adapter,
),
);
}
const factory ModelRefBase.fromPath(String path, [ModelAdapter? adapter]) =
_ModelRefBaseWithPath<T>;

/// Convert from [json] map to [ModelRefBase].
///
Expand Down Expand Up @@ -113,6 +107,25 @@ class ModelRefBase<T> extends ModelFieldValue<T?> {
}
}

@immutable
class _ModelRefBaseWithPath<T> extends _ModelRefBase<T> {
const _ModelRefBaseWithPath(this.path, [this.adapter])
: super(
const DocumentModelQuery(""),
);

final String path;
final ModelAdapter? adapter;

@override
DocumentModelQuery get modelQuery {
return DocumentModelQuery(
path.trimQuery().trimString("/"),
adapter: adapter,
);
}
}

/// [ModelFieldValueConverter] to enable automatic conversion of [ModelRefBase] as [ModelFieldValue].
///
/// [ModelRefBase][ModelFieldValue]として自動変換できるようにするための[ModelFieldValueConverter]
Expand Down
57 changes: 48 additions & 9 deletions packages/katana_model/lib/src/model_uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class ModelImageUri extends ModelFieldValue<Uri> {
/// ベースの値を[value]として与えます。空の[Uri]が定義されます。
///
/// 必ず画像URIとしての定義を行いたい場合は[ModelImageUri]を利用してください。
factory ModelImageUri.parse([String? value]) {
return ModelImageUri(value == null ? null : Uri.parse(value));
}
const factory ModelImageUri.parse([String? value]) = _ModelImageUriWithUri;

/// Used to disguise the retrieval of data from the server.
///
Expand Down Expand Up @@ -102,6 +100,21 @@ class ModelImageUri extends ModelFieldValue<Uri> {
int get hashCode => _value.hashCode;
}

@immutable
class _ModelImageUriWithUri extends _ModelImageUri {
const _ModelImageUriWithUri([this._path]) : super();

final String? _path;

@override
Uri? get _value {
if (_path == null) {
return null;
}
return Uri.parse(_path!);
}
}

@immutable
class _ModelImageUri extends ModelImageUri with ModelFieldValueAsMapMixin<Uri> {
const _ModelImageUri([
Expand Down Expand Up @@ -282,9 +295,7 @@ class ModelVideoUri extends ModelFieldValue<Uri> {
/// ベースの値を[value]として与えます。空の[Uri]が定義されます。
///
/// 必ず映像URIとしての定義を行いたい場合は[ModelVideoUri]を利用してください。
factory ModelVideoUri.parse([String? value]) {
return ModelVideoUri(value == null ? null : Uri.parse(value));
}
const factory ModelVideoUri.parse([String? value]) = _ModelVideoUriWithUri;

/// Used to disguise the retrieval of data from the server.
///
Expand Down Expand Up @@ -347,6 +358,21 @@ class ModelVideoUri extends ModelFieldValue<Uri> {
int get hashCode => _value.hashCode;
}

@immutable
class _ModelVideoUriWithUri extends _ModelVideoUri {
const _ModelVideoUriWithUri([this._path]) : super();

final String? _path;

@override
Uri? get _value {
if (_path == null) {
return null;
}
return Uri.parse(_path!);
}
}

@immutable
class _ModelVideoUri extends ModelVideoUri with ModelFieldValueAsMapMixin<Uri> {
const _ModelVideoUri([
Expand Down Expand Up @@ -527,9 +553,7 @@ class ModelUri extends ModelFieldValue<Uri> {
/// ベースの値を[value]として与えます。空の[Uri]が定義されます。
///
/// 必ずURIとしての定義を行いたい場合は[ModelUri]を利用してください。
factory ModelUri.parse([String? value]) {
return ModelUri(value == null ? null : Uri.parse(value));
}
const factory ModelUri.parse([String? value]) = _ModelUriWithUri;

/// Used to disguise the retrieval of data from the server.
///
Expand Down Expand Up @@ -591,6 +615,21 @@ class ModelUri extends ModelFieldValue<Uri> {
int get hashCode => _value.hashCode;
}

@immutable
class _ModelUriWithUri extends _ModelUri {
const _ModelUriWithUri([this._path]) : super();

final String? _path;

@override
Uri? get _value {
if (_path == null) {
return null;
}
return Uri.parse(_path!);
}
}

@immutable
class _ModelUri extends ModelUri with ModelFieldValueAsMapMixin<Uri> {
const _ModelUri([
Expand Down
2 changes: 1 addition & 1 deletion packages/katana_model_firestore/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ packages:
path: "../katana_model"
relative: true
source: path
version: "2.7.0"
version: "2.7.1"
lints:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/katana_model_local/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ packages:
path: "../katana_model"
relative: true
source: path
version: "2.7.0"
version: "2.7.1"
lints:
dependency: transitive
description:
Expand Down

0 comments on commit 9ad23b2

Please sign in to comment.