Skip to content

Commit 067c661

Browse files
authored
Mustachio: Add support for Iterable types in Property, some fixes (#2447)
1 parent e4e16af commit 067c661

File tree

8 files changed

+832
-469
lines changed

8 files changed

+832
-469
lines changed

lib/src/generator/templates.renderers.dart

Lines changed: 533 additions & 343 deletions
Large diffs are not rendered by default.

lib/src/mustachio/renderer_base.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class RendererBase<T> {
2323
/// Returns the [Property] on this renderer named [name].
2424
///
2525
/// If no property named [name] exists for this renderer, `null` is returned.
26-
Property getProperty(String key);
26+
Property<T> getProperty(String key);
2727

2828
/// Resolves [names] into one or more field accesses, returning the result as
2929
/// a String.
@@ -97,10 +97,20 @@ class Property<T> {
9797
/// object [context].
9898
final bool /*!*/ Function(T context) /*?*/ getBool;
9999

100-
// TODO(srawlins): Add functions for rendering Iterable properties and other
101-
// properties.
100+
final bool /*!*/ Function(T) /*?*/ isEmptyIterable;
102101

103-
Property({@required this.getValue, this.getProperties, this.getBool});
102+
final String /*!*/ Function(
103+
T, RendererBase<T>, List<MustachioNode> /*!*/) /*?*/
104+
renderIterable;
105+
106+
// TODO(srawlins): Add functions for rendering other properties.
107+
108+
Property(
109+
{@required this.getValue,
110+
this.getProperties,
111+
this.getBool,
112+
this.isEmptyIterable,
113+
this.renderIterable});
104114
}
105115

106116
/// An error indicating that a renderer failed to resolve a key.

test/mustachio/builder_test.dart

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ abstract class FooBase {
7777
class Foo extends FooBase {
7878
String s1 = "s1";
7979
bool b1 = false;
80+
List<int> l1 = [1, 2, 3];
8081
}
8182
class Bar {}
8283
''');
@@ -119,29 +120,50 @@ class Bar {}
119120
});
120121

121122
test('with a property map', () {
122-
expect(generatedContent,
123-
contains('static Map<String, Property> propertyMap() => {'));
123+
expect(
124+
generatedContent,
125+
contains(
126+
'static Map<String, Property<CT_>> propertyMap<CT_ extends Foo>() => {'));
124127
});
125128

126129
test('with a property map with a String property', () {
127130
expect(generatedContent, contains('''
128131
's1': Property(
129-
getValue: (Object c) => (c as Foo).s1,
132+
getValue: (CT_ c) => c.s1,
130133
getProperties: _Renderer_String.propertyMap,
131134
),
132135
'''));
133136
});
134137

135138
test('with a property map which references the superclass', () {
136-
expect(generatedContent, contains('..._Renderer_FooBase.propertyMap(),'));
139+
expect(generatedContent,
140+
contains('..._Renderer_FooBase.propertyMap<CT_>(),'));
137141
});
138142

139143
test('with a property map with a bool property', () {
140144
expect(generatedContent, contains('''
141145
'b1': Property(
142-
getValue: (Object c) => (c as Foo).b1,
146+
getValue: (CT_ c) => c.b1,
143147
getProperties: _Renderer_bool.propertyMap,
144-
getBool: (Object c) => (c as Foo).b1 == true,
148+
getBool: (CT_ c) => c.b1 == true,
149+
),
150+
'''));
151+
});
152+
153+
test('with a property map with an Iterable property', () {
154+
expect(generatedContent, contains('''
155+
'l1': Property(
156+
getValue: (CT_ c) => c.l1,
157+
getProperties: _Renderer_List.propertyMap,
158+
isEmptyIterable: (CT_ c) => c.l1?.isEmpty ?? true,
159+
renderIterable:
160+
(CT_ c, RendererBase<CT_> r, List<MustachioNode> ast) {
161+
var buffer = StringBuffer();
162+
for (var e in c.l1) {
163+
buffer.write(_render_int(e, ast, parent: r));
164+
}
165+
return buffer.toString();
166+
},
145167
),
146168
'''));
147169
});
@@ -202,15 +224,15 @@ import 'package:mustachio/annotations.dart';
202224
test(
203225
'with a property map which references the superclass with a type '
204226
'variable', () {
205-
expect(
206-
generatedContent, contains('..._Renderer_FooBase.propertyMap<T>(),'));
227+
expect(generatedContent,
228+
contains('..._Renderer_FooBase.propertyMap<T, CT_>(),'));
207229
});
208230

209231
test(
210232
'with a property map which references the superclass with an interface '
211233
'type', () {
212234
expect(generatedContent,
213-
contains('..._Renderer_BarBase.propertyMap<int>(),'));
235+
contains('..._Renderer_BarBase.propertyMap<int, CT_>(),'));
214236
});
215237
});
216238

test/mustachio/foo.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ library dartdoc.testing.foo;
44
import 'package:dartdoc/src/mustachio/annotations.dart';
55

66
class Foo {
7-
String s1 = 's1';
8-
bool b1 = true;
7+
String s1;
8+
bool b1;
9+
List<int> l1;
910
}

0 commit comments

Comments
 (0)