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

Type hierarchy use enum #122

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions pkgs/dart_model/lib/src/dart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import 'scopes.dart';

export 'dart_model.g.dart';

extension DeclarationExtension on Declaration {
Member? get maybeMember =>
declarationType == DeclarationType.member ? Member.fromJson(node) : null;
Interface? get maybeInterface => declarationType == DeclarationType.interface
? Interface.fromJson(node)
: null;
}

extension QualifiedNameExtension on QualifiedName {
String get asString =>
'$uri#${scope == null ? '' : '$scope${isStatic! ? '::' : '.'}'}$name';
Expand Down
59 changes: 41 additions & 18 deletions pkgs/dart_model/lib/src/dart_model.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,58 @@ extension type MetadataAnnotation.fromJson(Map<String, Object?> node)
QualifiedName get type => node['type'] as QualifiedName;
}

/// Interface type for all declarations
extension type Declaration._(Map<String, Object?> node) implements Object {
/// The metadata annotations attached to this declaration.
List<MetadataAnnotation> get metadataAnnotations =>
(node['metadataAnnotations'] as List).cast();

/// The properties of this declaration.
Properties get properties => node['properties'] as Properties;

/// The type of declaration
DeclarationType get declarationType =>
node['declarationType'] as DeclarationType;
}

/// Declaration type.
extension type const DeclarationType.fromJson(String string) implements Object {
static const DeclarationType interface =
DeclarationType.fromJson('interface');
static const DeclarationType member = DeclarationType.fromJson('member');
}

/// An interface.
extension type Interface.fromJson(Map<String, Object?> node) implements Object {
extension type Interface.fromJson(Map<String, Object?> node)
implements Declaration {
static final TypedMapSchema _schema = TypedMapSchema({
'metadataAnnotations': Type.closedListPointer,
'members': Type.growableMapPointer,
'thisType': Type.typedMapPointer,
'metadataAnnotations': Type.closedListPointer,
'properties': Type.typedMapPointer,
'declarationType': Type.stringPointer,
});
Interface({
List<MetadataAnnotation>? metadataAnnotations,
NamedTypeDesc? thisType,
List<MetadataAnnotation>? metadataAnnotations,
Properties? properties,
DeclarationType? declarationType,
}) : this.fromJson(Scope.createMap(
_schema,
metadataAnnotations,
Scope.createGrowableMap(),
thisType,
metadataAnnotations,
properties,
// TODO(davidmorgan): this is a manual edit!
'interface',
));

/// The metadata annotations attached to this interface.
List<MetadataAnnotation> get metadataAnnotations =>
(node['metadataAnnotations'] as List).cast();

/// Map of members by name.
Map<String, Member> get members =>
(node['members'] as Map).cast<String, Member>();

/// The type of the expression `this` when used in this interface.
NamedTypeDesc get thisType => node['thisType'] as NamedTypeDesc;

/// The properties of this interface.
Properties get properties => node['properties'] as Properties;
}

/// Library.
Expand All @@ -189,32 +208,36 @@ extension type Library.fromJson(Map<String, Object?> node) implements Object {
}

/// Member of a scope.
extension type Member.fromJson(Map<String, Object?> node) implements Object {
extension type Member.fromJson(Map<String, Object?> node)
implements Declaration {
static final TypedMapSchema _schema = TypedMapSchema({
'properties': Type.typedMapPointer,
'returnType': Type.typedMapPointer,
'requiredPositionalParameters': Type.closedListPointer,
'optionalPositionalParameters': Type.closedListPointer,
'namedParameters': Type.closedListPointer,
'metadataAnnotations': Type.closedListPointer,
'properties': Type.typedMapPointer,
'declarationType': Type.stringPointer,
});
Member({
Properties? properties,
StaticTypeDesc? returnType,
List<StaticTypeDesc>? requiredPositionalParameters,
List<StaticTypeDesc>? optionalPositionalParameters,
List<NamedFunctionTypeParameter>? namedParameters,
List<MetadataAnnotation>? metadataAnnotations,
Properties? properties,
}) : this.fromJson(Scope.createMap(
_schema,
properties,
returnType,
requiredPositionalParameters,
optionalPositionalParameters,
namedParameters,
metadataAnnotations,
properties,
// TODO(davidmorgan): this is a manual edit!
'member',
));

/// The properties of this member.
Properties get properties => node['properties'] as Properties;

/// The return type of this member, if it has one.
StaticTypeDesc get returnType => node['returnType'] as StaticTypeDesc;

Expand Down
37 changes: 35 additions & 2 deletions pkgs/dart_model/test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void main() {
])
..members['_root'] = Member(
properties: Properties(isField: true, isStatic: false),
namedParameters: [],
)));
});
});
Expand All @@ -44,10 +45,13 @@ void main() {
],
'members': {
'_root': {
'properties': {'isField': true, 'isStatic': false}
'properties': {'isField': true, 'isStatic': false},
'declarationType': 'member',
'namedParameters': <Map<String, Object?>>[],
}
},
'properties': {'isClass': true}
'properties': {'isClass': true},
'declarationType': 'interface',
}
}
}
Expand Down Expand Up @@ -214,6 +218,35 @@ void main() {
expect(copiedModel.qualifiedNameOf(member.node), null);
expect(model.qualifiedNameOf(copiedMember.node), null);
});

test('Declaration', () {
Scope.macro.run(() {
MacroScope.current.addModel(model);
final interface = model
.uris['package:dart_model/dart_model.dart']!.scopes['JsonData']!;
final member = interface.members['_root']!;

expect(member.declarationType, DeclarationType.member);
expect(interface.declarationType, DeclarationType.interface);

String produceOutput(Declaration declaration) {
final maybeMember = declaration.maybeMember;
final maybeInterface = declaration.maybeInterface;
return [
'declaration:${declaration.properties.toString()}',
if (maybeMember != null)
'member:${maybeMember.namedParameters.toString()}',
if (maybeInterface != null)
'interface:${maybeInterface.members.length}',
].toString();
}

expect(produceOutput(member),
'[declaration:{isField: true, isStatic: false}, member:[]]');
expect(produceOutput(interface),
'[declaration:{isClass: true}, interface:1]');
});
});
});

group('QualifiedName', () {
Expand Down
43 changes: 31 additions & 12 deletions schemas/dart_model.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,14 @@
}
}
},
"DeclarationType": {
"type": "string",
"description": "Declaration type."
},
"Interface": {
"type": "object",
"description": "An interface.",
"properties": {
"metadataAnnotations": {
"type": "array",
"description": "The metadata annotations attached to this interface.",
"items": {
"$ref": "#/$defs/MetadataAnnotation"
}
},
"members": {
"type": "object",
"description": "Map of members by name.",
Expand All @@ -113,9 +110,20 @@
"$comment": "The type of the expression `this` when used in this interface.",
"$ref": "#/$defs/NamedTypeDesc"
},
"metadataAnnotations": {
"type": "array",
"description": "The metadata annotations attached to this declaration.",
"items": {
"$ref": "#/$defs/MetadataAnnotation"
}
},
"properties": {
"$comment": "The properties of this interface.",
"$comment": "The properties of this declaration.",
"$ref": "#/$defs/Properties"
},
"declarationType": {
"$comment": "The type of declaration",
"$ref": "#/$defs/DeclarationType"
}
}
},
Expand All @@ -136,10 +144,6 @@
"type": "object",
"description": "Member of a scope.",
"properties": {
"properties": {
"$comment": "The properties of this member.",
"$ref": "#/$defs/Properties"
},
"returnType": {
"$comment": "The return type of this member, if it has one.",
"$ref": "#/$defs/StaticTypeDesc"
Expand All @@ -164,6 +168,21 @@
"items": {
"$ref": "#/$defs/NamedFunctionTypeParameter"
}
},
"metadataAnnotations": {
"type": "array",
"description": "The metadata annotations attached to this declaration.",
"items": {
"$ref": "#/$defs/MetadataAnnotation"
}
},
"properties": {
"$comment": "The properties of this declaration.",
"$ref": "#/$defs/Properties"
},
"declarationType": {
"$comment": "The type of declaration",
"$ref": "#/$defs/DeclarationType"
}
}
},
Expand Down
36 changes: 26 additions & 10 deletions tool/dart_model_generator/lib/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,41 @@ static Protocol handshakeProtocol = Protocol(
type: 'QualifiedName',
description: 'The type of the annotation.'),
]),
Definition.clazz('Declaration',
description: 'Interface type for all declarations',
interfaceOnly: true,
properties: [
Property('metadataAnnotations',
type: 'List<MetadataAnnotation>',
description:
'The metadata annotations attached to this declaration.'),
Property('properties',
type: 'Properties',
description: 'The properties of this declaration.'),
Property('declarationType',
type: 'DeclarationType',
description: 'The type of declaration'),
]),
Definition.$enum(
'DeclarationType',
description: 'Declaration type.',
values: ['interface', 'member'],
),
Definition.clazz(
'Interface',
description: 'An interface.',
createInBuffer: true,
implements: [
'Declaration',
],
properties: [
Property('metadataAnnotations',
type: 'List<MetadataAnnotation>',
description:
'The metadata annotations attached to this interface.'),
Property('members',
type: 'Map<Member>', description: 'Map of members by name.'),
Property('thisType',
type: 'NamedTypeDesc',
description:
'The type of the expression `this` when used in this '
'interface.'),
Property('properties',
type: 'Properties',
description: 'The properties of this interface.'),
],
),
Definition.clazz('Library',
Expand All @@ -141,10 +157,10 @@ static Protocol handshakeProtocol = Protocol(
Definition.clazz('Member',
description: 'Member of a scope.',
createInBuffer: true,
implements: [
'Declaration',
],
properties: [
Property('properties',
type: 'Properties',
description: 'The properties of this member.'),
Property(
'returnType',
type: 'StaticTypeDesc',
Expand Down
Loading
Loading