Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #165 from viniciusmelquiades/deprecated-field
Browse files Browse the repository at this point in the history
Add deprecated annotations in fields
  • Loading branch information
comigor authored Jul 10, 2020
2 parents e4a1ee3 + 296a8d0 commit 0cb309c
Show file tree
Hide file tree
Showing 21 changed files with 1,113 additions and 84 deletions.
9 changes: 6 additions & 3 deletions .github/actions/check-version-and-changelog/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ github_ref="$2"
PR_HREF=$(cat "$GITHUB_EVENT_PATH" | jq -r '.pull_request._links.self.href')

function send_message_and_bail {
if [ ! -z "$REPO_TOKEN" ]; then
ERROR="$1"
ERROR="$1"

if [ ! -z "$REPO_TOKEN" ]; then
jq -c -n --arg body "$ERROR" '{"event":"COMMENT", "body":$body}' > /tmp/payload.json
curl -f -X POST \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $REPO_TOKEN" \
--data "@/tmp/payload.json" \
"$PR_HREF/reviews" -vv
"$PR_HREF/reviews" -vv || true
fi

echo "------------------------------------------------"
echo "$ERROR"
echo "------------------------------------------------"
exit 1
}

Expand Down
13 changes: 8 additions & 5 deletions .github/actions/dart-test/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ cd "$GITHUB_WORKSPACE"
PR_HREF=$(cat "$GITHUB_EVENT_PATH" | jq -r '.pull_request._links.self.href')

function send_message_and_bail {
if [ ! -z "$REPO_TOKEN" ]; then
ERROR="$1"
DETAIL="$2"
# DETAIL=$(echo "$2" | sed -zE 's/\n/\\n/g')
ERROR="$1"
DETAIL="$2"

if [ ! -z "$REPO_TOKEN" ]; then
BODY=$(cat <<EOF
$ERROR
Expand All @@ -41,9 +40,13 @@ EOF
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $REPO_TOKEN" \
--data "@/tmp/payload.json" \
"$PR_HREF/reviews" -vv
"$PR_HREF/reviews" -vv || true
fi

echo "------------------------------------------------"
echo "$ERROR"
echo "$DETAIL"
echo "------------------------------------------------"
exit 1
}

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 6.5.0-beta.1
- Add deprecated annotations in fields.

## 6.4.4-beta.1
- Build type name recursively, considering casing changes.

Expand Down
48 changes: 43 additions & 5 deletions lib/generator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:artemis/generator/data/data.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:build/build.dart';
import 'package:meta/meta.dart';
import 'package:gql/ast.dart';
Expand All @@ -19,7 +20,9 @@ void _log(Context context, int align, Object logObject) {
}

/// Enum value for values not mapped in the GraphQL enum
final EnumValue ARTEMIS_UNKNOWN = EnumValue(name: 'ARTEMIS_UNKNOWN');
final EnumValueDefinition ARTEMIS_UNKNOWN = EnumValueDefinition(
name: EnumValueName(name: 'ARTEMIS_UNKNOWN'),
);

/// Generate queries definitions from a GraphQL schema and a list of queries,
/// given Artemis options and schema mappings.
Expand Down Expand Up @@ -331,7 +334,7 @@ Make sure your query is correct and your schema is updated.''');

if (fieldType is! ListTypeNode) {
annotations.add(
'JsonKey(unknownEnumValue: ${dartTypeName.namePrintable}.${ARTEMIS_UNKNOWN.namePrintable})');
'JsonKey(unknownEnumValue: ${dartTypeName.namePrintable}.${ARTEMIS_UNKNOWN.name.namePrintable})');
}
}

Expand All @@ -341,6 +344,11 @@ Make sure your query is correct and your schema is updated.''');
annotations.add('JsonKey(name: \'${name.name}\')');
}

final fieldDirectives =
regularField?.directives ?? regularInputField?.directives;

annotations.addAll(_proceedDeprecated(fieldDirectives));

return ClassProperty(
type: dartTypeName,
name: name,
Expand All @@ -349,6 +357,31 @@ Make sure your query is correct and your schema is updated.''');
);
}

List<String> _proceedDeprecated(
List<DirectiveNode> directives,
) {
final annotations = <String>[];

final deprecatedDirective = directives?.firstWhere(
(directive) => directive.name.value == 'deprecated',
orElse: () => null,
);

if (deprecatedDirective != null) {
final reasonValueNode = deprecatedDirective?.arguments
?.firstWhere((argument) => argument.name.value == 'reason')
?.value;

final reason = reasonValueNode is StringValueNode
? reasonValueNode.value
: 'No longer supported';

annotations.add("Deprecated('$reason')");
}

return annotations;
}

class _GeneratorVisitor extends RecursiveVisitor {
_GeneratorVisitor({
@required this.context,
Expand Down Expand Up @@ -497,7 +530,7 @@ class _GeneratorVisitor extends RecursiveVisitor {
context.usedEnums.add(EnumName(name: leafType.name.value));
if (leafType is! ListTypeNode) {
annotations.add(
'JsonKey(unknownEnumValue: ${EnumName(name: dartTypeName.name).namePrintable}.${ARTEMIS_UNKNOWN.namePrintable})');
'JsonKey(unknownEnumValue: ${EnumName(name: dartTypeName.name).namePrintable}.${ARTEMIS_UNKNOWN.name.namePrintable})');
}
} else if (leafType is InputObjectTypeDefinitionNode) {
addUsedInputObjectsAndEnums(leafType);
Expand Down Expand Up @@ -633,8 +666,13 @@ class _CanonicalVisitor extends RecursiveVisitor {

enums.add(EnumDefinition(
name: enumName,
values: node.values.map((eV) => EnumValue(name: eV.name.value)).toList()
..add(ARTEMIS_UNKNOWN),
values: node.values
.map((ev) => EnumValueDefinition(
name: EnumValueName(name: ev.name.value),
annotations: _proceedDeprecated(ev.directives),
))
.toList()
..add(ARTEMIS_UNKNOWN),
));
}

Expand Down
20 changes: 2 additions & 18 deletions lib/generator/data/enum_definition.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'package:artemis/generator/data/definition.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:artemis/generator/data_printer.dart';
import 'package:artemis/generator/helpers.dart';
import 'package:meta/meta.dart';
import 'package:recase/recase.dart';

/// Define a Dart enum parsed from GraphQL schema.
class EnumDefinition extends Definition with DataPrinter {
@override
final EnumName name;

/// The possible values of this enum.
final Iterable<EnumValue> values;
final Iterable<EnumValueDefinition> values;

/// Instantiate an enum definition.
EnumDefinition({
Expand All @@ -26,22 +26,6 @@ class EnumDefinition extends Definition with DataPrinter {
};
}

/// Enum value name
class EnumValue extends Name with DataPrinter {
/// Instantiate a enum value name definition.
EnumValue({@required String name}) : super(name: name);

@override
Map<String, Object> get namedProps => {
'name': name,
};

@override
String normalize(String name) {
return ReCase(super.normalize(name)).camelCase;
}
}

/// Enum name
class EnumName extends Name with DataPrinter {
/// Instantiate a enum name definition.
Expand Down
41 changes: 41 additions & 0 deletions lib/generator/data/enum_value_definition.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:artemis/generator/data/definition.dart';
import 'package:artemis/generator/data_printer.dart';
import 'package:meta/meta.dart';
import 'package:recase/recase.dart';

/// Enum value
class EnumValueDefinition extends Definition with DataPrinter {
@override
final EnumValueName name;

/// Some other custom annotation.
final List<String> annotations;

/// Instantiate an enum value
EnumValueDefinition({
@required this.name,
this.annotations = const [],
});

@override
Map<String, Object> get namedProps => {
'name': name,
'annotations': annotations,
};
}

/// Enum value name
class EnumValueName extends Name with DataPrinter {
/// Instantiate a enum value name definition.
EnumValueName({@required String name}) : super(name: name);

@override
Map<String, Object> get namedProps => {
'name': name,
};

@override
String normalize(String name) {
return ReCase(super.normalize(name)).camelCase;
}
}
11 changes: 10 additions & 1 deletion lib/generator/print_helpers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:artemis/generator/data/data.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
import 'package:gql_code_gen/gql_code_gen.dart' as dart;
Expand All @@ -8,9 +9,17 @@ import '../generator/helpers.dart';
/// Generates a [Spec] of a single enum definition.
Spec enumDefinitionToSpec(EnumDefinition definition) =>
CodeExpression(Code('''enum ${definition.name.namePrintable} {
${definition.values.removeDuplicatedBy((i) => i).map((v) => '@JsonValue("${v.name}")${v.namePrintable}, ').join()}
${definition.values.removeDuplicatedBy((i) => i).map(_enumValueToSpec).join()}
}'''));

String _enumValueToSpec(EnumValueDefinition value) {
final annotations = value.annotations
.map((annotation) => '@$annotation')
.followedBy(['@JsonValue("${value.name.name}")']).join(' ');

return '$annotations${value.name.namePrintable}, ';
}

String _fromJsonBody(ClassDefinition definition) {
final buffer = StringBuffer();
buffer.writeln(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 6.4.4-beta.1
version: 6.5.0-beta.1

authors:
- Igor Borges <igor@borges.dev>
Expand Down
41 changes: 30 additions & 11 deletions test/generator/print_helpers_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:artemis/generator/data/data.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:artemis/generator/print_helpers.dart';
import 'package:gql/language.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -27,10 +28,18 @@ void main() {

test('It will generate an Enum declaration.', () {
final definition = EnumDefinition(name: EnumName(name: 'Name'), values: [
EnumValue(name: 'Option'),
EnumValue(name: 'anotherOption'),
EnumValue(name: 'third_option'),
EnumValue(name: 'FORTH_OPTION'),
EnumValueDefinition(
name: EnumValueName(name: 'Option'),
),
EnumValueDefinition(
name: EnumValueName(name: 'anotherOption'),
),
EnumValueDefinition(
name: EnumValueName(name: 'third_option'),
),
EnumValueDefinition(
name: EnumValueName(name: 'FORTH_OPTION'),
),
]);

final str = specToString(enumDefinitionToSpec(definition));
Expand All @@ -50,10 +59,18 @@ void main() {

test('It will ignore duplicate options.', () {
final definition = EnumDefinition(name: EnumName(name: 'Name'), values: [
EnumValue(name: 'Option'),
EnumValue(name: 'AnotherOption'),
EnumValue(name: 'Option'),
EnumValue(name: 'AnotherOption'),
EnumValueDefinition(
name: EnumValueName(name: 'Option'),
),
EnumValueDefinition(
name: EnumValueName(name: 'AnotherOption'),
),
EnumValueDefinition(
name: EnumValueName(name: 'Option'),
),
EnumValueDefinition(
name: EnumValueName(name: 'AnotherOption'),
),
]);

final str = specToString(enumDefinitionToSpec(definition));
Expand Down Expand Up @@ -637,9 +654,11 @@ class TestQueryArguments extends JsonSerializable with EquatableMixin {
operationName: 'test_query',
document: parseString('query test_query {}'),
classes: [
EnumDefinition(
name: EnumName(name: 'Enum'),
values: [EnumValue(name: 'Value')]),
EnumDefinition(name: EnumName(name: 'Enum'), values: [
EnumValueDefinition(
name: EnumValueName(name: 'Value'),
)
]),
ClassDefinition(name: ClassName(name: 'AClass'), properties: [])
],
),
Expand Down
13 changes: 10 additions & 3 deletions test/query_generator/aliases/alias_on_leaves_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:artemis/generator/data/data.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:test/test.dart';

import '../../helpers.dart';
Expand Down Expand Up @@ -52,9 +53,15 @@ final LibraryDefinition libraryDefinition =
operationName: r'some_query',
classes: [
EnumDefinition(name: EnumName(name: r'MyEnum'), values: [
EnumValue(name: r'A'),
EnumValue(name: r'B'),
EnumValue(name: r'ARTEMIS_UNKNOWN')
EnumValueDefinition(
name: EnumValueName(name: r'A'),
),
EnumValueDefinition(
name: EnumValueName(name: r'B'),
),
EnumValueDefinition(
name: EnumValueName(name: r'ARTEMIS_UNKNOWN'),
),
]),
ClassDefinition(
name: ClassName(name: r'SomeQuery$_Response$_SomeObject'),
Expand Down
Loading

0 comments on commit 0cb309c

Please sign in to comment.