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.
///
/// Throws a [LibGit2Error] if error occured.
dynamic getAttribute({
Object? getAttribute({
required Pointer<git_repository> repoPointer,
required int flags,
required String path,

View file

@ -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<git_checkout_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);
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<git_checkout_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);
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<git_checkout_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);
if (error < 0) {
@ -105,12 +116,12 @@ void tree({
}
}
List<dynamic> initOptions({
List<Object> initOptions({
required int strategy,
String? directory,
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);
optsC.ref.checkout_strategy = strategy;

View file

@ -10,7 +10,7 @@ import '../util.dart';
/// you must call `free()` on the patch when done.
///
/// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> fromBuffers({
Map<String, Pointer?> fromBuffers({
String? oldBuffer,
String? oldAsPath,
String? newBuffer,
@ -66,7 +66,7 @@ Map<String, dynamic> fromBuffers({
/// must call `free()` on the patch when done.
///
/// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> fromBlobs({
Map<String, Pointer?> fromBlobs({
Pointer<git_blob>? oldBlobPointer,
String? oldAsPath,
Pointer<git_blob>? newBlobPointer,
@ -114,7 +114,7 @@ Map<String, dynamic> fromBlobs({
/// call `free()` on the patch when done.
///
/// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> fromBlobAndBuffer({
Map<String, Pointer?> fromBlobAndBuffer({
Pointer<git_blob>? oldBlobPointer,
String? oldAsPath,
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.
///
/// Throws a [LibGit2Error] if error occured.
Map<String, dynamic> hunk({
Map<String, Object> hunk({
required Pointer<git_patch> patchPointer,
required int hunkIndex,
}) {

View file

@ -374,7 +374,7 @@ void connect({
/// remains available after disconnecting.
///
/// 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 size = calloc<Uint64>();
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);
throw LibGit2Error(libgit2.git_error_last());
} else {
var result = <Map<String, dynamic>>[];
var result = <Map<String, Object?>>[];
for (var i = 0; i < size.value; i++) {
var remote = <String, dynamic>{};
var remote = <String, Object?>{};
Oid? loid;
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);
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);

View file

@ -97,7 +97,11 @@ class Blob {
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.
@ -123,7 +127,11 @@ class Blob {
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.

View file

@ -99,7 +99,9 @@ class Config with IterableMixin<ConfigEntry> {
}
/// 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<ConfigEntry> {
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');
}
}

View file

@ -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<GitDiff> 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 = <String, dynamic>{};
var result = <String, Pointer?>{};
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<git_patch>;
_aPointer = result['a'];
_bPointer = result['b'];
}
@ -90,8 +90,8 @@ class Patch {
late final Pointer<git_patch> _patchPointer;
dynamic _aPointer;
dynamic _bPointer;
Pointer<NativeType>? _aPointer;
Pointer<NativeType>? _bPointer;
/// Pointer to memory address for allocated patch object.
Pointer<git_patch> 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<git_diff_hunk>,
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);
}

View file

@ -194,7 +194,7 @@ class Remote {
/// specified url. By default connection isn't done through proxy.
///
/// Throws a [LibGit2Error] if error occured.
List<Map<String, dynamic>> ls({
List<Map<String, Object?>> ls({
String? proxy,
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.
///
/// Throws a [LibGit2Error] if error occured.
dynamic getAttribute({
Object? getAttribute({
required String path,
required String name,
Set<GitAttributeCheck> flags = const {GitAttributeCheck.fileThenIndex},

View file

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

View file

@ -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,

View file

@ -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',