Skip to content

Commit 42ec8d4

Browse files
committed
wip on only maps!
1 parent 3163d35 commit 42ec8d4

File tree

3 files changed

+39
-102
lines changed

3 files changed

+39
-102
lines changed

functions_framework_builder/lib/src/valid_json_utils.dart

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,8 @@ bool _validJsonType(DartType type, bool allowComplexMembers) {
2727
return memberType.isDynamic || memberType.isDartCoreObject;
2828
}
2929

30-
if (type.isDartCoreBool ||
31-
type.isDartCoreNum ||
32-
type.isDartCoreDouble ||
33-
type.isDartCoreInt ||
34-
type.isDartCoreString) {
35-
return true;
36-
}
37-
3830
if (type is InterfaceType) {
39-
if (type.isDartCoreList) {
40-
final arg = type.typeArguments.single;
41-
return validCollectionMember(arg);
42-
} else if (type.isDartCoreMap) {
31+
if (type.isDartCoreMap) {
4332
final keyArg = type.typeArguments[0];
4433
final valueArg = type.typeArguments[1];
4534
return keyArg.isDartCoreString && validCollectionMember(valueArg);

functions_framework_builder/test/builder_test.dart

Lines changed: 29 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ $lines
226226

227227
test('simple return type', () async {
228228
final newInputContent = inputContent
229-
.replaceAll('void ', 'int ')
230-
.replaceAll('<void>', '<int>');
229+
.replaceAll('void ', 'Map<String, dynamic> ')
230+
.replaceAll('<void>', '<Map<String, dynamic>>');
231231

232232
await _testItems(
233233
newInputContent,
@@ -266,48 +266,8 @@ $lines
266266

267267
test('JSON return type', () async {
268268
final newInputContent = inputContent
269-
.replaceAll('void ', 'int ')
270-
.replaceAll('<void>', '<int>');
271-
272-
await _testItems(
273-
newInputContent,
274-
[
275-
'syncFunction',
276-
'asyncFunction',
277-
'futureOrFunction',
278-
'extraParam',
279-
'optionalParam',
280-
],
281-
(e) => """
282-
case '$e':
283-
return JsonFunctionTarget(
284-
function_library.$e,
285-
(json) {
286-
if (json is Map<String, dynamic>) {
287-
try {
288-
return function_library.JsonType.fromJson(json);
289-
} catch (e, stack) {
290-
throw BadRequestException(
291-
400,
292-
'There was an error parsing the provided JSON data.',
293-
innerError: e,
294-
innerStack: stack,
295-
);
296-
}
297-
}
298-
throw BadRequestException(
299-
400,
300-
'The provided JSON is not the expected type '
301-
'`Map<String, dynamic>`.',
302-
);
303-
},
304-
);""");
305-
});
306-
307-
test('complex return type', () async {
308-
final newInputContent = inputContent
309-
.replaceAll('void ', 'Map<String, List<JsonType>> ')
310-
.replaceAll('<void>', '<Map<String, List<JsonType>>>');
269+
.replaceAll('void ', 'Map<String, dynamic> ')
270+
.replaceAll('<void>', '<Map<String, dynamic>>');
311271

312272
await _testItems(
313273
newInputContent,
@@ -365,53 +325,22 @@ $lines
365325
return JsonFunctionTarget.voidResult(
366326
function_library.$e,
367327
(json) {
368-
if (json is num) {
328+
if (json is Map<String, dynamic>) {
369329
return json;
370330
}
371331
throw BadRequestException(
372332
400,
373333
'The provided JSON is not the expected type '
374-
'`num`.',
334+
'`Map<String, dynamic>`.',
375335
);
376336
},
377337
);""");
378338
});
379339

380340
test('simple return type', () async {
381341
final newInputContent = inputContent
382-
.replaceAll('void ', 'int ')
383-
.replaceAll('<void>', '<int>');
384-
await _testItems(
385-
newInputContent,
386-
[
387-
'syncFunction',
388-
'asyncFunction',
389-
'futureOrFunction',
390-
'extraParam',
391-
'optionalParam',
392-
],
393-
(e) => """
394-
case '$e':
395-
return JsonFunctionTarget(
396-
function_library.$e,
397-
(json) {
398-
if (json is num) {
399-
return json;
400-
}
401-
throw BadRequestException(
402-
400,
403-
'The provided JSON is not the expected type '
404-
'`num`.',
405-
);
406-
},
407-
);""");
408-
});
409-
410-
test('complex return type', () async {
411-
final newInputContent = inputContent
412-
.replaceAll('void ', 'Map<String, List<bool>> ')
413-
.replaceAll('<void>', '<Map<String, List<bool>>>');
414-
342+
.replaceAll('void ', 'Map<String, dynamic> ')
343+
.replaceAll('<void>', '<Map<String, dynamic>>');
415344
await _testItems(
416345
newInputContent,
417346
[
@@ -426,13 +355,13 @@ $lines
426355
return JsonFunctionTarget(
427356
function_library.$e,
428357
(json) {
429-
if (json is num) {
358+
if (json is Map<String, dynamic>) {
430359
return json;
431360
}
432361
throw BadRequestException(
433362
400,
434363
'The provided JSON is not the expected type '
435-
'`num`.',
364+
'`Map<String, dynamic>`.',
436365
);
437366
},
438367
);""");
@@ -441,8 +370,8 @@ $lines
441370
test('void with context', () async {
442371
final newInputContent = inputContent
443372
.replaceAll(
444-
'(num request)',
445-
'(num request, RequestContext context)',
373+
'(Map<String, dynamic> request)',
374+
'(Map<String, dynamic> request, RequestContext context)',
446375
)
447376
.replaceAll(
448377
'int? other',
@@ -463,13 +392,13 @@ $lines
463392
return JsonWithContextFunctionTarget.voidResult(
464393
function_library.$e,
465394
(json) {
466-
if (json is num) {
395+
if (json is Map<String, dynamic>) {
467396
return json;
468397
}
469398
throw BadRequestException(
470399
400,
471400
'The provided JSON is not the expected type '
472-
'`num`.',
401+
'`Map<String, dynamic>`.',
473402
);
474403
},
475404
);""");
@@ -556,6 +485,21 @@ package:$_pkgName/functions.dart:8:10
556485
// Custom and JSON event types
557486
//
558487
'Duration handleGet(DateTime request) => null;': notCompatibleMatcher,
488+
489+
//
490+
// dart:core types that aren't `Map<String, dynamic>`
491+
//
492+
// Map param is under-specified
493+
'Map<String, dynamic> handleGet(Map request) => null;':
494+
notCompatibleMatcher,
495+
'int handleGet(Map<String, dynamic> request) => null;':
496+
notCompatibleMatcher,
497+
'Map<String, dynamic> handleGet(int request) => null;':
498+
notCompatibleMatcher,
499+
// Map return type is under-specified
500+
'Map handleGet(Map<String, dynamic> request) => null;':
501+
notCompatibleMatcher,
502+
'int handleGet(int request) => null;': notCompatibleMatcher,
559503
};
560504

561505
for (var shape in invalidShapes.entries) {

functions_framework_builder/test/test_examples/valid_json_type_handlers.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ import 'dart:async';
1717
import 'package:functions_framework/functions_framework.dart';
1818

1919
@CloudFunction()
20-
void syncFunction(num request) => throw UnimplementedError();
20+
void syncFunction(Map<String, dynamic> request) => throw UnimplementedError();
2121

2222
@CloudFunction()
23-
Future<void> asyncFunction(num request) => throw UnimplementedError();
23+
Future<void> asyncFunction(Map<String, dynamic> request) =>
24+
throw UnimplementedError();
2425

2526
@CloudFunction()
26-
FutureOr<void> futureOrFunction(num request) => throw UnimplementedError();
27+
FutureOr<void> futureOrFunction(Map<String, dynamic> request) =>
28+
throw UnimplementedError();
2729

2830
@CloudFunction()
29-
void extraParam(num request, [int? other]) => throw UnimplementedError();
31+
void extraParam(Map<String, dynamic> request, [int? other]) =>
32+
throw UnimplementedError();
3033

3134
@CloudFunction()
32-
void optionalParam([num? request, int? other]) => throw UnimplementedError();
35+
void optionalParam([Map<String, dynamic>? request, int? other]) =>
36+
throw UnimplementedError();

0 commit comments

Comments
 (0)