Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dynamite): allow per spec configuration #1174

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/dynamite/dynamite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ targets:
- 'factory .*\.fromJson\(Map<String, dynamic> json\) => jsonSerializers\.deserializeWith\(serializer, json\)!;'
- 'Map<String, dynamic> toJson\(\) => jsonSerializers\.serializeWith\(serializer, this\)! as Map<String, dynamic>;'
- 'static BuiltSet<.*> get values => _\$.*Values;'
overrides:
# Tighten linting rules for specific specs
# Mark the generated library as experimental
lib/my_spec.openapi.json:
Leptopoda marked this conversation as resolved.
Show resolved Hide resolved
analyzer_ignores:
- camel_case_types
experimental: true

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,66 @@ abstract class DynamiteConfig implements Built<DynamiteConfig, DynamiteConfigBui

/// The specified line with the formatter should use.
int? get pageWidth;

/// A collection of dynamite configs for a specific file.
///
/// This parameter is only supported for the top level config.
// overrides is reserved in the build.yaml
BuiltMap<String, DynamiteConfig>? get overrides;

/// Whether the generated library should be marked with the `@experimental` annotation.
bool get experimental;

@BuiltValueHook(initializeBuilder: true)
static void _defaults(final DynamiteConfigBuilder b) {
b.experimental ??= false;
}

/// Gets the config for the given [uri].
///
/// Returns this if no override is set.
DynamiteConfig configFor(final String uri) {
if (overrides == null) {
return this;
}

DynamiteConfig? override;
for (final entry in overrides!.entries) {
if (entry.key == uri) {
override = entry.value;
break;
}
}

Leptopoda marked this conversation as resolved.
Show resolved Hide resolved
if (override == null) {
return this;
}

return merge(override);
}

/// Merges `this` config with the given [override].
///
/// Throws a [ArgumentError] if the override has overrides itself.
DynamiteConfig merge(final DynamiteConfig other) {
if (other.overrides != null) {
throw ArgumentError('Configs can only be merged with a config that does not have further overrides');
}

return rebuild((final b) {
final analyzerIgnores = other.analyzerIgnores;
if (analyzerIgnores != null) {
b.analyzerIgnores.replace(analyzerIgnores);
}

final coverageIgnores = other.coverageIgnores;
if (coverageIgnores != null) {
b.coverageIgnores.replace(coverageIgnores);
}

b.experimental = other.experimental;
});
}
}

@SerializersFor([
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions packages/dynamite/dynamite/lib/src/openapi_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class OpenAPIBuilder implements Builder {
}
}

try {
final inputId = buildStep.inputId;
final outputId = inputId.changeExtension('.dart');
final inputId = buildStep.inputId;
final outputId = inputId.changeExtension('.dart');

final emitter = DartEmitter(
orderDirectives: true,
useNullSafetySyntax: true,
);
final emitter = DartEmitter(
orderDirectives: true,
useNullSafetySyntax: true,
);

try {
final spec = switch (inputId.extension) {
'.json' => openapi.serializers.deserializeWith(
openapi.OpenAPI.serializer,
Expand All @@ -76,7 +76,8 @@ class OpenAPIBuilder implements Builder {
throw Exception('Only OpenAPI between $minSupportedVersion and $maxSupportedVersion are supported.');
}

final state = State(buildConfig);
final config = buildConfig.configFor(inputId.path);
final state = State(config);

final output = Library((final b) {
final analyzerIgnores = state.buildConfig.analyzerIgnores;
Expand Down Expand Up @@ -114,6 +115,12 @@ class OpenAPIBuilder implements Builder {
Directive.part(p.basename(outputId.changeExtension('.g.dart').path)),
);
}

if (state.buildConfig.experimental) {
b.annotations.add(
refer('experimental'),
);
}
});

var outputString = output.accept(emitter).toString();
Expand Down
7 changes: 7 additions & 0 deletions packages/nextcloud/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ targets:
- 'factory .*\.fromJson\(Map<String, dynamic> json\) => jsonSerializers\.deserializeWith\(serializer, json\)!;'
- 'Map<String, dynamic> toJson\(\) => jsonSerializers\.serializeWith\(serializer, this\)! as Map<String, dynamic>;'
- 'static BuiltSet<.*> get values => _\$.*Values;'
overrides:
lib/src/api/news.openapi.json:
experimental: true
lib/src/api/notes.openapi.json:
experimental: true
lib/src/api/uppush.openapi.json:
experimental: true
1 change: 1 addition & 0 deletions packages/nextcloud/lib/src/api/news.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

@experimental
library news_openapi;

import 'package:built_collection/built_collection.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/nextcloud/lib/src/api/notes.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

@experimental
library notes_openapi;

import 'dart:convert';
Expand Down
1 change: 1 addition & 0 deletions packages/nextcloud/lib/src/api/uppush.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case

@experimental
library uppush_openapi;

import 'package:built_collection/built_collection.dart';
Expand Down