diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e7bd292..7afe6886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.0.0-alpha.24 +* Add `AngelEnv` class to `core`. +* Deprecate `Angel.isProduction`, in favor of `AngelEnv`. + # 2.0.0-alpha.23 * `ResponseContext.render` sets `charset` to `utf8` in `contentType`. diff --git a/analysis_options.yaml b/analysis_options.yaml index a0ee1ea1..fb26f9d9 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -3,4 +3,4 @@ analyzer: implicit-casts: false linter: rules: - - avoid_slow_async_io \ No newline at end of file + - avoid_slow_async_io diff --git a/lib/src/core/controller.dart b/lib/src/core/controller.dart index c30c05fc..01ba1713 100644 --- a/lib/src/core/controller.dart +++ b/lib/src/core/controller.dart @@ -75,9 +75,9 @@ class Controller { methodName != 'call' && methodName != 'equals' && methodName != '==') { - Expose exposeDecl = decl.function.annotations + var exposeDecl = decl.function.annotations .map((m) => m.reflectee) - .firstWhere((r) => r is Expose, orElse: () => null); + .firstWhere((r) => r is Expose, orElse: () => null) as Expose; if (exposeDecl == null) return; diff --git a/lib/src/core/core.dart b/lib/src/core/core.dart index 3822463a..755ae9ae 100644 --- a/lib/src/core/core.dart +++ b/lib/src/core/core.dart @@ -1,6 +1,7 @@ export 'anonymous_service.dart'; export 'controller.dart'; export 'driver.dart'; +export 'env.dart'; export 'hooked_service.dart'; export 'map_service.dart'; export 'metadata.dart'; diff --git a/lib/src/core/env.dart b/lib/src/core/env.dart index fa0860ca..b43e0bfa 100644 --- a/lib/src/core/env.dart +++ b/lib/src/core/env.dart @@ -1,15 +1,15 @@ import 'dart:io'; /// A constant instance of [AngelEnv]. -const AngelEnv angelEnv = const AngelEnv(); +const AngelEnvironment angelEnv = const AngelEnvironment(); /// Queries the environment's `ANGEL_ENV` value. -class AngelEnv { +class AngelEnvironment { final String _customValue; /// You can optionally provide a custom value, in order to override the system's /// value. - const AngelEnv([this._customValue]); + const AngelEnvironment([this._customValue]); /// Returns the value of the `ANGEL_ENV` variable; defaults to `'development'`. String get value => diff --git a/lib/src/core/hooked_service.dart b/lib/src/core/hooked_service.dart index 658d822a..25669c8d 100644 --- a/lib/src/core/hooked_service.dart +++ b/lib/src/core/hooked_service.dart @@ -90,7 +90,7 @@ class HookedService> /// Adds hooks to this instance. void addHooks(Angel app) { - Hooks hooks = getAnnotation(inner, Hooks, app.container.reflector); + Hooks hooks = getAnnotation(inner, app.container.reflector); List> before = [], after = []; if (hooks != null) { @@ -101,7 +101,7 @@ class HookedService> void applyListeners( Function fn, HookedServiceEventDispatcher dispatcher, [bool isAfter]) { - Hooks hooks = getAnnotation(fn, Hooks, app.container.reflector); + Hooks hooks = getAnnotation(fn, app.container.reflector); final listeners = >[] ..addAll(isAfter == true ? after : before); diff --git a/lib/src/core/injection.dart b/lib/src/core/injection.dart index 98c4cb84..dd14e81b 100644 --- a/lib/src/core/injection.dart +++ b/lib/src/core/injection.dart @@ -53,8 +53,8 @@ resolveInjection(requirement, InjectionRequest injection, RequestContext req, requirement.length == 2 && requirement.first is String && requirement.last is Type) { - String key = requirement.first; - Type type = requirement.last; + var key = requirement.first; + var type = requirement.last; if (req.params.containsKey(key) || req.app.configuration.containsKey(key) || _primitiveTypes.contains(type)) { diff --git a/lib/src/core/map_service.dart b/lib/src/core/map_service.dart index adcc854e..75eef3df 100644 --- a/lib/src/core/map_service.dart +++ b/lib/src/core/map_service.dart @@ -51,7 +51,7 @@ class MapService extends Service> { if (allowQuery == false || params == null || params['query'] is! Map) return new Future.value(items); else { - Map query = params['query']; + var query = params['query'] as Map; return new Future.value(items.where((item) { for (var key in query.keys) { diff --git a/lib/src/core/routable.dart b/lib/src/core/routable.dart index 2a51f01a..01cfe1a9 100644 --- a/lib/src/core/routable.dart +++ b/lib/src/core/routable.dart @@ -99,7 +99,7 @@ class Routable extends Router { final handlers = []; // Merge @Middleware declaration, if any Middleware middlewareDeclaration = - getAnnotation(handler, Middleware, _container?.reflector); + getAnnotation(handler, _container?.reflector); if (middlewareDeclaration != null) { handlers.addAll(middlewareDeclaration.handlers); } diff --git a/lib/src/core/server.dart b/lib/src/core/server.dart index af9cc062..8e944e60 100644 --- a/lib/src/core/server.dart +++ b/lib/src/core/server.dart @@ -15,6 +15,7 @@ import 'package:mime/mime.dart'; import 'package:tuple/tuple.dart'; import 'controller.dart'; +import 'env.dart'; import 'hooked_service.dart'; import 'request_context.dart'; import 'response_context.dart'; @@ -42,7 +43,6 @@ class Angel extends Routable { MiddlewarePipeline>> handlerCache = new HashMap(); Router _flattened; - bool _isProduction; Angel _parent; /// A global Map of converters that can transform responses bodies. @@ -75,6 +75,8 @@ class Angel extends Routable { /// A set of [Controller] objects that have been loaded into the application. Map get controllers => _controllers; + /// Now *deprecated*, in favor of [AngelEnv] and [angelEnv]. + /// /// Indicates whether the application is running in a production environment. /// /// The criteria for this is the `ANGEL_ENV` environment variable being set to @@ -82,10 +84,8 @@ class Angel extends Routable { /// /// This value is memoized the first time you call it, so do not change environment /// configuration at runtime! - bool get isProduction { - return _isProduction ??= - (Platform.environment['ANGEL_ENV'] == 'production'); - } + @deprecated + bool get isProduction => angelEnv.isProduction; /// Returns the parent instance of this application, if any. Angel get parent => _parent; @@ -282,7 +282,7 @@ class Angel extends Routable { return parent != null ? parent.findProperty(key) : null; } - /// Runs several optimizations, *if* [isProduction] is `true`. + /// Runs several optimizations, *if* [angelEnv.isProduction] is `true`. /// /// * Preprocesses all dependency injection, and eliminates the burden of reflecting handlers /// at run-time. @@ -291,7 +291,6 @@ class Angel extends Routable { /// You may [force] the optimization to run, if you are not running in production. void optimizeForProduction({bool force: false}) { if (isProduction == true || force == true) { - _isProduction = true; _flattened ??= flatten(this); logger?.info('Angel is running in production mode.'); } diff --git a/lib/src/core/service.dart b/lib/src/core/service.dart index 755fb6b1..7eb6d840 100644 --- a/lib/src/core/service.dart +++ b/lib/src/core/service.dart @@ -195,12 +195,12 @@ class Service extends Routable { // Add global middleware if declared on the instance itself Middleware before = - getAnnotation(service, Middleware, app.container.reflector); + getAnnotation(service, app.container.reflector); if (before != null) handlers.addAll(before.handlers); Middleware indexMiddleware = - getAnnotation(service.index, Middleware, app.container.reflector); + getAnnotation(service.index, app.container.reflector); get('/', (req, res) { return this.index(mergeMap([ {'query': req.queryParameters}, @@ -213,7 +213,7 @@ class Service extends Routable { ..addAll((indexMiddleware == null) ? [] : indexMiddleware.handlers)); Middleware createMiddleware = - getAnnotation(service.create, Middleware, app.container.reflector); + getAnnotation(service.create, app.container.reflector); post('/', (req, ResponseContext res) { return req.parseBody().then((_) { return this @@ -236,7 +236,7 @@ class Service extends Routable { (createMiddleware == null) ? [] : createMiddleware.handlers)); Middleware readMiddleware = - getAnnotation(service.read, Middleware, app.container.reflector); + getAnnotation(service.read, app.container.reflector); get('/:id', (req, res) { return this.read( @@ -252,7 +252,7 @@ class Service extends Routable { ..addAll((readMiddleware == null) ? [] : readMiddleware.handlers)); Middleware modifyMiddleware = - getAnnotation(service.modify, Middleware, app.container.reflector); + getAnnotation(service.modify, app.container.reflector); patch('/:id', (req, res) { return req.parseBody().then((_) { return this.modify( @@ -271,7 +271,7 @@ class Service extends Routable { (modifyMiddleware == null) ? [] : modifyMiddleware.handlers)); Middleware updateMiddleware = - getAnnotation(service.update, Middleware, app.container.reflector); + getAnnotation(service.update, app.container.reflector); post('/:id', (req, res) { return req.parseBody().then((_) { return this.update( @@ -306,7 +306,7 @@ class Service extends Routable { (updateMiddleware == null) ? [] : updateMiddleware.handlers)); Middleware removeMiddleware = - getAnnotation(service.remove, Middleware, app.container.reflector); + getAnnotation(service.remove, app.container.reflector); delete('/', (req, res) { return this.remove( null, diff --git a/lib/src/util.dart b/lib/src/util.dart index e3537a22..8ae812b8 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -2,26 +2,26 @@ import 'package:angel_container/angel_container.dart'; final RegExp straySlashes = new RegExp(r'(^/+)|(/+$)'); -matchingAnnotation(List metadata, Type T) { +T matchingAnnotation(List metadata) { for (ReflectedInstance metaDatum in metadata) { if (metaDatum.type.reflectedType == T) { - return metaDatum.reflectee; + return metaDatum.reflectee as T; } } return null; } -getAnnotation(obj, Type T, Reflector reflector) { +T getAnnotation(obj, Reflector reflector) { if (reflector == null) { return null; } else { if (obj is Function) { var methodMirror = reflector.reflectFunction(obj); - return matchingAnnotation(methodMirror.annotations, T); + return matchingAnnotation(methodMirror.annotations); } else { var classMirror = reflector.reflectClass(obj.runtimeType as Type); - return matchingAnnotation(classMirror.annotations, T); + return matchingAnnotation(classMirror.annotations); } } } diff --git a/test/controller_test.dart b/test/controller_test.dart index 72277a20..2a470a29 100644 --- a/test/controller_test.dart +++ b/test/controller_test.dart @@ -122,7 +122,7 @@ main() { expect(rgx.firstMatch(response.body)?.start, equals(0)); - Map todo = json.decode(response.body.replaceAll(rgx, "")); + var todo = json.decode(response.body.replaceAll(rgx, "")) as Map; print("Todo: $todo"); expect(todo['text'], equals("Hello")); expect(todo['over'], equals("world")); diff --git a/test/di_test.dart b/test/di_test.dart index 70c2dc3e..00888dc7 100644 --- a/test/di_test.dart +++ b/test/di_test.dart @@ -73,19 +73,19 @@ main() { test("make in route", () async { var response = await client.get("$url/errands3"); - String text = await json.decode(response.body); + var text = await json.decode(response.body) as String; expect(text, equals(TEXT)); }); test("make in controller", () async { var response = await client.get("$url/errands4"); - String text = await json.decode(response.body); + var text = await json.decode(response.body) as String; expect(text, equals(TEXT)); }); } void validateTodoSingleton(response) { - Map todo = json.decode(response.body.toString()); + var todo = json.decode(response.body.toString()) as Map; expect(todo["id"], equals(null)); expect(todo["text"], equals(TEXT)); expect(todo["over"], equals(OVER)); diff --git a/test/hooked_test.dart b/test/hooked_test.dart index bee62afb..a153ac29 100644 --- a/test/hooked_test.dart +++ b/test/hooked_test.dart @@ -77,7 +77,7 @@ main() { body: json.encode({"arbitrary": "data"}), headers: headers as Map); print(response.body); - Map result = json.decode(response.body); + var result = json.decode(response.body) as Map; expect(result["hello"], equals("hooked world")); }); @@ -95,7 +95,7 @@ main() { var response = await client.get("$url/todos"); print(response.body); - List result = json.decode(response.body); + var result = json.decode(response.body) as List; expect(result[0]["angel"], equals("framework")); });