Skip to content

Commit

Permalink
Make union type "unknown" private to force use of default.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmorgan committed Aug 12, 2024
1 parent 0b951a9 commit d9b1c73
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
14 changes: 4 additions & 10 deletions pkgs/_macro_client/lib/macro_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,10 @@ class MacroClient {
// part of the protocol+code, update implementation here and below.
}
final response = Response.fromJson(jsonData);
switch (response.type) {
case ResponseType.unknown:
// Ignore unknown response.
break;
default:
// TODO(davidmorgan): track requests and responses properly.
if (_responseCompleter != null) {
_responseCompleter!.complete(response);
_responseCompleter = null;
}
// TODO(davidmorgan): track requests and responses properly.
if (_responseCompleter != null) {
_responseCompleter!.complete(response);
_responseCompleter = null;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/_macro_server/lib/macro_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class MacroServer {
.forEach((line) {
final jsonData = json.decode(line) as Map<String, Object?>;
final request = MacroRequest.fromJson(jsonData);
if (request.type != MacroRequestType.unknown) {
if (request.type.isKnown) {
service
.handle(request)
.then((response) => socket.writeln(json.encode(response!.node)));
}
final response = Response.fromJson(jsonData);
if (response.type != ResponseType.unknown) {
if (response.type.isKnown) {
_responseCompleter!.complete(response);
_responseCompleter = null;
}
Expand Down
7 changes: 5 additions & 2 deletions pkgs/dart_model/lib/src/dart_model.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,13 @@ extension type Query.fromJson(Map<String, Object?> node) {
}

enum StaticTypeType {
unknown,
// Private so switches must have a default. See `isKnown`.
_unknown,
neverType,
nullableType,
voidType;

bool get isKnown => this != _unknown;
}

extension type StaticType.fromJson(Map<String, Object?> node) {
Expand All @@ -170,7 +173,7 @@ extension type StaticType.fromJson(Map<String, Object?> node) {
case 'VoidType':
return StaticTypeType.voidType;
default:
return StaticTypeType.unknown;
return StaticTypeType._unknown;
}
}

Expand Down
21 changes: 15 additions & 6 deletions pkgs/macro_service/lib/src/macro_service.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ extension type HostEndpoint.fromJson(Map<String, Object?> node) {
}

enum HostRequestType {
unknown,
// Private so switches must have a default. See `isKnown`.
_unknown,
augmentRequest;

bool get isKnown => this != _unknown;
}

extension type HostRequest.fromJson(Map<String, Object?> node) {
Expand All @@ -66,7 +69,7 @@ extension type HostRequest.fromJson(Map<String, Object?> node) {
case 'AugmentRequest':
return HostRequestType.augmentRequest;
default:
return HostRequestType.unknown;
return HostRequestType._unknown;
}
}

Expand Down Expand Up @@ -107,9 +110,12 @@ extension type MacroStartedResponse.fromJson(Map<String, Object?> node) {
}

enum MacroRequestType {
unknown,
// Private so switches must have a default. See `isKnown`.
_unknown,
macroStartedRequest,
queryRequest;

bool get isKnown => this != _unknown;
}

extension type MacroRequest.fromJson(Map<String, Object?> node) {
Expand All @@ -127,7 +133,7 @@ extension type MacroRequest.fromJson(Map<String, Object?> node) {
case 'QueryRequest':
return MacroRequestType.queryRequest;
default:
return MacroRequestType.unknown;
return MacroRequestType._unknown;
}
}

Expand Down Expand Up @@ -167,11 +173,14 @@ extension type QueryResponse.fromJson(Map<String, Object?> node) {
}

enum ResponseType {
unknown,
// Private so switches must have a default. See `isKnown`.
_unknown,
augmentResponse,
errorResponse,
macroStartedResponse,
queryResponse;

bool get isKnown => this != _unknown;
}

extension type Response.fromJson(Map<String, Object?> node) {
Expand All @@ -197,7 +206,7 @@ extension type Response.fromJson(Map<String, Object?> node) {
case 'QueryResponse':
return ResponseType.queryResponse;
default:
return ResponseType.unknown;
return ResponseType._unknown;
}
}

Expand Down
12 changes: 7 additions & 5 deletions tool/dart_model_generator/lib/generate_dart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ String _generateUnion(
// TODO(davidmorgan): add description(s).
result
..writeln('enum ${name}Type {')
..writeln(['unknown']
.followedBy(unionEntries.map((e) => _firstToLowerCase(e.$1)))
.join(', '))
..writeln(';}');
..writeln(' // Private so switches must have a default. See `isKnown`.')
..writeln('_unknown,')
..write(unionEntries.map((e) => _firstToLowerCase(e.$1)).join(', '))
..writeln(';')
..writeln('bool get isKnown => this != _unknown;')
..writeln('}');

// TODO(davidmorgan): add description.
result.writeln('extension type $name.fromJson(Map<String, Object?> node) {');
Expand All @@ -222,7 +224,7 @@ String _generateUnion(
result.writeln("case '$type': return ${name}Type.$lowerType;");
}
result
..writeln('default: return ${name}Type.unknown;')
..writeln('default: return ${name}Type._unknown;')
..writeln('}')
..writeln('}');

Expand Down

0 comments on commit d9b1c73

Please sign in to comment.