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

View File

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

View File

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

View File

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

View File

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