Skip to content

Commit 2db1109

Browse files
authored
Tiny cleanup (#172)
Inspired by experiments with null-safety migration
1 parent 36198df commit 2db1109

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

functions_framework/lib/src/function_config.dart

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,36 +124,29 @@ class FunctionConfig {
124124
port = defaults?.port ?? defaultPort;
125125
}
126126

127+
final functionTypeOptionValue = options[_functionTypeOpt] as String;
128+
127129
return FunctionConfig(
128130
port: port,
129131
target: options[_targetOpt] as String ??
130132
defaults?.target ??
131133
defaultFunctionTarget,
132-
functionType: _parseFunctionType(options[_functionTypeOpt] as String) ??
133-
defaults?.functionType ??
134-
defaultFunctionType,
134+
functionType: functionTypeOptionValue == null
135+
? defaults?.functionType ?? defaultFunctionType
136+
: _parseFunctionType(functionTypeOptionValue),
135137
);
136138
}
137139
}
138140

139-
FunctionType _parseFunctionType(String type) {
140-
type ??= '';
141-
switch (type) {
142-
case '':
143-
return defaultFunctionType;
144-
case 'http':
145-
return FunctionType.http;
146-
case 'cloudevent':
147-
return FunctionType.cloudevent;
148-
default:
149-
throw BadConfigurationException(
141+
FunctionType _parseFunctionType(String type) => FunctionType.values.singleWhere(
142+
(element) => type == _enumValue(element),
143+
orElse: () => throw BadConfigurationException(
150144
'FUNCTION_SIGNATURE_TYPE environment variable "$type" is not a valid '
151145
'option (must be "http" or "cloudevent")',
152-
);
153-
}
154-
}
146+
),
147+
);
155148

156-
String _enumValue(Object enumEntry) {
149+
String _enumValue(FunctionType enumEntry) {
157150
final v = enumEntry.toString();
158151
return v.substring(v.indexOf('.') + 1);
159152
}

functions_framework/lib/src/json_request_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ MediaType mediaTypeFromRequest(Request request) {
2525
throw BadRequestException(400, '$contentTypeHeader header is required.');
2626
}
2727
try {
28-
return MediaType.parse(request.headers[contentTypeHeader]);
28+
return MediaType.parse(contentType);
2929
} catch (e, stack) {
3030
throw BadRequestException(
3131
400,

0 commit comments

Comments
 (0)