refactor(config): simplify api methods names

This commit is contained in:
Aleksey Kulikov 2021-08-21 11:25:06 +03:00
parent 9873d6ccc0
commit da8494d3e2
5 changed files with 50 additions and 59 deletions

View file

@ -13,8 +13,7 @@ void main() async {
final config = Config.open(); final config = Config.open();
print('All entries of system/global config:'); print('All entries of system/global config:');
final entries = config.getEntries(); for (final entry in config.variables.entries) {
for (final entry in entries.entries) {
print('${entry.key}: ${entry.value}'); print('${entry.key}: ${entry.value}');
} }
// free() should be called on object to free memory when done. // free() should be called on object to free memory when done.
@ -26,18 +25,17 @@ void main() async {
final repoConfig = Config.open('$tmpDir/.git/config'); final repoConfig = Config.open('$tmpDir/.git/config');
print('\nAll entries of repo config:'); print('\nAll entries of repo config:');
final entries = repoConfig.getEntries(); for (final entry in repoConfig.variables.entries) {
for (final entry in entries.entries) {
print('${entry.key}: ${entry.value}'); print('${entry.key}: ${entry.value}');
} }
// Set value of config variable // Set value of config variable
repoConfig.setValue('core.variable', 'value'); repoConfig['core.variable'] = 'value';
print( print(
'\nNew value for variable "core.variable": ${repoConfig.getValue('core.variable')}'); '\nNew value for variable "core.variable": ${repoConfig['core.variable']}');
// Delete variable // Delete variable
repoConfig.deleteEntry('core.variable'); repoConfig.delete('core.variable');
repoConfig.free(); repoConfig.free();
} catch (e) { } catch (e) {
@ -50,7 +48,7 @@ void main() async {
final globalConfig = Config.global(); final globalConfig = Config.global();
// Get value of config variable. // Get value of config variable.
final userName = globalConfig.getValue('user.name'); final userName = globalConfig['user.name'];
print('\nUser Name from global config: $userName'); print('\nUser Name from global config: $userName');
globalConfig.free(); globalConfig.free();

View file

@ -222,7 +222,7 @@ Map<String, String> getEntries(Pointer<git_config> cfg) {
/// (usually the local one). /// (usually the local one).
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void deleteEntry(Pointer<git_config> cfg, String variable) { void delete(Pointer<git_config> cfg, String variable) {
final name = variable.toNativeUtf8().cast<Int8>(); final name = variable.toNativeUtf8().cast<Int8>();
final error = libgit2.git_config_delete_entry(cfg, name); final error = libgit2.git_config_delete_entry(cfg, name);
calloc.free(name); calloc.free(name);
@ -236,7 +236,7 @@ void deleteEntry(Pointer<git_config> cfg, String variable) {
/// ///
/// If regexp is present, then the iterator will only iterate over all /// If regexp is present, then the iterator will only iterate over all
/// values which match the pattern. /// values which match the pattern.
List<String> getMultivarValue( List<String> multivarValues(
Pointer<git_config> cfg, Pointer<git_config> cfg,
String variable, String variable,
String? regexp, String? regexp,
@ -270,7 +270,7 @@ List<String> getMultivarValue(
/// highest level (usually the local one). /// highest level (usually the local one).
/// ///
/// The regexp is applied case-sensitively on the value. /// The regexp is applied case-sensitively on the value.
void setMultivarValue( void setMultivar(
Pointer<git_config> cfg, Pointer<git_config> cfg,
String variable, String variable,
String regexp, String regexp,

View file

@ -84,17 +84,14 @@ class Config {
Config get snapshot => Config(bindings.snapshot(_configPointer)); Config get snapshot => Config(bindings.snapshot(_configPointer));
/// Returns map of all the config variables and their values. /// Returns map of all the config variables and their values.
Map<String, String> getEntries() { Map<String, String> get variables => bindings.getEntries(_configPointer);
return bindings.getEntries(_configPointer);
}
/// Returns the value of config [variable]. /// Returns the value of config [variable].
String getValue(String variable) { String operator [](String variable) =>
return bindings.getValue(_configPointer, variable); bindings.getValue(_configPointer, variable);
}
/// Sets the [value] of config [variable]. /// Sets the [value] of config [variable].
void setValue(String variable, dynamic value) { void operator []=(String variable, dynamic value) {
if (value.runtimeType == bool) { if (value.runtimeType == bool) {
bindings.setBool(_configPointer, variable, value); bindings.setBool(_configPointer, variable, value);
} else if (value.runtimeType == int) { } else if (value.runtimeType == int) {
@ -108,16 +105,14 @@ class Config {
/// (usually the local one). /// (usually the local one).
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void deleteEntry(String variable) { void delete(String variable) => bindings.delete(_configPointer, variable);
bindings.deleteEntry(_configPointer, variable);
}
/// Returns list of values for multivar [variable] /// Returns list of values for multivar [variable]
/// ///
/// If [regexp] is present, then the iterator will only iterate over all /// If [regexp] is present, then the iterator will only iterate over all
/// values which match the pattern. /// values which match the pattern.
List<String> getMultivarValue(String variable, {String? regexp}) { List<String> multivar(String variable, {String? regexp}) {
return bindings.getMultivarValue(_configPointer, variable, regexp); return bindings.multivarValues(_configPointer, variable, regexp);
} }
/// Sets the [value] of a multivar [variable] in the config file with the /// Sets the [value] of a multivar [variable] in the config file with the
@ -125,8 +120,8 @@ class Config {
/// ///
/// The [regexp] is applied case-sensitively on the value. /// The [regexp] is applied case-sensitively on the value.
/// Empty [regexp] sets [value] for all values of a multivar [variable]. /// Empty [regexp] sets [value] for all values of a multivar [variable].
void setMultivarValue(String variable, String regexp, String value) { void setMultivar(String variable, String regexp, String value) {
bindings.setMultivarValue(_configPointer, variable, regexp, value); bindings.setMultivar(_configPointer, variable, regexp, value);
} }
/// Deletes one or several values from a multivar [variable] in the config file /// Deletes one or several values from a multivar [variable] in the config file

View file

@ -34,61 +34,59 @@ void main() {
expect(config, isA<Config>()); expect(config, isA<Config>());
}); });
test('getEntries() returns map with variables and values', () { test('returns map with variables and values', () {
final entries = config.getEntries(); expect(config.variables['remote.origin.url'], equals('someurl'));
expect(entries['remote.origin.url'], equals('someurl'));
}); });
group('getValue()', () { group('get value', () {
test('returns value of variable', () { test('returns value of variable', () {
expect(config.getValue('core.bare'), equals('false')); expect(config['core.bare'], equals('false'));
}); });
test('throws when variable isn\'t found', () { test('throws when variable isn\'t found', () {
expect( expect(
() => config.getValue('not.there'), () => config['not.there'],
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
}); });
group('setValue()', () { group('set value', () {
test('sets boolean value for provided variable', () { test('sets boolean value for provided variable', () {
config.setValue('core.bare', true); config['core.bare'] = true;
expect(config.getValue('core.bare'), equals('true')); expect(config['core.bare'], equals('true'));
}); });
test('sets integer value for provided variable', () { test('sets integer value for provided variable', () {
config.setValue('core.repositoryformatversion', 1); config['core.repositoryformatversion'] = 1;
expect(config.getValue('core.repositoryformatversion'), equals('1')); expect(config['core.repositoryformatversion'], equals('1'));
}); });
test('sets string value for provided variable', () { test('sets string value for provided variable', () {
config.setValue('remote.origin.url', 'updated'); config['remote.origin.url'] = 'updated';
expect(config.getValue('remote.origin.url'), equals('updated')); expect(config['remote.origin.url'], equals('updated'));
}); });
}); });
group('deleteEntry()', () { group('delete', () {
test('successfully deletes entry', () { test('successfully deletes entry', () {
expect(config.getValue('core.bare'), equals('false')); expect(config['core.bare'], equals('false'));
config.deleteEntry('core.bare'); config.delete('core.bare');
final entries = config.getEntries(); expect(config.variables['core.bare'], isNull);
expect(entries['core.bare'], isNull);
}); });
test('throws on deleting non existing variable', () { test('throws on deleting non existing variable', () {
expect( expect(
() => config.deleteEntry('not.there'), () => config.delete('not.there'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
}); });
group('getMultivarValue()', () { group('get multivar values', () {
test('returns values of multivar', () { test('returns list of values', () {
expect( expect(
config.getMultivarValue('core.gitproxy'), config.multivar('core.gitproxy'),
[ [
'proxy-command for kernel.org', 'proxy-command for kernel.org',
'default-proxy', 'default-proxy',
@ -96,29 +94,29 @@ void main() {
); );
}); });
test('returns values of multivar for provided regexp', () { test('returns list of values for provided regexp', () {
expect( expect(
config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'), config.multivar('core.gitproxy', regexp: 'for kernel.org\$'),
['proxy-command for kernel.org'], ['proxy-command for kernel.org'],
); );
}); });
test('returns empty list if multivar not found', () { test('returns empty list if multivar not found', () {
expect(config.getMultivarValue('not.there'), equals([])); expect(config.multivar('not.there'), equals([]));
}); });
}); });
group('setMultivarValue()', () { group('setMultivarValue()', () {
test('sets value of multivar', () { test('sets value of multivar', () {
config.setMultivarValue('core.gitproxy', 'default', 'updated'); config.setMultivar('core.gitproxy', 'default', 'updated');
final multivarValues = config.getMultivarValue('core.gitproxy'); final multivarValues = config.multivar('core.gitproxy');
expect(multivarValues, isNot(contains('default-proxy'))); expect(multivarValues, isNot(contains('default-proxy')));
expect(multivarValues, contains('updated')); expect(multivarValues, contains('updated'));
}); });
test('sets value for all multivar values when regexp is empty', () { test('sets value for all multivar values when regexp is empty', () {
config.setMultivarValue('core.gitproxy', '', 'updated'); config.setMultivar('core.gitproxy', '', 'updated');
final multivarValues = config.getMultivarValue('core.gitproxy'); final multivarValues = config.multivar('core.gitproxy');
expect(multivarValues, isNot(contains('default-proxy'))); expect(multivarValues, isNot(contains('default-proxy')));
expect(multivarValues, isNot(contains('proxy-command for kernel.org'))); expect(multivarValues, isNot(contains('proxy-command for kernel.org')));
expect(multivarValues, contains('updated')); expect(multivarValues, contains('updated'));
@ -129,14 +127,14 @@ void main() {
group('deleteMultivar()', () { group('deleteMultivar()', () {
test('successfully deletes value of a multivar', () { test('successfully deletes value of a multivar', () {
expect( expect(
config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'), config.multivar('core.gitproxy', regexp: 'for kernel.org\$'),
['proxy-command for kernel.org'], ['proxy-command for kernel.org'],
); );
config.deleteMultivar('core.gitproxy', 'for kernel.org\$'); config.deleteMultivar('core.gitproxy', 'for kernel.org\$');
expect( expect(
config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'), config.multivar('core.gitproxy', regexp: 'for kernel.org\$'),
[], [],
); );
}); });
@ -144,7 +142,7 @@ void main() {
test('successfully deletes all values of a multivar when regexp is empty', test('successfully deletes all values of a multivar when regexp is empty',
() { () {
expect( expect(
config.getMultivarValue('core.gitproxy'), config.multivar('core.gitproxy'),
[ [
'proxy-command for kernel.org', 'proxy-command for kernel.org',
'default-proxy', 'default-proxy',
@ -153,7 +151,7 @@ void main() {
config.deleteMultivar('core.gitproxy', ''); config.deleteMultivar('core.gitproxy', '');
expect(config.getMultivarValue('core.gitproxy'), []); expect(config.multivar('core.gitproxy'), []);
}); });
}); });
}); });

View file

@ -143,7 +143,7 @@ void main() {
test('returns config for repository', () { test('returns config for repository', () {
final config = repo.config; final config = repo.config;
expect(config.getValue('remote.origin.url'), expect(config['remote.origin.url'],
'git://github.com/SkinnyMind/libgit2dart.git'); 'git://github.com/SkinnyMind/libgit2dart.git');
config.free(); config.free();