Skip to content

Commit 3163d35

Browse files
authored
Migrate impl to null safety and support latest source_gen dependency (#187)
1 parent 9d6cc9e commit 3163d35

14 files changed

+40
-40
lines changed

functions_framework/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ dev_dependencies:
2323
build_verify: ^2.0.0
2424
json_serializable: ^4.0.0
2525
# Because we use the ignore_for_file build.yaml config
26-
source_gen: ^0.9.5
26+
source_gen: ^1.0.0
2727
test: ^1.15.7

functions_framework_builder/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 0.4.0-dev
44

55
- Generate null-safe code.
6+
- Migrate implementation to null safety.
67
- Support the latest `package:functions_framework` version.
78
- Allow the latest, null-safe versions of a number of packages.
89

functions_framework_builder/lib/builder.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import 'src/constants.dart';
3333
import 'src/function_type_validator.dart';
3434
import 'src/supported_function_type.dart';
3535

36-
Builder functionsFrameworkBuilder([BuilderOptions options]) =>
36+
Builder functionsFrameworkBuilder([BuilderOptions? options]) =>
3737
const _FunctionsFrameworkBuilder();
3838

3939
class _FunctionsFrameworkBuilder implements Builder {
@@ -62,12 +62,10 @@ class _FunctionsFrameworkBuilder implements Builder {
6262
);
6363
}
6464

65-
final function = element as FunctionElement;
66-
6765
final targetReader = annotatedElement.annotation.read('target');
6866

6967
final targetName =
70-
targetReader.isNull ? function.name : targetReader.stringValue;
68+
targetReader.isNull ? element.name : targetReader.stringValue;
7169

7270
if (entries.containsKey(targetName)) {
7371
throw InvalidGenerationSourceError(
@@ -79,7 +77,7 @@ class _FunctionsFrameworkBuilder implements Builder {
7977
final invokeExpression = validator.validate(
8078
libraryElement,
8179
targetName,
82-
function,
80+
element,
8381
);
8482

8583
entries[targetName] = invokeExpression;

functions_framework_builder/lib/src/generic_function_type.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ class GenericFunctionType implements SupportedFunctionType {
4141
String get typedefName => _typedefName;
4242

4343
@override
44-
String get typeDescription => _functionTypeAliasElement.aliasedElement
44+
String get typeDescription => _functionTypeAliasElement.aliasedElement!
4545
.getDisplayString(withNullability: false);
4646

4747
final TypeAliasElement _functionTypeAliasElement;
4848
final bool _withContext;
4949

50-
GenericFunctionType._(this._functionTypeAliasElement, this._withContext)
51-
: assert(_functionTypeAliasElement != null);
50+
GenericFunctionType._(this._functionTypeAliasElement, this._withContext);
5251

5352
static Future<GenericFunctionType> create(Resolver resolver) async {
5453
final lib = await resolver.libraryFor(AssetId.resolve(_libraryUri));
@@ -71,7 +70,7 @@ class GenericFunctionType implements SupportedFunctionType {
7170
}
7271

7372
@override
74-
FactoryData createReference(
73+
FactoryData? createReference(
7574
LibraryElement library,
7675
String targetName,
7776
FunctionElement element,
@@ -101,11 +100,11 @@ class GenericFunctionType implements SupportedFunctionType {
101100

102101
if (library.typeSystem.isSubtypeOf(element.type, functionType)) {
103102
if (paramInfo.paramType != null) {
104-
if (library.exportNamespace.get(paramInfo.paramType.element.name) ==
103+
if (library.exportNamespace.get(paramInfo.paramType!.element.name) ==
105104
null) {
106105
// TODO: add a test for this!
107106
throw InvalidGenerationSourceError(
108-
'The type `${paramInfo.paramType.element.name}` is not exposed by '
107+
'The type `${paramInfo.paramType!.element.name}` is not exposed by '
109108
'the function library `${library.source.uri}` so it cannot '
110109
'be used.',
111110
);
@@ -152,7 +151,7 @@ class _GenericFactoryData implements FactoryData {
152151
final typeDisplayName = info.paramType == null
153152
? jsonTypeDisplay
154153
: '$functionsLibraryPrefix.'
155-
'${info.paramType.getDisplayString(withNullability: false)}';
154+
'${info.paramType!.getDisplayString(withNullability: false)}';
156155

157156
final returnBlock = info.paramType == null
158157
? 'return $_jsonParamName;'

functions_framework_builder/lib/src/supported_function_type.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class SupportedFunctionType {
2626
final String typeDescription;
2727

2828
final FunctionType _type;
29-
final String _constructor;
29+
final String? _constructor;
3030

3131
SupportedFunctionType._(
3232
this.libraryUri,
3333
this.typedefName,
3434
this._type, {
35-
String constructor,
35+
String? constructor,
3636
}) : _constructor = constructor,
3737
typeDescription = _type.getDisplayString(withNullability: false);
3838

@@ -62,7 +62,7 @@ class SupportedFunctionType {
6262
);
6363
}
6464

65-
FactoryData createReference(
65+
FactoryData? createReference(
6666
LibraryElement library,
6767
String targetName,
6868
FunctionElement element,

functions_framework_builder/lib/src/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ String escapeDartString(String value) {
2424
var canBeRaw = true;
2525

2626
value = value.replaceAllMapped(_escapeRegExp, (match) {
27-
final value = match[0];
27+
final value = match[0]!;
2828
if (value == "'") {
2929
hasSingleQuote = true;
3030
return value;

functions_framework_builder/lib/src/valid_json_utils.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import 'package:analyzer/dart/element/type.dart';
16+
import 'package:collection/collection.dart';
1617

1718
const fromJsonFactoryName = 'fromJson';
1819

@@ -49,17 +50,16 @@ bool _validJsonType(DartType type, bool allowComplexMembers) {
4950

5051
class JsonParamInfo {
5152
final DartType jsonType;
52-
final InterfaceType paramType;
53+
final InterfaceType? paramType;
5354

5455
JsonParamInfo._(this.jsonType, this.paramType);
5556
}
5657

57-
JsonParamInfo validJsonParamType(DartType type) {
58+
JsonParamInfo? validJsonParamType(DartType type) {
5859
// Look for a `fromJson` factory that takes a JSON-able type
5960
if (type is InterfaceType) {
60-
final fromJsonCtor = type.constructors.singleWhere(
61+
final fromJsonCtor = type.constructors.singleWhereOrNull(
6162
(element) => element.name == fromJsonFactoryName,
62-
orElse: () => null,
6363
);
6464
if (fromJsonCtor != null) {
6565
final requiredParams = fromJsonCtor.parameters
@@ -99,8 +99,7 @@ JsonReturnKind _validJsonReturnTypeCore(DartType type) {
9999

100100
// Look for a `toJson` function that returns a JSON-able type
101101
if (type is InterfaceType) {
102-
final interfaceType = type as InterfaceType;
103-
final toJsonMethod = interfaceType.getMethod('toJson');
102+
final toJsonMethod = type.getMethod('toJson');
104103
if (toJsonMethod != null &&
105104
toJsonMethod.parameters.every((element) => element.isOptional)) {
106105
type = toJsonMethod.returnType;

functions_framework_builder/pubspec.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ description: Builder for package:functions_framework
44
repository: https://github.com/GoogleCloudPlatform/functions-framework-dart
55

66
environment:
7-
sdk: ">=2.11.99 <3.0.0"
7+
sdk: ">=2.12.0 <3.0.0"
88

99
dependencies:
1010
analyzer: ^1.0.0
1111
build: ^2.0.0
1212
build_config: ^0.4.3
13-
dart_style: ^1.3.10
13+
collection: ^1.15.0
14+
dart_style: ^2.0.0
1415
# There is a tight version constraint because the builder has a strict
1516
# dependency on all features exposed.
1617
functions_framework: '>=0.4.0 <0.4.1'
1718
glob: ^2.0.0
1819
meta: ^1.2.4
1920
path: ^1.7.0
2021
shelf: ^1.0.0
21-
source_gen: ^0.9.8
22+
source_gen: ^1.0.0
2223

2324
dev_dependencies:
2425
build_test: ^2.0.0

functions_framework_builder/test/builder_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ $lines
185185
'(JsonType request, RequestContext context)',
186186
)
187187
.replaceAll(
188-
'int other',
189-
'RequestContext context, int other',
188+
'int? other',
189+
'RequestContext context, int? other',
190190
);
191191

192192
await _testItems(
@@ -445,8 +445,8 @@ $lines
445445
'(num request, RequestContext context)',
446446
)
447447
.replaceAll(
448-
'int other',
449-
'RequestContext context, int other',
448+
'int? other',
449+
'RequestContext context, int? other',
450450
);
451451

452452
await _testItems(
@@ -607,7 +607,7 @@ $entries
607607

608608
Future<void> _generateTest(
609609
String inputLibrary,
610-
String expectedContent, {
610+
String? expectedContent, {
611611
bool validateLog = true,
612612
}) async {
613613
final srcs = {'$_pkgName|lib/functions.dart': inputLibrary};
@@ -633,7 +633,7 @@ Future<void> _generateTest(
633633
}
634634
addTearDown(() {
635635
var output = 'Unexpected log message: "${log.message}"';
636-
if ((log.message == null || log.message.isEmpty) && log.error != null) {
636+
if ((log.message.isEmpty) && log.error != null) {
637637
output = '$output (${log.error})';
638638
}
639639
fail(output);

functions_framework_builder/test/test_examples/valid_cloud_event_handlers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ FutureOr<void> futureOrFunction(CloudEvent request) =>
2727
throw UnimplementedError();
2828

2929
@CloudFunction()
30-
void optionalParam([CloudEvent request]) => throw UnimplementedError();
30+
void optionalParam([CloudEvent? request]) => throw UnimplementedError();
3131

3232
@CloudFunction()
3333
void objectParam(Object request) => throw UnimplementedError();

0 commit comments

Comments
 (0)