style: use Object instead of dynamic

This commit is contained in:
Aleksey Kulikov 2021-10-13 16:10:18 +03:00
parent 1972c6d1ab
commit beed516c89
13 changed files with 84 additions and 56 deletions

View file

@ -10,7 +10,7 @@ import 'libgit2_bindings.dart';
/// or a [String] value, if the attribute was set to an actual string. /// or a [String] value, if the attribute was set to an actual string.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
dynamic getAttribute({ Object? getAttribute({
required Pointer<git_repository> repoPointer, required Pointer<git_repository> repoPointer,
required int flags, required int flags,
required String path, required String path,

View file

@ -28,12 +28,15 @@ void head({
final pathPointers = initOpts[1]; final pathPointers = initOpts[1];
final strArray = initOpts[2]; final strArray = initOpts[2];
final error = libgit2.git_checkout_head(repoPointer, optsC); final error = libgit2.git_checkout_head(
repoPointer,
optsC as Pointer<git_checkout_options>,
);
for (final p in pathPointers) { for (final p in pathPointers as List) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray as Pointer);
calloc.free(optsC); calloc.free(optsC);
if (error < 0) { if (error < 0) {
@ -59,12 +62,16 @@ void index({
final pathPointers = initOpts[1]; final pathPointers = initOpts[1];
final strArray = initOpts[2]; final strArray = initOpts[2];
final error = libgit2.git_checkout_index(repoPointer, nullptr, optsC); final error = libgit2.git_checkout_index(
repoPointer,
nullptr,
optsC as Pointer<git_checkout_options>,
);
for (final p in pathPointers) { for (final p in pathPointers as List) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray as Pointer);
calloc.free(optsC); calloc.free(optsC);
if (error < 0) { if (error < 0) {
@ -92,12 +99,16 @@ void tree({
final pathPointers = initOpts[1]; final pathPointers = initOpts[1];
final strArray = initOpts[2]; final strArray = initOpts[2];
final error = libgit2.git_checkout_tree(repoPointer, treeishPointer, optsC); final error = libgit2.git_checkout_tree(
repoPointer,
treeishPointer,
optsC as Pointer<git_checkout_options>,
);
for (final p in pathPointers) { for (final p in pathPointers as List) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray as Pointer);
calloc.free(optsC); calloc.free(optsC);
if (error < 0) { if (error < 0) {
@ -105,12 +116,12 @@ void tree({
} }
} }
List<dynamic> initOptions({ List<Object> initOptions({
required int strategy, required int strategy,
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
final optsC = calloc<git_checkout_options>(sizeOf<git_checkout_options>()); final optsC = calloc<git_checkout_options>();
libgit2.git_checkout_options_init(optsC, GIT_CHECKOUT_OPTIONS_VERSION); libgit2.git_checkout_options_init(optsC, GIT_CHECKOUT_OPTIONS_VERSION);
optsC.ref.checkout_strategy = strategy; optsC.ref.checkout_strategy = strategy;

View file

@ -10,7 +10,7 @@ import '../util.dart';
/// you must call `free()` on the patch when done. /// you must call `free()` on the patch when done.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> fromBuffers({ Map<String, Pointer?> fromBuffers({
String? oldBuffer, String? oldBuffer,
String? oldAsPath, String? oldAsPath,
String? newBuffer, String? newBuffer,
@ -66,7 +66,7 @@ Map<String, dynamic> fromBuffers({
/// must call `free()` on the patch when done. /// must call `free()` on the patch when done.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> fromBlobs({ Map<String, Pointer?> fromBlobs({
Pointer<git_blob>? oldBlobPointer, Pointer<git_blob>? oldBlobPointer,
String? oldAsPath, String? oldAsPath,
Pointer<git_blob>? newBlobPointer, Pointer<git_blob>? newBlobPointer,
@ -114,7 +114,7 @@ Map<String, dynamic> fromBlobs({
/// call `free()` on the patch when done. /// call `free()` on the patch when done.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> fromBlobAndBuffer({ Map<String, Pointer?> fromBlobAndBuffer({
Pointer<git_blob>? oldBlobPointer, Pointer<git_blob>? oldBlobPointer,
String? oldAsPath, String? oldAsPath,
String? buffer, String? buffer,
@ -194,7 +194,7 @@ int numHunks(Pointer<git_patch> patch) => libgit2.git_patch_num_hunks(patch);
/// Given a patch and a hunk index into the patch, this returns detailed information about that hunk. /// Given a patch and a hunk index into the patch, this returns detailed information about that hunk.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> hunk({ Map<String, Object> hunk({
required Pointer<git_patch> patchPointer, required Pointer<git_patch> patchPointer,
required int hunkIndex, required int hunkIndex,
}) { }) {

View file

@ -374,7 +374,7 @@ void connect({
/// remains available after disconnecting. /// remains available after disconnecting.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
List<Map<String, dynamic>> lsRemotes(Pointer<git_remote> remote) { List<Map<String, Object?>> lsRemotes(Pointer<git_remote> remote) {
final out = calloc<Pointer<Pointer<git_remote_head>>>(); final out = calloc<Pointer<Pointer<git_remote_head>>>();
final size = calloc<Uint64>(); final size = calloc<Uint64>();
final error = libgit2.git_remote_ls(out, size, remote); final error = libgit2.git_remote_ls(out, size, remote);
@ -384,10 +384,10 @@ List<Map<String, dynamic>> lsRemotes(Pointer<git_remote> remote) {
calloc.free(size); calloc.free(size);
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
var result = <Map<String, dynamic>>[]; var result = <Map<String, Object?>>[];
for (var i = 0; i < size.value; i++) { for (var i = 0; i < size.value; i++) {
var remote = <String, dynamic>{}; var remote = <String, Object?>{};
Oid? loid; Oid? loid;
final bool local = out[0][i].ref.local == 1 ? true : false; final bool local = out[0][i].ref.local == 1 ? true : false;

View file

@ -72,10 +72,10 @@ void apply({
final error = libgit2.git_stash_apply(repoPointer, index, options); 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(p);
} }
calloc.free(strArray); calloc.free(strArray as Pointer);
calloc.free(optsC); calloc.free(optsC);
calloc.free(options); calloc.free(options);
@ -127,10 +127,10 @@ void pop({
final error = libgit2.git_stash_pop(repoPointer, index, options); 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(p);
} }
calloc.free(strArray); calloc.free(strArray as Pointer);
calloc.free(optsC); calloc.free(optsC);
calloc.free(options); calloc.free(options);

View file

@ -97,7 +97,11 @@ class Blob {
interhunkLines: interhunkLines, interhunkLines: interhunkLines,
); );
return Patch(result['patch'], result['a'], result['b']); return Patch(
result['patch'] as Pointer<git_patch>,
result['a'],
result['b'],
);
} }
/// Directly generate a [Patch] from the difference between the blob and a buffer. /// Directly generate a [Patch] from the difference between the blob and a buffer.
@ -123,7 +127,11 @@ class Blob {
interhunkLines: interhunkLines, interhunkLines: interhunkLines,
); );
return Patch(result['patch'], result['a'], result['b']); return Patch(
result['patch'] as Pointer<git_patch>,
result['a'],
result['b'],
);
} }
/// Releases memory allocated for blob object. /// Releases memory allocated for blob object.

View file

@ -99,7 +99,9 @@ class Config with IterableMixin<ConfigEntry> {
} }
/// Sets the [value] of config [variable]. /// 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) { if (value is bool) {
bindings.setBool( bindings.setBool(
configPointer: _configPointer, configPointer: _configPointer,
@ -112,12 +114,14 @@ class Config with IterableMixin<ConfigEntry> {
variable: variable, variable: variable,
value: value, value: value,
); );
} else { } else if (value is String) {
bindings.setString( bindings.setString(
configPointer: _configPointer, configPointer: _configPointer,
variable: variable, variable: variable,
value: value, value: value,
); );
} else {
throw ArgumentError.value('$value must be either bool, int or String');
} }
} }

View file

@ -20,9 +20,9 @@ class Patch {
/// Should be freed with `free()` to release allocated memory. /// Should be freed with `free()` to release allocated memory.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Patch.createFrom({ Patch.create({
required dynamic a, required Object? a,
required dynamic b, required Object? b,
String? aPath, String? aPath,
String? bPath, String? bPath,
Set<GitDiff> flags = const {GitDiff.normal}, Set<GitDiff> flags = const {GitDiff.normal},
@ -32,12 +32,12 @@ class Patch {
libgit2.git_libgit2_init(); libgit2.git_libgit2_init();
final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
var result = <String, dynamic>{}; var result = <String, Pointer?>{};
if (a is Blob || a == null) { if (a is Blob?) {
if (b is Blob) { if (b is Blob) {
result = bindings.fromBlobs( result = bindings.fromBlobs(
oldBlobPointer: a?.pointer, oldBlobPointer: a?.pointer ?? nullptr,
oldAsPath: aPath, oldAsPath: aPath,
newBlobPointer: b.pointer, newBlobPointer: b.pointer,
newAsPath: bPath, newAsPath: bPath,
@ -45,7 +45,7 @@ class Patch {
contextLines: contextLines, contextLines: contextLines,
interhunkLines: interhunkLines, interhunkLines: interhunkLines,
); );
} else if (b is String || b == null) { } else if (b is String?) {
result = bindings.fromBlobAndBuffer( result = bindings.fromBlobAndBuffer(
oldBlobPointer: a?.pointer, oldBlobPointer: a?.pointer,
oldAsPath: aPath, oldAsPath: aPath,
@ -58,9 +58,9 @@ class Patch {
} else { } else {
throw ArgumentError('Provided argument(s) is not Blob or String'); 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( result = bindings.fromBuffers(
oldBuffer: a, oldBuffer: a as String?,
oldAsPath: aPath, oldAsPath: aPath,
newBuffer: b, newBuffer: b,
newAsPath: bPath, newAsPath: bPath,
@ -72,7 +72,7 @@ class Patch {
throw ArgumentError('Provided argument(s) is not Blob or String'); throw ArgumentError('Provided argument(s) is not Blob or String');
} }
_patchPointer = result['patch']; _patchPointer = result['patch'] as Pointer<git_patch>;
_aPointer = result['a']; _aPointer = result['a'];
_bPointer = result['b']; _bPointer = result['b'];
} }
@ -90,8 +90,8 @@ class Patch {
late final Pointer<git_patch> _patchPointer; late final Pointer<git_patch> _patchPointer;
dynamic _aPointer; Pointer<NativeType>? _aPointer;
dynamic _bPointer; Pointer<NativeType>? _bPointer;
/// Pointer to memory address for allocated patch object. /// Pointer to memory address for allocated patch object.
Pointer<git_patch> get pointer => _patchPointer; Pointer<git_patch> get pointer => _patchPointer;
@ -134,7 +134,12 @@ class Patch {
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
final hunk = bindings.hunk(patchPointer: _patchPointer, hunkIndex: 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<git_diff_hunk>,
hunk['linesN'] as int,
i,
));
} }
return hunks; return hunks;
@ -143,10 +148,10 @@ class Patch {
/// Releases memory allocated for patch object. /// Releases memory allocated for patch object.
void free() { void free() {
if (_aPointer != null) { if (_aPointer != null) {
calloc.free(_aPointer); calloc.free(_aPointer!);
} }
if (_bPointer != null) { if (_bPointer != null) {
calloc.free(_bPointer); calloc.free(_bPointer!);
} }
bindings.free(_patchPointer); bindings.free(_patchPointer);
} }

View file

@ -194,7 +194,7 @@ class Remote {
/// specified url. By default connection isn't done through proxy. /// specified url. By default connection isn't done through proxy.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
List<Map<String, dynamic>> ls({ List<Map<String, Object?>> ls({
String? proxy, String? proxy,
Callbacks callbacks = const Callbacks(), Callbacks callbacks = const Callbacks(),
}) { }) {

View file

@ -1237,7 +1237,7 @@ class Repository {
/// or a [String] value, if the attribute was set to an actual string. /// or a [String] value, if the attribute was set to an actual string.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
dynamic getAttribute({ Object? getAttribute({
required String path, required String path,
required String name, required String name,
Set<GitAttributeCheck> flags = const {GitAttributeCheck.fileThenIndex}, Set<GitAttributeCheck> flags = const {GitAttributeCheck.fileThenIndex},

View file

@ -8,7 +8,7 @@ void main() {
late Directory tmpDir; late Directory tmpDir;
late Signature sig1; late Signature sig1;
late Signature sig2; late Signature sig2;
var hunks = <Map<String, dynamic>>[]; var hunks = <Map<String, Object>>[];
setUp(() { setUp(() {
tmpDir = setupRepo(Directory('test/assets/blamerepo/')); tmpDir = setupRepo(Directory('test/assets/blamerepo/'));

View file

@ -146,7 +146,7 @@ index e69de29..0000000
final a = repo.lookupBlob( final a = repo.lookupBlob(
repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'], repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'],
); );
final patch = Patch.createFrom( final patch = Patch.create(
a: a, a: a,
b: 'Feature edit\n', b: 'Feature edit\n',
aPath: path, aPath: path,
@ -162,7 +162,7 @@ index e69de29..0000000
final a = repo.lookupBlob( final a = repo.lookupBlob(
repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'], repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'],
); );
final patch = Patch.createFrom( final patch = Patch.create(
a: a, a: a,
b: null, b: null,
aPath: path, aPath: path,

View file

@ -52,7 +52,7 @@ index e69de29..0000000
group('Patch', () { group('Patch', () {
test('successfully creates from buffers', () { test('successfully creates from buffers', () {
final patch = Patch.createFrom( final patch = Patch.create(
a: oldBlob, a: oldBlob,
b: newBlob, b: newBlob,
aPath: path, aPath: path,
@ -66,7 +66,7 @@ index e69de29..0000000
}); });
test('successfully creates from one buffer (add)', () { test('successfully creates from one buffer (add)', () {
final patch = Patch.createFrom( final patch = Patch.create(
a: null, a: null,
b: newBlob, b: newBlob,
aPath: path, aPath: path,
@ -79,7 +79,7 @@ index e69de29..0000000
}); });
test('successfully creates from one buffer (delete)', () { test('successfully creates from one buffer (delete)', () {
final patch = Patch.createFrom( final patch = Patch.create(
a: oldBlob, a: oldBlob,
b: null, b: null,
aPath: path, aPath: path,
@ -94,7 +94,7 @@ index e69de29..0000000
test('successfully creates from blobs', () { test('successfully creates from blobs', () {
final a = repo.lookupBlob(oldBlobID); final a = repo.lookupBlob(oldBlobID);
final b = repo.lookupBlob(newBlobID); final b = repo.lookupBlob(newBlobID);
final patch = Patch.createFrom( final patch = Patch.create(
a: a, a: a,
b: b, b: b,
aPath: path, aPath: path,
@ -108,7 +108,7 @@ index e69de29..0000000
test('successfully creates from one blob (add)', () { test('successfully creates from one blob (add)', () {
final b = repo.lookupBlob(newBlobID); final b = repo.lookupBlob(newBlobID);
final patch = Patch.createFrom( final patch = Patch.create(
a: null, a: null,
b: b, b: b,
aPath: path, aPath: path,
@ -122,7 +122,7 @@ index e69de29..0000000
test('successfully creates from one blob (delete)', () { test('successfully creates from one blob (delete)', () {
final a = repo.lookupBlob(oldBlobID); final a = repo.lookupBlob(oldBlobID);
final patch = Patch.createFrom( final patch = Patch.create(
a: a, a: a,
b: null, b: null,
aPath: path, aPath: path,
@ -136,7 +136,7 @@ index e69de29..0000000
test('successfully creates from blob and buffer', () { test('successfully creates from blob and buffer', () {
final a = repo.lookupBlob(oldBlobID); final a = repo.lookupBlob(oldBlobID);
final patch = Patch.createFrom( final patch = Patch.create(
a: a, a: a,
b: newBlob, b: newBlob,
aPath: path, aPath: path,
@ -153,7 +153,7 @@ index e69de29..0000000
repo['fc38877b2552ab554752d9a77e1f48f738cca79b'], repo['fc38877b2552ab554752d9a77e1f48f738cca79b'],
); );
expect( expect(
() => Patch.createFrom( () => Patch.create(
a: commit, a: commit,
b: null, b: null,
aPath: 'file', aPath: 'file',
@ -163,7 +163,7 @@ index e69de29..0000000
); );
expect( expect(
() => Patch.createFrom( () => Patch.create(
a: null, a: null,
b: commit, b: commit,
aPath: 'file', aPath: 'file',