Add configuration provider and simple provider functions

This commit is contained in:
Sambo Chea 2021-03-24 13:24:40 +07:00
commit 57f3cd483e
11 changed files with 110 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
# Files and directories created by pub
.dart_tool/
.packages
# Conventional directory for build outputs
build/
# Directory created by dartdoc
doc/api/

4
CHANGELOG.md Normal file
View File

@ -0,0 +1,4 @@
## 1.0.0
- Initial version, created by Stagehand
- Add configurable interface, created by @SomboChea

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Dart Configurable Environment
- [ ] Allow to get property from env file
- [ ] Cache property for runtime

14
analysis_options.yaml Normal file
View File

@ -0,0 +1,14 @@
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:pedantic/analysis_options.yaml
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types
analyzer:
# exclude:
# - path/to/excluded/files/**

3
bin/configurable.dart Normal file
View File

@ -0,0 +1,3 @@
void main(List<String> arguments) {
print('Just, say Hello to You!');
}

View File

@ -0,0 +1,4 @@
abstract class ConfigurationProvider {
bool containsKey(String key);
String? getOrNull(String key, {String? defaultValue});
}

View File

@ -0,0 +1,22 @@
import 'package:configurable/configuration_provider.dart';
class SimpleConfigurationProvider implements ConfigurationProvider {
Map<String, String?> configs = {};
@override
String? getOrNull(String key, {String? defaultValue}) {
var value = configs[key];
if (value == null) {
configs[key] = defaultValue;
return defaultValue;
}
return value;
}
@override
bool containsKey(String key) {
return configs.containsKey(key);
}
}

26
lib/system_config.dart Normal file
View File

@ -0,0 +1,26 @@
import 'package:configurable/configuration_provider.dart';
import 'package:configurable/simple_configuration_provider.dart';
class SystemConfig {
static ConfigurationProvider? provider;
static void setProvider(ConfigurationProvider provider) {
SystemConfig.provider = provider;
}
static ConfigurationProvider getProvider() {
if (provider == null) {
setProvider(SimpleConfigurationProvider());
}
return provider!;
}
static String get(String key, {String? defaultValue}) {
return getOrNull(key, defaultValue: defaultValue)!;
}
static String? getOrNull(String key, {String? defaultValue}) {
return getProvider().getOrNull(key);
}
}

12
pubspec.lock Normal file
View File

@ -0,0 +1,12 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
pedantic:
dependency: "direct dev"
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.0"
sdks:
dart: ">=2.12.0 <3.0.0"

13
pubspec.yaml Normal file
View File

@ -0,0 +1,13 @@
name: configurable
description: A simple command-line application.
# version: 1.0.0
# homepage: https://www.example.com
environment:
sdk: '>=2.12.0 <3.0.0'
#dependencies:
# path: ^1.7.0
dev_dependencies:
pedantic: ^1.9.0

0
test/config_test.dart Normal file
View File