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();
print('All entries of system/global config:');
final entries = config.getEntries();
for (final entry in entries.entries) {
for (final entry in config.variables.entries) {
print('${entry.key}: ${entry.value}');
}
// 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');
print('\nAll entries of repo config:');
final entries = repoConfig.getEntries();
for (final entry in entries.entries) {
for (final entry in repoConfig.variables.entries) {
print('${entry.key}: ${entry.value}');
}
// Set value of config variable
repoConfig.setValue('core.variable', 'value');
repoConfig['core.variable'] = 'value';
print(
'\nNew value for variable "core.variable": ${repoConfig.getValue('core.variable')}');
'\nNew value for variable "core.variable": ${repoConfig['core.variable']}');
// Delete variable
repoConfig.deleteEntry('core.variable');
repoConfig.delete('core.variable');
repoConfig.free();
} catch (e) {
@ -50,7 +48,7 @@ void main() async {
final globalConfig = Config.global();
// Get value of config variable.
final userName = globalConfig.getValue('user.name');
final userName = globalConfig['user.name'];
print('\nUser Name from global config: $userName');
globalConfig.free();

View file

@ -222,7 +222,7 @@ Map<String, String> getEntries(Pointer<git_config> cfg) {
/// (usually the local one).
///
/// 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 error = libgit2.git_config_delete_entry(cfg, 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
/// values which match the pattern.
List<String> getMultivarValue(
List<String> multivarValues(
Pointer<git_config> cfg,
String variable,
String? regexp,
@ -270,7 +270,7 @@ List<String> getMultivarValue(
/// highest level (usually the local one).
///
/// The regexp is applied case-sensitively on the value.
void setMultivarValue(
void setMultivar(
Pointer<git_config> cfg,
String variable,
String regexp,

View file

@ -84,17 +84,14 @@ class Config {
Config get snapshot => Config(bindings.snapshot(_configPointer));
/// Returns map of all the config variables and their values.
Map<String, String> getEntries() {
return bindings.getEntries(_configPointer);
}
Map<String, String> get variables => bindings.getEntries(_configPointer);
/// Returns the value of config [variable].
String getValue(String variable) {
return bindings.getValue(_configPointer, variable);
}
String operator [](String variable) =>
bindings.getValue(_configPointer, variable);
/// Sets the [value] of config [variable].
void setValue(String variable, dynamic value) {
void operator []=(String variable, dynamic value) {
if (value.runtimeType == bool) {
bindings.setBool(_configPointer, variable, value);
} else if (value.runtimeType == int) {
@ -108,16 +105,14 @@ class Config {
/// (usually the local one).
///
/// Throws a [LibGit2Error] if error occured.
void deleteEntry(String variable) {
bindings.deleteEntry(_configPointer, variable);
}
void delete(String variable) => bindings.delete(_configPointer, variable);
/// Returns list of values for multivar [variable]
///
/// If [regexp] is present, then the iterator will only iterate over all
/// values which match the pattern.
List<String> getMultivarValue(String variable, {String? regexp}) {
return bindings.getMultivarValue(_configPointer, variable, regexp);
List<String> multivar(String variable, {String? regexp}) {
return bindings.multivarValues(_configPointer, variable, regexp);
}
/// 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.
/// Empty [regexp] sets [value] for all values of a multivar [variable].
void setMultivarValue(String variable, String regexp, String value) {
bindings.setMultivarValue(_configPointer, variable, regexp, value);
void setMultivar(String variable, String regexp, String value) {
bindings.setMultivar(_configPointer, variable, regexp, value);
}
/// 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>());
});
test('getEntries() returns map with variables and values', () {
final entries = config.getEntries();
expect(entries['remote.origin.url'], equals('someurl'));
test('returns map with variables and values', () {
expect(config.variables['remote.origin.url'], equals('someurl'));
});
group('getValue()', () {
group('get value', () {
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', () {
expect(
() => config.getValue('not.there'),
() => config['not.there'],
throwsA(isA<LibGit2Error>()),
);
});
});
group('setValue()', () {
group('set value', () {
test('sets boolean value for provided variable', () {
config.setValue('core.bare', true);
expect(config.getValue('core.bare'), equals('true'));
config['core.bare'] = true;
expect(config['core.bare'], equals('true'));
});
test('sets integer value for provided variable', () {
config.setValue('core.repositoryformatversion', 1);
expect(config.getValue('core.repositoryformatversion'), equals('1'));
config['core.repositoryformatversion'] = 1;
expect(config['core.repositoryformatversion'], equals('1'));
});
test('sets string value for provided variable', () {
config.setValue('remote.origin.url', 'updated');
expect(config.getValue('remote.origin.url'), equals('updated'));
config['remote.origin.url'] = 'updated';
expect(config['remote.origin.url'], equals('updated'));
});
});
group('deleteEntry()', () {
group('delete', () {
test('successfully deletes entry', () {
expect(config.getValue('core.bare'), equals('false'));
config.deleteEntry('core.bare');
final entries = config.getEntries();
expect(entries['core.bare'], isNull);
expect(config['core.bare'], equals('false'));
config.delete('core.bare');
expect(config.variables['core.bare'], isNull);
});
test('throws on deleting non existing variable', () {
expect(
() => config.deleteEntry('not.there'),
() => config.delete('not.there'),
throwsA(isA<LibGit2Error>()),
);
});
});
group('getMultivarValue()', () {
test('returns values of multivar', () {
group('get multivar values', () {
test('returns list of values', () {
expect(
config.getMultivarValue('core.gitproxy'),
config.multivar('core.gitproxy'),
[
'proxy-command for kernel.org',
'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(
config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'),
config.multivar('core.gitproxy', regexp: 'for kernel.org\$'),
['proxy-command for kernel.org'],
);
});
test('returns empty list if multivar not found', () {
expect(config.getMultivarValue('not.there'), equals([]));
expect(config.multivar('not.there'), equals([]));
});
});
group('setMultivarValue()', () {
test('sets value of multivar', () {
config.setMultivarValue('core.gitproxy', 'default', 'updated');
final multivarValues = config.getMultivarValue('core.gitproxy');
config.setMultivar('core.gitproxy', 'default', 'updated');
final multivarValues = config.multivar('core.gitproxy');
expect(multivarValues, isNot(contains('default-proxy')));
expect(multivarValues, contains('updated'));
});
test('sets value for all multivar values when regexp is empty', () {
config.setMultivarValue('core.gitproxy', '', 'updated');
final multivarValues = config.getMultivarValue('core.gitproxy');
config.setMultivar('core.gitproxy', '', 'updated');
final multivarValues = config.multivar('core.gitproxy');
expect(multivarValues, isNot(contains('default-proxy')));
expect(multivarValues, isNot(contains('proxy-command for kernel.org')));
expect(multivarValues, contains('updated'));
@ -129,14 +127,14 @@ void main() {
group('deleteMultivar()', () {
test('successfully deletes value of a multivar', () {
expect(
config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'),
config.multivar('core.gitproxy', regexp: 'for kernel.org\$'),
['proxy-command for kernel.org'],
);
config.deleteMultivar('core.gitproxy', 'for kernel.org\$');
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',
() {
expect(
config.getMultivarValue('core.gitproxy'),
config.multivar('core.gitproxy'),
[
'proxy-command for kernel.org',
'default-proxy',
@ -153,7 +151,7 @@ void main() {
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', () {
final config = repo.config;
expect(config.getValue('remote.origin.url'),
expect(config['remote.origin.url'],
'git://github.com/SkinnyMind/libgit2dart.git');
config.free();