From 2a469e10e819c9b8866b94805c908c86b25f0486 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sat, 14 Jan 2023 14:53:33 +0100 Subject: [PATCH] feat: Lint using very_good_analysis --- analysis_options.yaml | 17 +++++-- flake.lock | 18 +++---- flake.nix | 11 +++-- lib/awaitabledatasender.dart | 94 +++++++++++++++++++----------------- lib/moxlib.dart | 6 +-- pubspec.yaml | 3 +- 6 files changed, 86 insertions(+), 63 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index a5744c1..6b2506f 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -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 -# https://dart.dev/guides/language/analysis-options +analyzer: + exclude: + - "**/*.g.dart" + - "**/*.freezed.dart" + - "test/" + - "integration_test/" diff --git a/flake.lock b/flake.lock index b1054f5..842552d 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "flake-utils": { "locked": { - "lastModified": 1649676176, - "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=", + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", "owner": "numtide", "repo": "flake-utils", - "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", "type": "github" }, "original": { @@ -17,16 +17,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1650034868, - "narHash": "sha256-OAaf5BdWKGXTXvYnbvJuoQjSWnVKgt1cIOChF0MFt2o=", - "owner": "PapaTutuWawa", + "lastModified": 1667610399, + "narHash": "sha256-XZd0f4ZWAY0QOoUSdiNWj/eFiKb4B9CJPtl9uO9SYY4=", + "owner": "NixOS", "repo": "nixpkgs", - "rev": "13a5646d450052b88067cab37b198f8a2737e431", + "rev": "1dd8696f96db47156e1424a49578fe7dd4ce99a4", "type": "github" }, "original": { - "owner": "PapaTutuWawa", - "ref": "nixos-unstable-flutter-2.13.0-0.1.pre", + "owner": "NixOS", + "ref": "nixpkgs-unstable", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index ef5f494..47cee57 100644 --- a/flake.nix +++ b/flake.nix @@ -1,14 +1,17 @@ { description = "moxlib"; 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"; }; outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; - config.android_sdk.accept_license = true; + config = { + android_sdk.accept_license = true; + allowUnfree = true; + }; }; android = pkgs.androidenv.composeAndroidPackages { # TODO: Find a way to pin these @@ -26,11 +29,11 @@ useGoogleAPIs = false; useGoogleTVAddOns = false; }; - pinnedJDK = pkgs.jdk11; + pinnedJDK = pkgs.jdk; in { devShell = pkgs.mkShell { buildInputs = with pkgs; [ - flutterPackages.beta pinnedJDK android.platform-tools flutterPackages.dart-beta # Flutter + flutter pinnedJDK android.platform-tools dart # Flutter gitlint jq # Code hygiene ripgrep # General utilities ]; diff --git a/lib/awaitabledatasender.dart b/lib/awaitabledatasender.dart index 341ea80..256d32e 100644 --- a/lib/awaitabledatasender.dart +++ b/lib/awaitabledatasender.dart @@ -1,39 +1,43 @@ -import "dart:async"; - -import "package:synchronized/synchronized.dart"; -import "package:uuid/uuid.dart"; -import "package:logging/logging.dart"; -import "package:meta/meta.dart"; +import 'dart:async'; +import 'package:logging/logging.dart'; +import 'package:meta/meta.dart'; +import 'package:synchronized/synchronized.dart'; +import 'package:uuid/uuid.dart'; /// Interface to allow arbitrary data to be sent as long as it can be /// JSON serialized/deserialized. class JsonImplementation { JsonImplementation(); - Map toJson() => {}; + // ignore: avoid_unused_constructor_parameters factory JsonImplementation.fromJson(Map json) { return JsonImplementation(); } + + Map toJson() => {}; } /// Wrapper class that adds an ID to the data packet to be sent. class DataWrapper { - final String id; - final T data; - const DataWrapper( this.id, - this.data + this.data, ); + /// The id of the data packet. + final String id; + + /// The actual data. + final T data; + Map toJson() => { - "id": id, - "data": data.toJson() + 'id': id, + 'data': data.toJson() }; static DataWrapper fromJson(Map json) => DataWrapper( - json["id"]! as String, - json["data"]! as T + json['id']! as String, + json['data']! as T, ); DataWrapper reply(T newData) => DataWrapper(id, newData); @@ -49,16 +53,23 @@ abstract class AwaitableDataSender< S extends JsonImplementation, R extends JsonImplementation > { - final Lock _lock; - final Map> _awaitables; - final Uuid _uuid; - final Logger _log; - @mustCallSuper - AwaitableDataSender() : _awaitables = {}, _uuid = const Uuid(), _lock = Lock(), _log = Logger("AwaitableDataSender"); + AwaitableDataSender(); + /// A mapping of ID to completer for pending requests. + final Map> _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 - Map getAwaitables() => _awaitables; + Map> getAwaitables() => _awaitables; /// Called after an awaitable has been added. @visibleForTesting @@ -74,10 +85,10 @@ abstract class AwaitableDataSender< Future sendData(S data, { bool awaitable = true, @visibleForTesting String? id }) async { // ignore: no_leading_underscores_for_local_identifiers final _id = id ?? _uuid.v4(); - Future future = Future.value(null); - _log.fine("sendData: Waiting to acquire lock..."); + var future = Future.value(); + _log.fine('sendData: Waiting to acquire lock...'); await _lock.synchronized(() async { - _log.fine("sendData: Done"); + _log.fine('sendData: Done'); if (awaitable) { _awaitables[_id] = Completer(); onAdd(); @@ -86,15 +97,15 @@ abstract class AwaitableDataSender< await sendDataImpl( DataWrapper( _id, - data - ) + data, + ), ); if (awaitable) { future = _awaitables[_id]!.future; } - _log.fine("sendData: Releasing lock..."); + _log.fine('sendData: Releasing lock...'); }); return future; @@ -103,24 +114,21 @@ abstract class AwaitableDataSender< /// Should be called when a [DataWrapper] has been received. Will resolve /// the promise received from [sendData]. Future onData(DataWrapper data) async { - bool found = false; - Completer? completer; - _log.fine("onData: Waiting to acquire lock..."); - await _lock.synchronized(() async { - _log.fine("onData: Done"); - completer = _awaitables[data.id]; - if (completer != null) { - _awaitables.remove(data.id); - found = true; - } + _log.fine('onData: Waiting to acquire lock...'); + final completer = await _lock.synchronized(() async { + _log.fine('onData: Done'); + final c = _awaitables[data.id]; + if (c != null) { + _awaitables.remove(data.id); + 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; } } diff --git a/lib/moxlib.dart b/lib/moxlib.dart index 1b8fee9..48fa1d2 100644 --- a/lib/moxlib.dart +++ b/lib/moxlib.dart @@ -1,5 +1,5 @@ library moxlib; -export "awaitabledatasender.dart"; -export "lists.dart"; -export "math.dart"; +export 'awaitabledatasender.dart'; +export 'lists.dart'; +export 'math.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 615dcc5..5944a58 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,10 +9,11 @@ environment: dependencies: logging: ^1.0.2 + meta: ^1.7.0 synchronized: ^3.0.0 uuid: ^3.0.5 - meta: ^1.7.0 dev_dependencies: flutter_lints: ^2.0.0 test: ^1.20.1 + very_good_analysis: ^3.0.1