Skip to content

Commit d563967

Browse files
committed
make 'extensions' as nested maps
1 parent 426a6e9 commit d563967

File tree

13 files changed

+94
-56
lines changed

13 files changed

+94
-56
lines changed

analysis_options.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ linter:
55
- always_declare_return_types
66
- always_put_control_body_on_new_line
77
- always_put_required_named_parameters_first
8-
- always_require_non_null_named_parameters
8+
# - always_require_non_null_named_parameters
99
# - always_specify_types
1010
- annotate_overrides
1111
- avoid_annotating_with_dynamic
@@ -26,8 +26,8 @@ linter:
2626
- avoid_relative_lib_imports
2727
- avoid_renaming_method_parameters
2828
- avoid_return_types_on_setters
29-
- avoid_returning_null
30-
- avoid_returning_null_for_future
29+
# - avoid_returning_null
30+
# - avoid_returning_null_for_future
3131
- avoid_returning_null_for_void
3232
- avoid_returning_this
3333
- avoid_setters_without_getters

lib/src/gpx_reader.dart

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,39 @@ class GpxReader {
379379
return string;
380380
}
381381

382+
Object? _readMap(Iterator<XmlEvent> iterator, String tagName) {
383+
final elm = iterator.current;
384+
if (!(elm is XmlStartElementEvent &&
385+
elm.name == tagName &&
386+
!elm.isSelfClosing)) {
387+
return null;
388+
}
389+
390+
final valueMap = <String, Object>{};
391+
String? valueText;
392+
while (iterator.moveNext()) {
393+
final val = iterator.current;
394+
395+
if (val is XmlStartElementEvent) {
396+
valueMap[val.name] = _readMap(iterator, val.name) ?? {};
397+
}
398+
399+
if (val is XmlTextEvent) {
400+
valueText = val.value;
401+
}
402+
403+
if (val is XmlCDATAEvent) {
404+
valueText = val.value;
405+
}
406+
407+
if (val is XmlEndElementEvent && val.name == tagName) {
408+
break;
409+
}
410+
}
411+
412+
return valueMap.isNotEmpty ? valueMap : valueText;
413+
}
414+
382415
Trkseg _readSegment(Iterator<XmlEvent> iterator) {
383416
final trkseg = Trkseg();
384417
final elm = iterator.current;
@@ -407,31 +440,9 @@ class GpxReader {
407440
return trkseg;
408441
}
409442

410-
Map<String, String> _readExtensions(Iterator<XmlEvent> iterator) {
411-
final exts = <String, String>{};
412-
final elm = iterator.current;
413-
414-
/*if (elm is XmlStartElementEvent) {
415-
link.href = elm.attributes
416-
.firstWhere((attr) => attr.name == GpxTagV11.href)
417-
.value;
418-
}*/
419-
420-
if ((elm is XmlStartElementEvent) && !elm.isSelfClosing) {
421-
while (iterator.moveNext()) {
422-
final val = iterator.current;
423-
424-
if (val is XmlStartElementEvent) {
425-
exts[val.name] = _readString(iterator, val.name) ?? '';
426-
}
427-
428-
if (val is XmlEndElementEvent && val.name == GpxTagV11.extensions) {
429-
break;
430-
}
431-
}
432-
}
433-
434-
return exts;
443+
Map<String, Object> _readExtensions(Iterator<XmlEvent> iterator) {
444+
final exts = _readMap(iterator, GpxTagV11.extensions) ?? {};
445+
return (exts is Map<String, Object>) ? exts : {};
435446
}
436447

437448
Link _readLink(Iterator<XmlEvent> iterator) {

lib/src/gpx_writer.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class GpxWriter {
191191
}
192192
}
193193

194-
void _writeExtensions(XmlBuilder builder, Map<String, String>? value) {
194+
void _writeExtensions(XmlBuilder builder, Map<String, Object>? value) {
195195
if (value != null && value.isNotEmpty) {
196196
builder.element(GpxTagV11.extensions, nest: () {
197197
value.forEach((k, v) {
@@ -216,7 +216,15 @@ class GpxWriter {
216216

217217
void _writeElement(XmlBuilder builder, String tagName, Object? value) {
218218
if (value != null) {
219-
builder.element(tagName, nest: value);
219+
if (value is Map) {
220+
builder.element(tagName, nest: () {
221+
value.forEach((k, v) {
222+
_writeElement(builder, k, v);
223+
});
224+
});
225+
} else {
226+
builder.element(tagName, nest: value);
227+
}
220228
}
221229
}
222230

lib/src/model/gpx.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Gpx {
3232

3333
/// You can add extend GPX by adding your own elements from another schema
3434
/// here.
35-
Map<String, String> extensions = {};
35+
Map<String, Object> extensions = {};
3636

3737
@override
3838
// ignore: type_annotate_public_apis
@@ -44,7 +44,7 @@ class Gpx {
4444
const ListEquality().equals(other.wpts, wpts) &&
4545
const ListEquality().equals(other.rtes, rtes) &&
4646
const ListEquality().equals(other.trks, trks) &&
47-
const MapEquality().equals(other.extensions, extensions);
47+
const DeepCollectionEquality().equals(other.extensions, extensions);
4848
}
4949

5050
return false;

lib/src/model/metadata.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Metadata {
3838

3939
/// You can add extend GPX by adding your own elements from another schema
4040
/// here.
41-
Map<String, String> extensions = {};
41+
Map<String, Object> extensions = {};
4242

4343
/// Construct a new [Metadata] object.
4444
Metadata(
@@ -50,9 +50,9 @@ class Metadata {
5050
this.time,
5151
this.keywords,
5252
this.bounds,
53-
Map<String, String>? extensions})
53+
Map<String, Object>? extensions})
5454
: links = links ?? [],
55-
extensions = extensions ?? <String, String>{};
55+
extensions = extensions ?? <String, Object>{};
5656

5757
@override
5858
// ignore: type_annotate_public_apis
@@ -66,7 +66,7 @@ class Metadata {
6666
other.time == time &&
6767
other.keywords == keywords &&
6868
other.bounds == bounds &&
69-
const MapEquality().equals(other.extensions, extensions);
69+
const DeepCollectionEquality().equals(other.extensions, extensions);
7070
}
7171

7272
return false;

lib/src/model/rte.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Rte {
3131

3232
/// You can add extend GPX by adding your own elements from another schema
3333
/// here.
34-
Map<String, String> extensions;
34+
Map<String, Object> extensions;
3535

3636
/// A list of route points.
3737
List<Wpt> rtepts;
@@ -45,10 +45,10 @@ class Rte {
4545
List<Link>? links,
4646
this.number,
4747
this.type,
48-
Map<String, String>? extensions,
48+
Map<String, Object>? extensions,
4949
List<Wpt>? rtepts})
5050
: links = links ?? [],
51-
extensions = extensions ?? <String, String>{},
51+
extensions = extensions ?? <String, Object>{},
5252
rtepts = rtepts ?? [];
5353

5454
@override
@@ -62,7 +62,7 @@ class Rte {
6262
const ListEquality().equals(other.links, links) &&
6363
other.number == number &&
6464
other.type == type &&
65-
const MapEquality().equals(other.extensions, extensions) &&
65+
const DeepCollectionEquality().equals(other.extensions, extensions) &&
6666
const ListEquality().equals(other.rtepts, rtepts);
6767
}
6868

lib/src/model/trk.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Trk {
3030

3131
/// You can add extend GPX by adding your own elements from another schema
3232
/// here.
33-
Map<String, String> extensions;
33+
Map<String, Object> extensions;
3434

3535
/// A Track Segment holds a list of Track Points which are logically connected
3636
/// in order. To represent a single GPS track where GPS reception was lost, or
@@ -47,10 +47,10 @@ class Trk {
4747
List<Link>? links,
4848
this.number,
4949
this.type,
50-
Map<String, String>? extensions,
50+
Map<String, Object>? extensions,
5151
List<Trkseg>? trksegs})
5252
: links = links ?? [],
53-
extensions = extensions ?? <String, String>{},
53+
extensions = extensions ?? <String, Object>{},
5454
trksegs = trksegs ?? [];
5555

5656
@override
@@ -64,7 +64,7 @@ class Trk {
6464
const ListEquality().equals(other.links, links) &&
6565
other.number == number &&
6666
other.type == type &&
67-
const MapEquality().equals(other.extensions, extensions) &&
67+
const DeepCollectionEquality().equals(other.extensions, extensions) &&
6868
const ListEquality().equals(other.trksegs, trksegs);
6969
}
7070

lib/src/model/trkseg.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ class Trkseg {
1414

1515
/// You can add extend GPX by adding your own elements from another schema
1616
/// here.
17-
Map<String, String> extensions;
17+
Map<String, Object> extensions;
1818

1919
/// Construct a new [Trkseg] object.
20-
Trkseg({List<Wpt>? trkpts, Map<String, String>? extensions})
20+
Trkseg({List<Wpt>? trkpts, Map<String, Object>? extensions})
2121
: trkpts = trkpts ?? [],
22-
extensions = extensions ?? <String, String>{};
22+
extensions = extensions ?? <String, Object>{};
2323

2424
@override
2525
// ignore: type_annotate_public_apis
2626
bool operator ==(other) {
2727
if (other is Trkseg) {
2828
return const ListEquality().equals(other.trkpts, trkpts) &&
29-
const MapEquality().equals(other.extensions, extensions);
29+
const DeepCollectionEquality().equals(other.extensions, extensions);
3030
}
3131

3232
return false;

lib/src/model/wpt.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Wpt {
7979

8080
/// You can add extend GPX by adding your own elements from another schema
8181
/// here.
82-
Map<String, String> extensions;
82+
Map<String, Object> extensions;
8383

8484
/// Construct a new [Wpt] object.
8585
Wpt(
@@ -103,9 +103,9 @@ class Wpt {
103103
this.pdop,
104104
this.ageofdgpsdata,
105105
this.dgpsid,
106-
Map<String, String>? extensions})
106+
Map<String, Object>? extensions})
107107
: links = links ?? [],
108-
extensions = extensions ?? <String, String>{};
108+
extensions = extensions ?? <String, Object>{};
109109

110110
@override
111111
// ignore: type_annotate_public_apis
@@ -131,7 +131,7 @@ class Wpt {
131131
other.pdop == pdop &&
132132
other.ageofdgpsdata == ageofdgpsdata &&
133133
other.dgpsid == dgpsid &&
134-
const MapEquality().equals(other.extensions, extensions);
134+
const DeepCollectionEquality().equals(other.extensions, extensions);
135135
}
136136

137137
return false;

test/assets/complex.gpx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<extensions>
1111
<m1>v1</m1>
1212
<m2>v2</m2>
13+
<mext:ext>
14+
<mext:val>val</mext:val>
15+
<mext:num>10</mext:num>
16+
</mext:ext>
1317
</extensions>
1418
</metadata>
1519
<extensions>
@@ -23,6 +27,10 @@
2327
<extensions>
2428
<k1>v1</k1>
2529
<k2>v2</k2>
30+
<wext:ext>
31+
<wext:val>val</wext:val>
32+
<wext:num>10</wext:num>
33+
</wext:ext>
2634
</extensions>
2735
</wpt>
2836
<wpt lat="36.62" lon="101.77">

0 commit comments

Comments
 (0)