Fixed static analysis warnings

This commit is contained in:
thomashii@dukefirehawk.com 2022-08-27 17:46:16 +08:00
parent c52bc61e49
commit 3fad3ee34b
3 changed files with 12 additions and 18 deletions

View file

@ -3,6 +3,8 @@
## 7.0.0 ## 7.0.0
* Require Dart >= 2.17 * Require Dart >= 2.17
* [Breaking] Updated `CacheAccessLevel` to enhanced Enum. Changed `PUBLIC` to `public` and `PRIVATE` to `private`.
* [Breaking] Removed `accessLevelToString` as the functionality is provided by enhanced Enum
## 6.0.0 ## 6.0.0

View file

@ -5,25 +5,13 @@ import 'package:file/file.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'virtual_directory.dart'; import 'virtual_directory.dart';
/// Returns a string representation of the given [CacheAccessLevel].
String accessLevelToString(CacheAccessLevel accessLevel) {
switch (accessLevel) {
case CacheAccessLevel.PRIVATE:
return 'private';
case CacheAccessLevel.PUBLIC:
return 'public';
default:
throw ArgumentError('Unrecognized cache access level: $accessLevel');
}
}
/// A `VirtualDirectory` that also sets `Cache-Control` headers. /// A `VirtualDirectory` that also sets `Cache-Control` headers.
class CachingVirtualDirectory extends VirtualDirectory { class CachingVirtualDirectory extends VirtualDirectory {
final _log = Logger('CachingVirtualDirectory'); final _log = Logger('CachingVirtualDirectory');
final Map<String, String> _etags = {}; final Map<String, String> _etags = {};
/// Either `PUBLIC` or `PRIVATE`. /// Either `public` or `private`.
final CacheAccessLevel accessLevel; final CacheAccessLevel accessLevel;
/// If `true`, responses will always have `private, max-age=0` as their `Cache-Control` header. /// If `true`, responses will always have `private, max-age=0` as their `Cache-Control` header.
@ -41,7 +29,7 @@ class CachingVirtualDirectory extends VirtualDirectory {
final int maxAge; final int maxAge;
CachingVirtualDirectory(Angel app, FileSystem fileSystem, CachingVirtualDirectory(Angel app, FileSystem fileSystem,
{this.accessLevel = CacheAccessLevel.PUBLIC, {this.accessLevel = CacheAccessLevel.public,
Directory? source, Directory? source,
bool debug = false, bool debug = false,
Iterable<String> indexFileNames = const ['index.html'], Iterable<String> indexFileNames = const ['index.html'],
@ -181,7 +169,7 @@ class CachingVirtualDirectory extends VirtualDirectory {
void setCachedHeaders( void setCachedHeaders(
DateTime modified, RequestContext req, ResponseContext res) { DateTime modified, RequestContext req, ResponseContext res) {
var privacy = accessLevelToString(accessLevel); var privacy = accessLevel.level;
res.headers res.headers
..['cache-control'] = '$privacy, max-age=$maxAge' ..['cache-control'] = '$privacy, max-age=$maxAge'
@ -194,5 +182,10 @@ class CachingVirtualDirectory extends VirtualDirectory {
} }
} }
// TODO: Refactor enum CacheAccessLevel {
enum CacheAccessLevel { PUBLIC, PRIVATE } public('public'),
private('private');
const CacheAccessLevel(this.level);
final String level;
}

View file

@ -5,7 +5,6 @@ import 'package:angel3_static/angel3_static.dart';
import 'package:file/local.dart'; import 'package:file/local.dart';
import 'package:http/http.dart' show Client; import 'package:http/http.dart' show Client;
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:matcher/matcher.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {