From beed516c891f97dc3e2ac39cea7bcac54971c18c Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Wed, 13 Oct 2021 16:10:18 +0300 Subject: [PATCH] style: use Object instead of dynamic --- lib/src/bindings/attr.dart | 2 +- lib/src/bindings/checkout.dart | 33 +++++++++++++++++++++----------- lib/src/bindings/patch.dart | 8 ++++---- lib/src/bindings/remote.dart | 6 +++--- lib/src/bindings/stash.dart | 8 ++++---- lib/src/blob.dart | 12 ++++++++++-- lib/src/config.dart | 8 ++++++-- lib/src/patch.dart | 35 +++++++++++++++++++--------------- lib/src/remote.dart | 2 +- lib/src/repository.dart | 2 +- test/blame_test.dart | 2 +- test/blob_test.dart | 4 ++-- test/patch_test.dart | 18 ++++++++--------- 13 files changed, 84 insertions(+), 56 deletions(-) diff --git a/lib/src/bindings/attr.dart b/lib/src/bindings/attr.dart index f1ede92..55b68ba 100644 --- a/lib/src/bindings/attr.dart +++ b/lib/src/bindings/attr.dart @@ -10,7 +10,7 @@ import 'libgit2_bindings.dart'; /// or a [String] value, if the attribute was set to an actual string. /// /// Throws a [LibGit2Error] if error occured. -dynamic getAttribute({ +Object? getAttribute({ required Pointer repoPointer, required int flags, required String path, diff --git a/lib/src/bindings/checkout.dart b/lib/src/bindings/checkout.dart index 5082a0f..856895a 100644 --- a/lib/src/bindings/checkout.dart +++ b/lib/src/bindings/checkout.dart @@ -28,12 +28,15 @@ void head({ final pathPointers = initOpts[1]; final strArray = initOpts[2]; - final error = libgit2.git_checkout_head(repoPointer, optsC); + final error = libgit2.git_checkout_head( + repoPointer, + optsC as Pointer, + ); - for (final p in pathPointers) { + for (final p in pathPointers as List) { calloc.free(p); } - calloc.free(strArray); + calloc.free(strArray as Pointer); calloc.free(optsC); if (error < 0) { @@ -59,12 +62,16 @@ void index({ final pathPointers = initOpts[1]; final strArray = initOpts[2]; - final error = libgit2.git_checkout_index(repoPointer, nullptr, optsC); + final error = libgit2.git_checkout_index( + repoPointer, + nullptr, + optsC as Pointer, + ); - for (final p in pathPointers) { + for (final p in pathPointers as List) { calloc.free(p); } - calloc.free(strArray); + calloc.free(strArray as Pointer); calloc.free(optsC); if (error < 0) { @@ -92,12 +99,16 @@ void tree({ final pathPointers = initOpts[1]; final strArray = initOpts[2]; - final error = libgit2.git_checkout_tree(repoPointer, treeishPointer, optsC); + final error = libgit2.git_checkout_tree( + repoPointer, + treeishPointer, + optsC as Pointer, + ); - for (final p in pathPointers) { + for (final p in pathPointers as List) { calloc.free(p); } - calloc.free(strArray); + calloc.free(strArray as Pointer); calloc.free(optsC); if (error < 0) { @@ -105,12 +116,12 @@ void tree({ } } -List initOptions({ +List initOptions({ required int strategy, String? directory, List? paths, }) { - final optsC = calloc(sizeOf()); + final optsC = calloc(); libgit2.git_checkout_options_init(optsC, GIT_CHECKOUT_OPTIONS_VERSION); optsC.ref.checkout_strategy = strategy; diff --git a/lib/src/bindings/patch.dart b/lib/src/bindings/patch.dart index c208a4c..2ecae83 100644 --- a/lib/src/bindings/patch.dart +++ b/lib/src/bindings/patch.dart @@ -10,7 +10,7 @@ import '../util.dart'; /// you must call `free()` on the patch when done. /// /// Throws a [LibGit2Error] if error occured. -Map fromBuffers({ +Map fromBuffers({ String? oldBuffer, String? oldAsPath, String? newBuffer, @@ -66,7 +66,7 @@ Map fromBuffers({ /// must call `free()` on the patch when done. /// /// Throws a [LibGit2Error] if error occured. -Map fromBlobs({ +Map fromBlobs({ Pointer? oldBlobPointer, String? oldAsPath, Pointer? newBlobPointer, @@ -114,7 +114,7 @@ Map fromBlobs({ /// call `free()` on the patch when done. /// /// Throws a [LibGit2Error] if error occured. -Map fromBlobAndBuffer({ +Map fromBlobAndBuffer({ Pointer? oldBlobPointer, String? oldAsPath, String? buffer, @@ -194,7 +194,7 @@ int numHunks(Pointer patch) => libgit2.git_patch_num_hunks(patch); /// Given a patch and a hunk index into the patch, this returns detailed information about that hunk. /// /// Throws a [LibGit2Error] if error occured. -Map hunk({ +Map hunk({ required Pointer patchPointer, required int hunkIndex, }) { diff --git a/lib/src/bindings/remote.dart b/lib/src/bindings/remote.dart index 171992b..53d47b4 100644 --- a/lib/src/bindings/remote.dart +++ b/lib/src/bindings/remote.dart @@ -374,7 +374,7 @@ void connect({ /// remains available after disconnecting. /// /// Throws a [LibGit2Error] if error occured. -List> lsRemotes(Pointer remote) { +List> lsRemotes(Pointer remote) { final out = calloc>>(); final size = calloc(); final error = libgit2.git_remote_ls(out, size, remote); @@ -384,10 +384,10 @@ List> lsRemotes(Pointer remote) { calloc.free(size); throw LibGit2Error(libgit2.git_error_last()); } else { - var result = >[]; + var result = >[]; for (var i = 0; i < size.value; i++) { - var remote = {}; + var remote = {}; Oid? loid; final bool local = out[0][i].ref.local == 1 ? true : false; diff --git a/lib/src/bindings/stash.dart b/lib/src/bindings/stash.dart index 9ab92b0..60aa05d 100644 --- a/lib/src/bindings/stash.dart +++ b/lib/src/bindings/stash.dart @@ -72,10 +72,10 @@ void apply({ final error = libgit2.git_stash_apply(repoPointer, index, options); - for (final p in pathPointers) { + for (final p in pathPointers as List) { calloc.free(p); } - calloc.free(strArray); + calloc.free(strArray as Pointer); calloc.free(optsC); calloc.free(options); @@ -127,10 +127,10 @@ void pop({ final error = libgit2.git_stash_pop(repoPointer, index, options); - for (final p in pathPointers) { + for (final p in pathPointers as List) { calloc.free(p); } - calloc.free(strArray); + calloc.free(strArray as Pointer); calloc.free(optsC); calloc.free(options); diff --git a/lib/src/blob.dart b/lib/src/blob.dart index d1a46a9..12c4200 100644 --- a/lib/src/blob.dart +++ b/lib/src/blob.dart @@ -97,7 +97,11 @@ class Blob { interhunkLines: interhunkLines, ); - return Patch(result['patch'], result['a'], result['b']); + return Patch( + result['patch'] as Pointer, + result['a'], + result['b'], + ); } /// Directly generate a [Patch] from the difference between the blob and a buffer. @@ -123,7 +127,11 @@ class Blob { interhunkLines: interhunkLines, ); - return Patch(result['patch'], result['a'], result['b']); + return Patch( + result['patch'] as Pointer, + result['a'], + result['b'], + ); } /// Releases memory allocated for blob object. diff --git a/lib/src/config.dart b/lib/src/config.dart index e8bc532..29024b5 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -99,7 +99,9 @@ class Config with IterableMixin { } /// Sets the [value] of config [variable]. - void operator []=(String variable, dynamic value) { + /// + /// Throws [ArgumentError] if provided [value] is not bool, int or String. + void operator []=(String variable, Object value) { if (value is bool) { bindings.setBool( configPointer: _configPointer, @@ -112,12 +114,14 @@ class Config with IterableMixin { variable: variable, value: value, ); - } else { + } else if (value is String) { bindings.setString( configPointer: _configPointer, variable: variable, value: value, ); + } else { + throw ArgumentError.value('$value must be either bool, int or String'); } } diff --git a/lib/src/patch.dart b/lib/src/patch.dart index ace149e..c4da45a 100644 --- a/lib/src/patch.dart +++ b/lib/src/patch.dart @@ -20,9 +20,9 @@ class Patch { /// Should be freed with `free()` to release allocated memory. /// /// Throws a [LibGit2Error] if error occured. - Patch.createFrom({ - required dynamic a, - required dynamic b, + Patch.create({ + required Object? a, + required Object? b, String? aPath, String? bPath, Set flags = const {GitDiff.normal}, @@ -32,12 +32,12 @@ class Patch { libgit2.git_libgit2_init(); final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); - var result = {}; + var result = {}; - if (a is Blob || a == null) { + if (a is Blob?) { if (b is Blob) { result = bindings.fromBlobs( - oldBlobPointer: a?.pointer, + oldBlobPointer: a?.pointer ?? nullptr, oldAsPath: aPath, newBlobPointer: b.pointer, newAsPath: bPath, @@ -45,7 +45,7 @@ class Patch { contextLines: contextLines, interhunkLines: interhunkLines, ); - } else if (b is String || b == null) { + } else if (b is String?) { result = bindings.fromBlobAndBuffer( oldBlobPointer: a?.pointer, oldAsPath: aPath, @@ -58,9 +58,9 @@ class Patch { } else { throw ArgumentError('Provided argument(s) is not Blob or String'); } - } else if ((a is String || a == null) && (b is String || b == null)) { + } else if ((a is String?) && (b is String?)) { result = bindings.fromBuffers( - oldBuffer: a, + oldBuffer: a as String?, oldAsPath: aPath, newBuffer: b, newAsPath: bPath, @@ -72,7 +72,7 @@ class Patch { throw ArgumentError('Provided argument(s) is not Blob or String'); } - _patchPointer = result['patch']; + _patchPointer = result['patch'] as Pointer; _aPointer = result['a']; _bPointer = result['b']; } @@ -90,8 +90,8 @@ class Patch { late final Pointer _patchPointer; - dynamic _aPointer; - dynamic _bPointer; + Pointer? _aPointer; + Pointer? _bPointer; /// Pointer to memory address for allocated patch object. Pointer get pointer => _patchPointer; @@ -134,7 +134,12 @@ class Patch { for (var i = 0; i < length; i++) { final hunk = bindings.hunk(patchPointer: _patchPointer, hunkIndex: i); - hunks.add(DiffHunk(_patchPointer, hunk['hunk'], hunk['linesN'], i)); + hunks.add(DiffHunk( + _patchPointer, + hunk['hunk'] as Pointer, + hunk['linesN'] as int, + i, + )); } return hunks; @@ -143,10 +148,10 @@ class Patch { /// Releases memory allocated for patch object. void free() { if (_aPointer != null) { - calloc.free(_aPointer); + calloc.free(_aPointer!); } if (_bPointer != null) { - calloc.free(_bPointer); + calloc.free(_bPointer!); } bindings.free(_patchPointer); } diff --git a/lib/src/remote.dart b/lib/src/remote.dart index 02fca03..b7f4e4f 100644 --- a/lib/src/remote.dart +++ b/lib/src/remote.dart @@ -194,7 +194,7 @@ class Remote { /// specified url. By default connection isn't done through proxy. /// /// Throws a [LibGit2Error] if error occured. - List> ls({ + List> ls({ String? proxy, Callbacks callbacks = const Callbacks(), }) { diff --git a/lib/src/repository.dart b/lib/src/repository.dart index e9ff5f2..2c543ba 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -1237,7 +1237,7 @@ class Repository { /// or a [String] value, if the attribute was set to an actual string. /// /// Throws a [LibGit2Error] if error occured. - dynamic getAttribute({ + Object? getAttribute({ required String path, required String name, Set flags = const {GitAttributeCheck.fileThenIndex}, diff --git a/test/blame_test.dart b/test/blame_test.dart index 782f72d..4d59267 100644 --- a/test/blame_test.dart +++ b/test/blame_test.dart @@ -8,7 +8,7 @@ void main() { late Directory tmpDir; late Signature sig1; late Signature sig2; - var hunks = >[]; + var hunks = >[]; setUp(() { tmpDir = setupRepo(Directory('test/assets/blamerepo/')); diff --git a/test/blob_test.dart b/test/blob_test.dart index d7080e4..c3c7274 100644 --- a/test/blob_test.dart +++ b/test/blob_test.dart @@ -146,7 +146,7 @@ index e69de29..0000000 final a = repo.lookupBlob( repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'], ); - final patch = Patch.createFrom( + final patch = Patch.create( a: a, b: 'Feature edit\n', aPath: path, @@ -162,7 +162,7 @@ index e69de29..0000000 final a = repo.lookupBlob( repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'], ); - final patch = Patch.createFrom( + final patch = Patch.create( a: a, b: null, aPath: path, diff --git a/test/patch_test.dart b/test/patch_test.dart index 33671b2..2d379d0 100644 --- a/test/patch_test.dart +++ b/test/patch_test.dart @@ -52,7 +52,7 @@ index e69de29..0000000 group('Patch', () { test('successfully creates from buffers', () { - final patch = Patch.createFrom( + final patch = Patch.create( a: oldBlob, b: newBlob, aPath: path, @@ -66,7 +66,7 @@ index e69de29..0000000 }); test('successfully creates from one buffer (add)', () { - final patch = Patch.createFrom( + final patch = Patch.create( a: null, b: newBlob, aPath: path, @@ -79,7 +79,7 @@ index e69de29..0000000 }); test('successfully creates from one buffer (delete)', () { - final patch = Patch.createFrom( + final patch = Patch.create( a: oldBlob, b: null, aPath: path, @@ -94,7 +94,7 @@ index e69de29..0000000 test('successfully creates from blobs', () { final a = repo.lookupBlob(oldBlobID); final b = repo.lookupBlob(newBlobID); - final patch = Patch.createFrom( + final patch = Patch.create( a: a, b: b, aPath: path, @@ -108,7 +108,7 @@ index e69de29..0000000 test('successfully creates from one blob (add)', () { final b = repo.lookupBlob(newBlobID); - final patch = Patch.createFrom( + final patch = Patch.create( a: null, b: b, aPath: path, @@ -122,7 +122,7 @@ index e69de29..0000000 test('successfully creates from one blob (delete)', () { final a = repo.lookupBlob(oldBlobID); - final patch = Patch.createFrom( + final patch = Patch.create( a: a, b: null, aPath: path, @@ -136,7 +136,7 @@ index e69de29..0000000 test('successfully creates from blob and buffer', () { final a = repo.lookupBlob(oldBlobID); - final patch = Patch.createFrom( + final patch = Patch.create( a: a, b: newBlob, aPath: path, @@ -153,7 +153,7 @@ index e69de29..0000000 repo['fc38877b2552ab554752d9a77e1f48f738cca79b'], ); expect( - () => Patch.createFrom( + () => Patch.create( a: commit, b: null, aPath: 'file', @@ -163,7 +163,7 @@ index e69de29..0000000 ); expect( - () => Patch.createFrom( + () => Patch.create( a: null, b: commit, aPath: 'file',