From a613a17686d6607c92943b6f76e3e99623ccafbf Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Sun, 29 Jan 2023 19:19:01 +0100 Subject: [PATCH] format --- .../test/schema/scalars_test.dart | 13 +++- codegen/gql_build/lib/src/schema_builder.dart | 18 +++-- .../gql_build/lib/src/serializer_builder.dart | 22 ++++-- codegen/gql_build/lib/src/var_builder.dart | 10 ++- codegen/gql_code_builder/lib/schema.dart | 3 +- .../gql_code_builder/lib/src/built_class.dart | 8 +- codegen/gql_code_builder/lib/src/common.dart | 6 +- codegen/gql_code_builder/lib/src/schema.dart | 12 +-- .../lib/src/schema/input.dart | 4 +- codegen/gql_code_builder/lib/var.dart | 76 +++++++++++-------- .../src/operation/definitions/selections.dart | 1 - links/gql_exec/lib/src/value.dart | 4 +- links/gql_link/lib/src/link.dart | 1 - 13 files changed, 109 insertions(+), 69 deletions(-) diff --git a/codegen/end_to_end_test/test/schema/scalars_test.dart b/codegen/end_to_end_test/test/schema/scalars_test.dart index 6eab24ad..bf7e70f7 100644 --- a/codegen/end_to_end_test/test/schema/scalars_test.dart +++ b/codegen/end_to_end_test/test/schema/scalars_test.dart @@ -50,8 +50,10 @@ void main() { final scalar = GISODate(isoString); test('correctly serializes and deserializes', () { - expect(serializers.deserializeWith(GISODate.serializer, isoString), equals(scalar)); - expect(serializers.serializeWith(GISODate.serializer, scalar), equals(isoString)); + expect(serializers.deserializeWith(GISODate.serializer, isoString), + equals(scalar)); + expect(serializers.serializeWith(GISODate.serializer, scalar), + equals(isoString)); }); }); @@ -102,8 +104,11 @@ void main() { final data = GReviewWithDateData( (b) => b ..createReview.stars = 1 - ..createReview.seenOn.add(DateTime.fromMillisecondsSinceEpoch(1591892597000)) - ..createReview.createdAt = DateTime.fromMillisecondsSinceEpoch(1591892597000), + ..createReview + .seenOn + .add(DateTime.fromMillisecondsSinceEpoch(1591892597000)) + ..createReview.createdAt = + DateTime.fromMillisecondsSinceEpoch(1591892597000), ); test('correctly overrides scalars in data types', () { diff --git a/codegen/gql_build/lib/src/schema_builder.dart b/codegen/gql_build/lib/src/schema_builder.dart index c7e2cfca..d85096f9 100644 --- a/codegen/gql_build/lib/src/schema_builder.dart +++ b/codegen/gql_build/lib/src/schema_builder.dart @@ -15,7 +15,8 @@ class SchemaBuilder implements Builder { final EnumFallbackConfig enumFallbackConfig; final bool generatePossibleTypesMap; - SchemaBuilder(this.typeOverrides, this.enumFallbackConfig, this.generatePossibleTypesMap); + SchemaBuilder(this.typeOverrides, this.enumFallbackConfig, + this.generatePossibleTypesMap); @override Map> get buildExtensions => { @@ -26,10 +27,13 @@ class SchemaBuilder implements Builder { FutureOr build(BuildStep buildStep) async { final doc = await readDocument(buildStep); - final generatedPartUrl = - buildStep.inputId.changeExtension(generatedFileExtension(schemaExtension)).uri.path; + final generatedPartUrl = buildStep.inputId + .changeExtension(generatedFileExtension(schemaExtension)) + .uri + .path; - final schemaUrl = outputAssetId(buildStep.inputId, schemaExtension).uri.toString(); + final schemaUrl = + outputAssetId(buildStep.inputId, schemaExtension).uri.toString(); final allocator = GqlAllocator( buildStep.inputId.uri.toString(), outputAssetId(buildStep.inputId, schemaExtension).uri.toString(), @@ -38,8 +42,10 @@ class SchemaBuilder implements Builder { final library = buildSchemaLibrary( doc, basename(generatedPartUrl), typeOverrides, enumFallbackConfig, - generatePossibleTypesMap: generatePossibleTypesMap, allocator: allocator); + generatePossibleTypesMap: generatePossibleTypesMap, + allocator: allocator); - return writeDocument(library, buildStep, schemaExtension, schemaUrl, allocator); + return writeDocument( + library, buildStep, schemaExtension, schemaUrl, allocator); } } diff --git a/codegen/gql_build/lib/src/serializer_builder.dart b/codegen/gql_build/lib/src/serializer_builder.dart index b9d49762..16d60280 100644 --- a/codegen/gql_build/lib/src/serializer_builder.dart +++ b/codegen/gql_build/lib/src/serializer_builder.dart @@ -29,9 +29,10 @@ class SerializerBuilder implements Builder { static final _generatedFiles = Glob("lib/**.gql.dart"); // create a path for the serializers output in same directory as schema - List get pathSegments => outputAssetId(schemaId, serializerExtension).pathSegments - ..removeLast() - ..add("serializers.gql.dart"); + List get pathSegments => + outputAssetId(schemaId, serializerExtension).pathSegments + ..removeLast() + ..add("serializers.gql.dart"); @override Map> get buildExtensions => { @@ -51,22 +52,27 @@ class SerializerBuilder implements Builder { /// BuiltValue classes with serializers. These will be added automatically /// using `@SerializersFor`. - final builtClasses = SplayTreeSet((a, b) => a.name.compareTo(b.name)); + final builtClasses = + SplayTreeSet((a, b) => a.name.compareTo(b.name)); /// Non BuiltValue classes with serializers (i.e. inline fragment classes). /// These need to be added manually since `@SerializersFor` only recognizes /// BuiltValue classes. - final nonBuiltClasses = SplayTreeSet((a, b) => a.name.compareTo(b.name)); + final nonBuiltClasses = + SplayTreeSet((a, b) => a.name.compareTo(b.name)); final hasSerializer = (ClassElement c) => c.fields.any((field) => field.isStatic && field.name == "serializer" && field.type.element?.name == "Serializer" && - field.type.element?.source?.uri.toString() == "package:built_value/serializer.dart"); + field.type.element?.source?.uri.toString() == + "package:built_value/serializer.dart"); final isBuiltValue = (ClassElement c) => c.allSupertypes.any((interface) => - (interface.element.name == "Built" || interface.element.name == "EnumClass") && - interface.element.source.uri.toString() == "package:built_value/built_value.dart"); + (interface.element.name == "Built" || + interface.element.name == "EnumClass") && + interface.element.source.uri.toString() == + "package:built_value/built_value.dart"); await for (final input in buildStep.findAssets(_generatedFiles)) { final lib = await buildStep.resolver.libraryFor(input); diff --git a/codegen/gql_build/lib/src/var_builder.dart b/codegen/gql_build/lib/src/var_builder.dart index c6f9c4d0..8733b6b5 100644 --- a/codegen/gql_build/lib/src/var_builder.dart +++ b/codegen/gql_build/lib/src/var_builder.dart @@ -30,8 +30,10 @@ class VarBuilder implements Builder { final doc = await readDocument(buildStep); final schema = await readDocument(buildStep, schemaId); - final generatedPartUrl = - buildStep.inputId.changeExtension(generatedFileExtension(varExtension)).uri.path; + final generatedPartUrl = buildStep.inputId + .changeExtension(generatedFileExtension(varExtension)) + .uri + .path; final schemaUrl = outputAssetId(schemaId, schemaExtension).uri.toString(); final allocator = GqlAllocator( @@ -40,8 +42,8 @@ class VarBuilder implements Builder { schemaUrl, ); - final library = buildVarLibrary( - doc, addTypenames(schema), basename(generatedPartUrl), typeOverrides, allocator); + final library = buildVarLibrary(doc, addTypenames(schema), + basename(generatedPartUrl), typeOverrides, allocator); return writeDocument( library, diff --git a/codegen/gql_code_builder/lib/schema.dart b/codegen/gql_code_builder/lib/schema.dart index 71b22952..99da105a 100644 --- a/codegen/gql_code_builder/lib/schema.dart +++ b/codegen/gql_code_builder/lib/schema.dart @@ -42,7 +42,8 @@ Code buildPossibleTypes(DocumentNode document) { // wrap the map in a literal for codegen final possibleTypesLiteral = literalMap(possibleTypesMap); // assign the literal to a const variable named "possibleTypes" - return declareConst("possibleTypesMap", type: Reference("Map>")) + return declareConst("possibleTypesMap", + type: Reference("Map>")) .assign(possibleTypesLiteral) .statement; } diff --git a/codegen/gql_code_builder/lib/src/built_class.dart b/codegen/gql_code_builder/lib/src/built_class.dart index 48f75e34..9b92906e 100644 --- a/codegen/gql_code_builder/lib/src/built_class.dart +++ b/codegen/gql_code_builder/lib/src/built_class.dart @@ -78,10 +78,10 @@ Class builtClass({ ), if (getters != null) ...getters, // Serialization methods - if(!hasCustomSerializer) - buildSerializerGetter(className).rebuild( - (b) => b..body = Code("_\$${toCamelCase(className)}Serializer"), - ), + if (!hasCustomSerializer) + buildSerializerGetter(className).rebuild( + (b) => b..body = Code("_\$${toCamelCase(className)}Serializer"), + ), buildToJsonGetter( className, isOverride: superclassSelections.isNotEmpty, diff --git a/codegen/gql_code_builder/lib/src/common.dart b/codegen/gql_code_builder/lib/src/common.dart index ce9d5d4e..41fafe63 100644 --- a/codegen/gql_code_builder/lib/src/common.dart +++ b/codegen/gql_code_builder/lib/src/common.dart @@ -203,7 +203,8 @@ Method buildOptionalGetter({ ..isNullable = true ..url = "package:gql_exec/value.dart" ..symbol = "Value" - ..types.add((baseGetter.returns as TypeReference).rebuild((b3) => b3..isNullable = false)))); + ..types.add((baseGetter.returns as TypeReference) + .rebuild((b3) => b3..isNullable = false)))); return optionalGetter; } @@ -263,5 +264,6 @@ Method buildFromJsonGetter(String className) => Method( ..lambda = true ..body = refer("serializers", "#serializer") .property("deserializeWith") - .call([refer(className).property("serializer"), refer("json")]).code, + .call( + [refer(className).property("serializer"), refer("json")]).code, ); diff --git a/codegen/gql_code_builder/lib/src/schema.dart b/codegen/gql_code_builder/lib/src/schema.dart index 90307d4d..0b9d5f23 100644 --- a/codegen/gql_code_builder/lib/src/schema.dart +++ b/codegen/gql_code_builder/lib/src/schema.dart @@ -32,8 +32,8 @@ class _SchemaBuilderVisitor extends SimpleVisitor?> { final EnumFallbackConfig enumFallbackConfig; final Allocator allocator; - _SchemaBuilderVisitor( - this.schemaSource, this.typeOverrides, this.enumFallbackConfig, this.allocator); + _SchemaBuilderVisitor(this.schemaSource, this.typeOverrides, + this.enumFallbackConfig, this.allocator); @override List visitDocumentNode( @@ -58,8 +58,8 @@ class _SchemaBuilderVisitor extends SimpleVisitor?> { schemaSource, typeOverrides, ); - final serializer = - nullAwareJsonSerializerClass(inputClass, allocator, schemaSource, typeOverrides); + final serializer = nullAwareJsonSerializerClass( + inputClass, allocator, schemaSource, typeOverrides); return [inputClass, serializer]; } @@ -67,7 +67,9 @@ class _SchemaBuilderVisitor extends SimpleVisitor?> { List visitScalarTypeDefinitionNode( ScalarTypeDefinitionNode node, ) => - typeOverrides.containsKey(node.name.value) ? [] : [buildScalarClass(node)]; + typeOverrides.containsKey(node.name.value) + ? [] + : [buildScalarClass(node)]; @override List visitEnumTypeDefinitionNode( diff --git a/codegen/gql_code_builder/lib/src/schema/input.dart b/codegen/gql_code_builder/lib/src/schema/input.dart index b42997fb..4ce965d8 100644 --- a/codegen/gql_code_builder/lib/src/schema/input.dart +++ b/codegen/gql_code_builder/lib/src/schema/input.dart @@ -19,8 +19,8 @@ List buildInputClasses( schemaSource, typeOverrides, ); - final serializer = - nullAwareJsonSerializerClass(inputClass, allocator, schemaSource, typeOverrides); + final serializer = nullAwareJsonSerializerClass( + inputClass, allocator, schemaSource, typeOverrides); return [inputClass, serializer]; }).toList(); diff --git a/codegen/gql_code_builder/lib/var.dart b/codegen/gql_code_builder/lib/var.dart index 2e5ca1e1..a8bb4a49 100644 --- a/codegen/gql_code_builder/lib/var.dart +++ b/codegen/gql_code_builder/lib/var.dart @@ -33,13 +33,15 @@ Library buildVarLibrary( .toList(); Map _fragmentMap(SourceNode source) => { - for (var def in source.document.definitions.whereType()) + for (var def + in source.document.definitions.whereType()) def.name.value: def, for (var import in source.imports) ..._fragmentMap(import) }; - final fragmentVarClasses = - docSource.document.definitions.whereType().map((frag) { + final fragmentVarClasses = docSource.document.definitions + .whereType() + .map((frag) { final varTypes = fragmentVarTypes( fragment: frag, fragmentMap: _fragmentMap(docSource), @@ -86,18 +88,19 @@ Library buildVarLibrary( ); } -Method nullAwareJsonSerializerField(Node op, String className) => Method((b) => b - ..annotations - .add(CodeExpression(Code("BuiltValueSerializer(custom: true, serializeNulls: true)"))) - ..static = true - ..type = MethodType.getter - ..lambda = true - ..returns = TypeReference((b2) => b2 - ..symbol = "Serializer" - ..url = "package:built_value/serializer.dart" - ..types.add(refer(className))) - ..name = "serializer" - ..body = Code("${className}Serializer()")); +Method nullAwareJsonSerializerField(Node op, String className) => + Method((b) => b + ..annotations.add(CodeExpression( + Code("BuiltValueSerializer(custom: true, serializeNulls: true)"))) + ..static = true + ..type = MethodType.getter + ..lambda = true + ..returns = TypeReference((b2) => b2 + ..symbol = "Serializer" + ..url = "package:built_value/serializer.dart" + ..types.add(refer(className))) + ..name = "serializer" + ..body = Code("${className}Serializer()")); Class nullAwareJsonSerializerClass( Class base, @@ -131,7 +134,8 @@ Class nullAwareJsonSerializerClass( ..returns = refer("Iterable") ..requiredParameters.add(Parameter((b) => b ..name = "serializers" - ..type = refer("Serializers", "package:built_value/serializer.dart"))) + ..type = + refer("Serializers", "package:built_value/serializer.dart"))) ..requiredParameters.add(Parameter((b) => b ..name = "object" ..type = refer(base.name))) @@ -140,13 +144,15 @@ Class nullAwareJsonSerializerClass( ..named = true ..type = refer("FullType", "package:built_value/serializer.dart") ..defaultTo = Code("FullType.unspecified"))) - ..body = _serializerBody(base, allocator, schemaSource, typeOverrides)), + ..body = + _serializerBody(base, allocator, schemaSource, typeOverrides)), Method((b) => b ..name = "deserialize" ..returns = refer(base.name) ..requiredParameters.add(Parameter((b) => b ..name = "serializers" - ..type = refer("Serializers", "package:built_value/serializer.dart"))) + ..type = + refer("Serializers", "package:built_value/serializer.dart"))) ..requiredParameters.add(Parameter((b) => b ..name = "serialized" ..type = refer("Iterable"))) @@ -155,20 +161,22 @@ Class nullAwareJsonSerializerClass( ..named = true ..type = refer("FullType", "package:built_value/serializer.dart") ..defaultTo = Code("FullType.unspecified"))) - ..body = _deserializerBody(base, allocator, schemaSource, typeOverrides)), + ..body = + _deserializerBody(base, allocator, schemaSource, typeOverrides)), ])); Code _serializerBody(Class base, Allocator allocator, SourceNode schemaSource, Map typeOverrides) { final vars = []; - for (final field - in base.methods.where((field) => !field.static && field.type == MethodType.getter)) { + for (final field in base.methods + .where((field) => !field.static && field.type == MethodType.getter)) { final isOptionalValue = isValue(field.returns!); final statements = []; if (isOptionalValue) { - final realType = (field.returns as TypeReference).types.first as TypeReference; + final realType = + (field.returns as TypeReference).types.first as TypeReference; final _valueVarName = "_\$${field.name}value"; @@ -221,7 +229,9 @@ String _generateFieldDeserializers( SourceNode schemaSource, Map typeOverrides, ) => - clazz.methods.where((field) => field.type == MethodType.getter && !field.static).map((field) { + clazz.methods + .where((field) => field.type == MethodType.getter && !field.static) + .map((field) { var type = field.returns!; final isWrappedValue = isValue(type); if (isWrappedValue) { @@ -229,14 +239,18 @@ String _generateFieldDeserializers( } final fullType = _generateFullType(type as TypeReference, allocator); - final typeDefNode = getTypeDefinitionNode(schemaSource.document, type.symbol.substring(1)); + final typeDefNode = getTypeDefinitionNode( + schemaSource.document, type.symbol.substring(1)); - print(typeDefNode.runtimeType.toString() + " " + (typeDefNode?.name.value.toString() ?? "")); + print(typeDefNode.runtimeType.toString() + + " " + + (typeDefNode?.name.value.toString() ?? "")); //TODO this feels flaky, find a better way final isBuilder = type.url != null && !isWrappedValue && - (typeDefNode is! ScalarTypeDefinitionNode && typeDefNode is! EnumTypeDefinitionNode); + (typeDefNode is! ScalarTypeDefinitionNode && + typeDefNode is! EnumTypeDefinitionNode); /// TODO check for wireName @@ -289,9 +303,11 @@ bool isValue(Reference ref) { } String _getWireName(Method m) { - final annotation = - m.annotations.firstWhereOrNull((a) => a is InvokeExpression && a.name == "BuiltValueField") - as InvokeExpression?; + final annotation = m.annotations.firstWhereOrNull( + (a) => a is InvokeExpression && a.name == "BuiltValueField") + as InvokeExpression?; if (annotation == null) return m.name!; - return (annotation.namedArguments["wireName"] as LiteralExpression?)?.literal ?? m.name!; + return (annotation.namedArguments["wireName"] as LiteralExpression?) + ?.literal ?? + m.name!; } diff --git a/gql/lib/src/operation/definitions/selections.dart b/gql/lib/src/operation/definitions/selections.dart index eab6af9e..451607aa 100644 --- a/gql/lib/src/operation/definitions/selections.dart +++ b/gql/lib/src/operation/definitions/selections.dart @@ -56,7 +56,6 @@ abstract class Selection extends ExecutableWithResolver { static Selection fromNode( SelectionNode astNode, [ - /// The [schemaType] of the containing element TypeDefinition? schemaType, GetExecutableType? getType, diff --git a/links/gql_exec/lib/src/value.dart b/links/gql_exec/lib/src/value.dart index 0ab0b96a..c4b49ba2 100644 --- a/links/gql_exec/lib/src/value.dart +++ b/links/gql_exec/lib/src/value.dart @@ -11,7 +11,9 @@ class Value { @override bool operator ==(Object other) => identical(this, other) || - other is Value && runtimeType == other.runtimeType && _value == other._value; + other is Value && + runtimeType == other.runtimeType && + _value == other._value; @override int get hashCode => _value.hashCode; diff --git a/links/gql_link/lib/src/link.dart b/links/gql_link/lib/src/link.dart index 2e1f355c..73af9085 100644 --- a/links/gql_link/lib/src/link.dart +++ b/links/gql_link/lib/src/link.dart @@ -89,7 +89,6 @@ abstract class Link { Stream request( /// An incoming [Request] Request request, [ - /// Function that invokes the [request] function of /// the next [Link] ///