Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmorgan committed Jul 22, 2024
1 parent e59d3b2 commit 52a7576
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
9 changes: 5 additions & 4 deletions pkgs/_macro_host/lib/macro_host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class MacroHost {
final MacroRunner macroRunner = MacroRunner();
late final HostEndpoint hostEndpoint;

MacroHost({required MacroHostService service})
: macroServer = MacroServer(service: service);
MacroHost._(this.macroServer, this.hostEndpoint);

Future<void> serve() async {
hostEndpoint = await macroServer.serve();
static Future<MacroHost> serve({required MacroHostService service}) async {
final server = MacroServer(service: service);
final endpoint = await server.serve();
return MacroHost._(server, endpoint);
}

// TODO(davidmorgan): methods for integration with analyzer+CFE go here:
Expand Down
4 changes: 1 addition & 3 deletions pkgs/_test_macros/lib/declare_x_macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class DeclareX {
// TODO(davidmorgan): this is a placeholder; make it do something, test it.
class DeclareXImplementation implements Macro {
@override
MacroDescription get description => MacroDescription(
runsInPhaseThree: true,
);
MacroDescription get description => MacroDescription(runsInPhases: [3]);

@override
Future<AugmentResponse> augment(Host host, AugmentRequest request) async {
Expand Down
18 changes: 4 additions & 14 deletions pkgs/macro_service/lib/src/macro_service.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,11 @@ extension type HostEndpoint.fromJson(Map<String, Object?> node) {
/// Information about a macro that the macro provides to the host.
extension type MacroDescription.fromJson(Map<String, Object?> node) {
MacroDescription({
bool? runsInPhaseOne,
bool? runsInPhaseTwo,
bool? runsInPhaseThree,
List<int>? runsInPhases,
}) : this.fromJson({
if (runsInPhaseOne != null) 'runsInPhaseOne': runsInPhaseOne,
if (runsInPhaseTwo != null) 'runsInPhaseTwo': runsInPhaseTwo,
if (runsInPhaseThree != null) 'runsInPhaseThree': runsInPhaseThree,
if (runsInPhases != null) 'runsInPhases': runsInPhases,
});

/// Whether the macro runs in phase one to produce types.
bool get runsInPhaseOne => node['runsInPhaseOne'] as bool;

/// Whether the macro runs in phase two to produce declarations.
bool get runsInPhaseTwo => node['runsInPhaseTwo'] as bool;

/// Whether the macro runs in phase three to produce definitions.
bool get runsInPhaseThree => node['runsInPhaseThree'] as bool;
/// Phases that the macro runs in: 1, 2 and/or 3.
List<int> get runsInPhases => (node['runsInPhases'] as List).cast();
}
17 changes: 6 additions & 11 deletions schemas/macro_service.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@
"type": "object",
"description": "Information about a macro that the macro provides to the host.",
"properties": {
"runsInPhaseOne": {
"description": "Whether the macro runs in phase one to produce types.",
"type": "boolean"
},
"runsInPhaseTwo": {
"description": "Whether the macro runs in phase two to produce declarations.",
"type": "boolean"
},
"runsInPhaseThree": {
"description": "Whether the macro runs in phase three to produce definitions.",
"type": "boolean"
"runsInPhases": {
"description": "Phases that the macro runs in: 1, 2 and/or 3.",
"type": "array",
"items": {
"type": "integer"
}
}
}
}
Expand Down
24 changes: 18 additions & 6 deletions tool/dart_model_generator/lib/generate_dart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,34 @@ PropertyMetadata _readPropertyMetadata(String name, JsonSchema schema) {
description: schema.description,
name: name,
type: PropertyType.list,
// `items` should be a type specified with a `$ref`.
elementTypeName: _readRefName(schema, 'items')),
elementTypeName: _readRefNameOrType(schema, 'items')),
SchemaType.object => PropertyMetadata(
description: schema.description,
name: name,
type: PropertyType.map,
// `additionalProperties` should be a type specified with a `$ref`.
elementTypeName: _readRefName(schema, 'additionalProperties')),
elementTypeName: _readRefNameOrType(schema, 'additionalProperties')),
_ => throw UnsupportedError('Unsupported schema type: ${schema.type}'),
};
}

/// Reads the type name of a `$ref` to a `$def`.
String _readRefName(JsonSchema schema, String key) {
final ref = (schema.schemaMap![key] as Map)[r'$ref'] as String;
return ref.substring(r'#/$defs/'.length);
///
/// If it's not there, falls back to `type` mapped to a Dart type name.
String _readRefNameOrType(JsonSchema schema, String key) {
final typeSchema = schema.schemaMap![key] as Map;
final ref = typeSchema[r'$ref'] as String?;
if (ref != null) {
return ref.substring(r'#/$defs/'.length);
} else {
final type = typeSchema['type'] as String;
switch (type) {
case 'integer':
return 'int';
default:
throw UnsupportedError(type);
}
}
}

/// The Dart types used in extension types to model JSON types.
Expand Down

0 comments on commit 52a7576

Please sign in to comment.