fix: log levels for tryGet

This commit is contained in:
Lukas Lihotzki 2021-07-02 14:39:52 +02:00
parent 0b8e3f3071
commit c10a98ff35
5 changed files with 53 additions and 33 deletions

View File

@ -47,12 +47,12 @@ class RoomEncryptedContent {
RoomEncryptedContent.fromJson(Map<String, dynamic> json)
: algorithm = json.tryGet('algorithm') ?? '',
senderKey = json.tryGet('sender_key') ?? '',
deviceId = json.tryGet<String?>('device_id'),
sessionId = json.tryGet<String?>('session_id'),
ciphertextMegolm = json.silentTryGet('ciphertext'),
deviceId = json.tryGet('device_id', TryGet.optional),
sessionId = json.tryGet('session_id', TryGet.optional),
ciphertextMegolm = json.tryGet('ciphertext', TryGet.silent),
// filter out invalid/incomplete CiphertextInfos
ciphertextOlm = json
.silentTryGet<Map<String, dynamic>>('ciphertext')
.tryGet<Map<String, dynamic>>('ciphertext', TryGet.silent)
?.entries
.map((e) {
try {

View File

@ -36,8 +36,9 @@ class RoomEncryptionContent {
RoomEncryptionContent.fromJson(Map<String, dynamic> json)
: algorithm = json.tryGet('algorithm') ?? '',
rotationPeriodMs = json.tryGet('rotation_period_ms'),
rotationPeriodMsgs = json.tryGet('rotation_period_msgs');
rotationPeriodMs = json.tryGet('rotation_period_ms', TryGet.optional),
rotationPeriodMsgs =
json.tryGet('rotation_period_msgs', TryGet.optional);
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};

View File

@ -37,7 +37,7 @@ class RoomKeyRequestContent {
RoomKeyRequestContent.fromJson(Map<String, dynamic> json)
: body = ((x) => x != null ? RequestedKeyInfo.fromJson(x) : null)(
json.tryGet('body')),
json.tryGet('body', TryGet.optional)),
action = json.tryGet('action') ?? '',
requestingDeviceId = json.tryGet('requesting_device_id') ?? '',
requestId = json.tryGet('request_id') ?? '';

View File

@ -38,12 +38,11 @@ class SecretStorageKeyContent {
SecretStorageKeyContent();
SecretStorageKeyContent.fromJson(Map<String, dynamic> json)
: passphrase = json['passphrase'] is Map<String, dynamic>
? PassphraseInfo.fromJson(json['passphrase'])
: null,
iv = json.tryGet('iv'),
mac = json.tryGet('mac'),
algorithm = json.tryGet('algorithm');
: passphrase = ((x) => x != null ? PassphraseInfo.fromJson(x) : null)(
json.tryGet('passphrase', TryGet.optional)),
iv = json.tryGet('iv', TryGet.optional),
mac = json.tryGet('mac', TryGet.optional),
algorithm = json.tryGet('algorithm', TryGet.optional);
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
@ -73,7 +72,7 @@ class PassphraseInfo {
: algorithm = json.tryGet('algorithm'),
salt = json.tryGet('salt'),
iterations = json.tryGet('iterations'),
bits = json.tryGet('bits');
bits = json.tryGet('bits', TryGet.optional);
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};

View File

@ -25,33 +25,54 @@ import 'dart:core';
import 'logs.dart';
extension TryGetMapExtension on Map<String, dynamic> {
T? tryGet<T extends Object?>(String key) {
final Object? value = this[key];
if (value is! T) {
Logs().w(
'Expected "$T" in event content for the Key "$key" but got "${value.runtimeType}".');
return null;
}
return value;
}
abstract class TryGet {
void call(String key, Type expected, Type actual);
static const TryGet required = _RequiredLog();
static const TryGet optional = _OptionalLog();
/// Same as tryGet but without logging any warnings.
/// This is helpful if you have a field that can mean multiple things on purpose.
T? silentTryGet<T extends Object?>(String key) {
static const TryGet silent = _SilentLog();
}
class _RequiredLog implements TryGet {
const _RequiredLog();
@override
void call(String key, Type expected, Type actual) => Logs().w(
'Expected "$expected" in event content for the Key "$key" but got "$actual".');
}
class _OptionalLog implements TryGet {
const _OptionalLog();
@override
void call(String key, Type expected, Type actual) {
if (actual != Null) {
Logs().w(
'Expected "$expected" in event content for the Key "$key" but got "$actual".');
}
}
}
class _SilentLog implements TryGet {
const _SilentLog();
@override
void call(String key, Type expected, Type actual) {}
}
extension TryGetMapExtension on Map<String, dynamic> {
T? tryGet<T extends Object>(String key, [TryGet log = TryGet.required]) {
final Object? value = this[key];
if (value is! T) {
log(key, T, value.runtimeType);
return null;
}
return value;
}
List<T>? tryGetList<T>(String key) {
List<T>? tryGetList<T>(String key, [TryGet log = TryGet.required]) {
final Object? value = this[key];
if (value is! List) {
Logs().w(
'Expected "List<$T>" in event content for the key "$key" but got "${value.runtimeType}".',
StackTrace.current);
log(key, T, value.runtimeType);
return null;
}
try {
@ -64,11 +85,10 @@ extension TryGetMapExtension on Map<String, dynamic> {
}
}
Map<A, B>? tryGetMap<A, B>(String key) {
Map<A, B>? tryGetMap<A, B>(String key, [TryGet log = TryGet.required]) {
final Object? value = this[key];
if (value is! Map) {
Logs().w(
'Expected "Map<$A,$B>" in event content for the key "$key" but got "${value.runtimeType}".');
log(key, <A, B>{}.runtimeType, value.runtimeType);
return null;
}
try {