Skip to content

Commit

Permalink
Remove useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Feb 21, 2024
1 parent 76038e6 commit 568dba5
Showing 1 changed file with 0 additions and 128 deletions.
128 changes: 0 additions & 128 deletions lib/src/utils/json.dart
Original file line number Diff line number Diff line change
@@ -1,132 +1,4 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:async/async.dart';
import 'package:deep_pick/deep_pick.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:http/http.dart';
import 'package:lichess_mobile/src/model/common/errors.dart';
import 'package:logging/logging.dart';
import 'package:result_extensions/result_extensions.dart';

typedef Mapper<T> = T Function(Map<String, dynamic>);

/// Reads a [T] object from a JSON object.
Result<T> readJsonObject<T>(
Map<String, dynamic> obj, {
required Mapper<T> mapper,
Logger? logger,
}) {
final result = Result(() => mapper(obj));
result.match(
onError: (error, st) {
logger?.severe('Could not read json object as $T: $error\n$st');
},
);
return result;
}

T readJsonObjectFromBytes<T>(
Uint8List bytes, {
required Mapper<T> mapper,
Logger? logger,
}) {
final json = jsonDecode(utf8.decode(bytes));
if (json is! Map<String, dynamic>) {
logger?.severe('Could not read json object as $T: expected an object.');
throw DataFormatException();
}
return mapper(json);
}

/// Reads a [T] object from a [Response] returning a JSON object.
Result<T> readJsonObjectFromResponse<T>(
Response response, {
required Mapper<T> mapper,
Logger? logger,
}) {
final json = jsonDecode(utf8.decode(response.bodyBytes));
if (json is! Map<String, dynamic>) {
logger?.severe('Could not read json object as $T: expected an object.');
throw DataFormatException();
}
return readJsonObject(json, mapper: mapper, logger: logger);
}

/// Reads a list of [T] objects from a JSON array.
Result<IList<T>> readJsonListOfObjects<T>(
List<dynamic> list, {
required Mapper<T> mapper,
Logger? logger,
}) {
final result = Result(() {
return IList(
list.map((e) {
if (e is! Map<String, dynamic>) {
logger?.severe('Could not read json object as $T');
throw DataFormatException();
}
return mapper(e);
}),
);
});
result.match(
onError: (error, st) {
logger?.severe('Could not read json as list of $T: $error\n$st');
},
);
return result;
}

/// Reads a list of [T] objects from a [Response] returning a JSON array.
Result<IList<T>> readJsonListOfObjectsFromResponse<T>(
Response response, {
required Mapper<T> mapper,
Logger? logger,
}) {
return Result(() {
final dynamic list = jsonDecode(utf8.decode(response.bodyBytes));
if (list is! List<dynamic>) {
logger?.severe('Received json is not a list');
throw DataFormatException();
}
return list;
}).flatMap(
(list) => readJsonListOfObjects(
list,
mapper: mapper,
logger: logger,
),
);
}

/// Reads a list of [T] objects from a newline-delimited json [Response].
Result<IList<T>> readNdJsonListFromResponse<T>(
Response response, {
required Mapper<T> mapper,
Logger? logger,
}) {
final result = Result(() {
final utf8Body = utf8.decode(response.bodyBytes);
final lines = utf8Body.split('\n');
return IList(
lines.where((e) => e.isNotEmpty && e != '\n').map((e) {
final json = jsonDecode(e);
if (json is! Map<String, dynamic>) {
logger
?.severe('Could not read json object as $T: expected an object.');
throw DataFormatException();
}
return mapper(json);
}),
);
});
result.match(
onError: (error, st) =>
logger?.severe('Could not read ndjson as list of $T: $error\n$st'),
);
return result;
}

extension TimeExtension on Pick {
/// Matches a DateTime from milliseconds since unix epoch.
Expand Down

0 comments on commit 568dba5

Please sign in to comment.