feat: Lint using very_good_analysis
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
PapaTutuWawa 2023-01-14 14:53:33 +01:00
parent 9bdff5ae01
commit 2a469e10e8
6 changed files with 86 additions and 63 deletions

View File

@ -1,4 +1,15 @@
include: package:flutter_lints/flutter.yaml include: package:very_good_analysis/analysis_options.yaml
linter:
rules:
public_member_api_docs: false
lines_longer_than_80_chars: false
use_setters_to_change_properties: false
avoid_positional_boolean_parameters: false
avoid_bool_literals_in_conditional_expressions: false
# Additional information about this file can be found at analyzer:
# https://dart.dev/guides/language/analysis-options exclude:
- "**/*.g.dart"
- "**/*.freezed.dart"
- "test/"
- "integration_test/"

View File

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1649676176, "lastModified": 1667395993,
"narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=", "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678", "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -17,16 +17,16 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1650034868, "lastModified": 1667610399,
"narHash": "sha256-OAaf5BdWKGXTXvYnbvJuoQjSWnVKgt1cIOChF0MFt2o=", "narHash": "sha256-XZd0f4ZWAY0QOoUSdiNWj/eFiKb4B9CJPtl9uO9SYY4=",
"owner": "PapaTutuWawa", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "13a5646d450052b88067cab37b198f8a2737e431", "rev": "1dd8696f96db47156e1424a49578fe7dd4ce99a4",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "PapaTutuWawa", "owner": "NixOS",
"ref": "nixos-unstable-flutter-2.13.0-0.1.pre", "ref": "nixpkgs-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }

View File

@ -1,14 +1,17 @@
{ {
description = "moxlib"; description = "moxlib";
inputs = { inputs = {
nixpkgs.url = "github:PapaTutuWawa/nixpkgs/nixos-unstable-flutter-2.13.0-0.1.pre"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
}; };
outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: let outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system; inherit system;
config.android_sdk.accept_license = true; config = {
android_sdk.accept_license = true;
allowUnfree = true;
};
}; };
android = pkgs.androidenv.composeAndroidPackages { android = pkgs.androidenv.composeAndroidPackages {
# TODO: Find a way to pin these # TODO: Find a way to pin these
@ -26,11 +29,11 @@
useGoogleAPIs = false; useGoogleAPIs = false;
useGoogleTVAddOns = false; useGoogleTVAddOns = false;
}; };
pinnedJDK = pkgs.jdk11; pinnedJDK = pkgs.jdk;
in { in {
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
buildInputs = with pkgs; [ buildInputs = with pkgs; [
flutterPackages.beta pinnedJDK android.platform-tools flutterPackages.dart-beta # Flutter flutter pinnedJDK android.platform-tools dart # Flutter
gitlint jq # Code hygiene gitlint jq # Code hygiene
ripgrep # General utilities ripgrep # General utilities
]; ];

View File

@ -1,39 +1,43 @@
import "dart:async"; import 'dart:async';
import 'package:logging/logging.dart';
import "package:synchronized/synchronized.dart"; import 'package:meta/meta.dart';
import "package:uuid/uuid.dart"; import 'package:synchronized/synchronized.dart';
import "package:logging/logging.dart"; import 'package:uuid/uuid.dart';
import "package:meta/meta.dart";
/// Interface to allow arbitrary data to be sent as long as it can be /// Interface to allow arbitrary data to be sent as long as it can be
/// JSON serialized/deserialized. /// JSON serialized/deserialized.
class JsonImplementation { class JsonImplementation {
JsonImplementation(); JsonImplementation();
Map<String, dynamic> toJson() => {}; // ignore: avoid_unused_constructor_parameters
factory JsonImplementation.fromJson(Map<String, dynamic> json) { factory JsonImplementation.fromJson(Map<String, dynamic> json) {
return JsonImplementation(); return JsonImplementation();
} }
Map<String, dynamic> toJson() => {};
} }
/// Wrapper class that adds an ID to the data packet to be sent. /// Wrapper class that adds an ID to the data packet to be sent.
class DataWrapper<T extends JsonImplementation> { class DataWrapper<T extends JsonImplementation> {
final String id;
final T data;
const DataWrapper( const DataWrapper(
this.id, this.id,
this.data this.data,
); );
/// The id of the data packet.
final String id;
/// The actual data.
final T data;
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
"id": id, 'id': id,
"data": data.toJson() 'data': data.toJson()
}; };
static DataWrapper fromJson<T extends JsonImplementation>(Map<String, dynamic> json) => DataWrapper<T>( static DataWrapper fromJson<T extends JsonImplementation>(Map<String, dynamic> json) => DataWrapper<T>(
json["id"]! as String, json['id']! as String,
json["data"]! as T json['data']! as T,
); );
DataWrapper reply(T newData) => DataWrapper(id, newData); DataWrapper reply(T newData) => DataWrapper(id, newData);
@ -49,16 +53,23 @@ abstract class AwaitableDataSender<
S extends JsonImplementation, S extends JsonImplementation,
R extends JsonImplementation R extends JsonImplementation
> { > {
final Lock _lock;
final Map<String, Completer<R>> _awaitables;
final Uuid _uuid;
final Logger _log;
@mustCallSuper @mustCallSuper
AwaitableDataSender() : _awaitables = {}, _uuid = const Uuid(), _lock = Lock(), _log = Logger("AwaitableDataSender"); AwaitableDataSender();
/// A mapping of ID to completer for pending requests.
final Map<String, Completer<R>> _awaitables = {};
/// Critical section for accessing [AwaitableDataSender._awaitables].
final Lock _lock = Lock();
/// A UUID object for generating UUIDs.
final Uuid _uuid = const Uuid();
/// A logger.
final Logger _log = Logger('AwaitableDataSender');
@visibleForTesting @visibleForTesting
Map<String, Completer> getAwaitables() => _awaitables; Map<String, Completer<R>> getAwaitables() => _awaitables;
/// Called after an awaitable has been added. /// Called after an awaitable has been added.
@visibleForTesting @visibleForTesting
@ -74,10 +85,10 @@ abstract class AwaitableDataSender<
Future<R?> sendData(S data, { bool awaitable = true, @visibleForTesting String? id }) async { Future<R?> sendData(S data, { bool awaitable = true, @visibleForTesting String? id }) async {
// ignore: no_leading_underscores_for_local_identifiers // ignore: no_leading_underscores_for_local_identifiers
final _id = id ?? _uuid.v4(); final _id = id ?? _uuid.v4();
Future<R?> future = Future.value(null); var future = Future<R?>.value();
_log.fine("sendData: Waiting to acquire lock..."); _log.fine('sendData: Waiting to acquire lock...');
await _lock.synchronized(() async { await _lock.synchronized(() async {
_log.fine("sendData: Done"); _log.fine('sendData: Done');
if (awaitable) { if (awaitable) {
_awaitables[_id] = Completer(); _awaitables[_id] = Completer();
onAdd(); onAdd();
@ -86,15 +97,15 @@ abstract class AwaitableDataSender<
await sendDataImpl( await sendDataImpl(
DataWrapper<S>( DataWrapper<S>(
_id, _id,
data data,
) ),
); );
if (awaitable) { if (awaitable) {
future = _awaitables[_id]!.future; future = _awaitables[_id]!.future;
} }
_log.fine("sendData: Releasing lock..."); _log.fine('sendData: Releasing lock...');
}); });
return future; return future;
@ -103,24 +114,21 @@ abstract class AwaitableDataSender<
/// Should be called when a [DataWrapper] has been received. Will resolve /// Should be called when a [DataWrapper] has been received. Will resolve
/// the promise received from [sendData]. /// the promise received from [sendData].
Future<bool> onData(DataWrapper<R> data) async { Future<bool> onData(DataWrapper<R> data) async {
bool found = false; _log.fine('onData: Waiting to acquire lock...');
Completer? completer; final completer = await _lock.synchronized(() async {
_log.fine("onData: Waiting to acquire lock..."); _log.fine('onData: Done');
await _lock.synchronized(() async { final c = _awaitables[data.id];
_log.fine("onData: Done"); if (c != null) {
completer = _awaitables[data.id];
if (completer != null) {
_awaitables.remove(data.id); _awaitables.remove(data.id);
found = true; return c;
} }
_log.fine("onData: Releasing lock"); _log.fine('onData: Releasing lock');
return null;
}); });
if (found) { completer?.complete(data.data);
completer!.complete(data.data);
}
return found; return completer != null;
} }
} }

View File

@ -1,5 +1,5 @@
library moxlib; library moxlib;
export "awaitabledatasender.dart"; export 'awaitabledatasender.dart';
export "lists.dart"; export 'lists.dart';
export "math.dart"; export 'math.dart';

View File

@ -9,10 +9,11 @@ environment:
dependencies: dependencies:
logging: ^1.0.2 logging: ^1.0.2
meta: ^1.7.0
synchronized: ^3.0.0 synchronized: ^3.0.0
uuid: ^3.0.5 uuid: ^3.0.5
meta: ^1.7.0
dev_dependencies: dev_dependencies:
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0
test: ^1.20.1 test: ^1.20.1
very_good_analysis: ^3.0.1