Skip to content

Commit 97b4464

Browse files
authored
Merge pull request #241 from k-paxian/refactor-utils-split
Refactor: Split `utils.dart` into multiple files
2 parents b4f0f8a + 87018c7 commit 97b4464

File tree

8 files changed

+110
-101
lines changed

8 files changed

+110
-101
lines changed

mapper/lib/dart_json_mapper.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export './src/errors.dart';
22
export './src/mapper.dart';
33
export './src/model/index.dart';
4-
export './src/utils.dart';
4+
export './src/globals.dart';
5+
export './src/json_map.dart';
6+
export './src/class_info.dart';
Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,6 @@ import 'package:reflectable/reflectable.dart';
33

44
import 'model/index.dart';
55

6-
Type typeOf<T>() => T;
7-
8-
const kIsWeb = identical(0, 0.0);
9-
10-
/// Provides logic for traversing Json object tree
11-
class JsonMap {
12-
final pathDelimiter = '/';
13-
14-
Map map;
15-
List<JsonMap>? parentMaps = [];
16-
Json? jsonMeta;
17-
18-
JsonMap(this.map, [this.jsonMeta, this.parentMaps]);
19-
20-
bool hasProperty(String name) {
21-
return _isPathExists(_getPath(name));
22-
}
23-
24-
dynamic getPropertyValue(String name) {
25-
dynamic result;
26-
final path = _getPath(name);
27-
_isPathExists(path, (m, k) {
28-
result = (m is Map && m.containsKey(k) && k != path) ? m[k] : m;
29-
});
30-
return result;
31-
}
32-
33-
void setPropertyValue(String name, dynamic value) {
34-
_isPathExists(_getPath(name), (m, k) {}, true, value);
35-
}
36-
37-
String _decodePath(String path) {
38-
if (path.startsWith('#')) {
39-
path = Uri.decodeComponent(path).substring(1);
40-
}
41-
return path;
42-
}
43-
44-
String _getPath(String propertyName) {
45-
final rootObjectSegments = jsonMeta != null && jsonMeta!.name != null
46-
? _decodePath(jsonMeta!.name!).split(pathDelimiter)
47-
: [];
48-
final propertySegments = _decodePath(propertyName).split(pathDelimiter);
49-
rootObjectSegments.addAll(propertySegments);
50-
rootObjectSegments.removeWhere((value) => value == '');
51-
return rootObjectSegments.join(pathDelimiter);
52-
}
53-
54-
bool _isPathExists(String path,
55-
[Function? propertyVisitor, bool? autoCreate, dynamic autoValue]) {
56-
final segments = path
57-
.split(pathDelimiter)
58-
.map((p) => p.replaceAll('~1', pathDelimiter).replaceAll('~0', '~'))
59-
.toList();
60-
dynamic current = map;
61-
var existingSegmentsCount = 0;
62-
for (var segment in segments) {
63-
final idx = int.tryParse(segment);
64-
if (segment == JsonProperty.parentReference) {
65-
final nearestParent =
66-
parentMaps!.lastWhereOrNull((element) => element.map != current);
67-
if (nearestParent != null) {
68-
current = nearestParent.map;
69-
existingSegmentsCount++;
70-
}
71-
continue;
72-
}
73-
if (current is List &&
74-
idx != null &&
75-
(current.length > idx) &&
76-
(idx >= 0) &&
77-
current.elementAt(idx) != null) {
78-
current = current.elementAt(idx);
79-
existingSegmentsCount++;
80-
}
81-
if (current is Map && current.containsKey(segment)) {
82-
current = current[segment];
83-
existingSegmentsCount++;
84-
} else {
85-
if (autoCreate == true) {
86-
existingSegmentsCount++;
87-
final isLastSegment = segments.length == existingSegmentsCount;
88-
current[segment] = isLastSegment ? autoValue : {};
89-
current = current[segment];
90-
}
91-
}
92-
}
93-
if (propertyVisitor != null && current != null) {
94-
propertyVisitor(current, segments.last);
95-
}
96-
return segments.length == existingSegmentsCount &&
97-
existingSegmentsCount > 0;
98-
}
99-
}
100-
1016
/// Provides unified access to class information based on [ClassMirror]
1027
class ClassInfo {
1038
ClassMirror classMirror;
@@ -431,4 +336,4 @@ class ClassInfo {
431336
}
432337
return result;
433338
}
434-
}
339+
}

mapper/lib/src/globals.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Type typeOf<T>() => T;
2+
3+
const kIsWeb = identical(0, 0.0);

mapper/lib/src/json_map.dart

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import 'package:collection/collection.dart' show IterableExtension;
2+
3+
import 'model/index.dart';
4+
5+
/// Provides logic for traversing Json object tree
6+
class JsonMap {
7+
final pathDelimiter = '/';
8+
9+
Map map;
10+
List<JsonMap>? parentMaps = [];
11+
Json? jsonMeta;
12+
13+
JsonMap(this.map, [this.jsonMeta, this.parentMaps]);
14+
15+
bool hasProperty(String name) {
16+
return _isPathExists(_getPath(name));
17+
}
18+
19+
dynamic getPropertyValue(String name) {
20+
dynamic result;
21+
final path = _getPath(name);
22+
_isPathExists(path, (m, k) {
23+
result = (m is Map && m.containsKey(k) && k != path) ? m[k] : m;
24+
});
25+
return result;
26+
}
27+
28+
void setPropertyValue(String name, dynamic value) {
29+
_isPathExists(_getPath(name), (m, k) {}, true, value);
30+
}
31+
32+
String _decodePath(String path) {
33+
if (path.startsWith('#')) {
34+
path = Uri.decodeComponent(path).substring(1);
35+
}
36+
return path;
37+
}
38+
39+
String _getPath(String propertyName) {
40+
final rootObjectSegments = jsonMeta != null && jsonMeta!.name != null
41+
? _decodePath(jsonMeta!.name!).split(pathDelimiter)
42+
: [];
43+
final propertySegments = _decodePath(propertyName).split(pathDelimiter);
44+
rootObjectSegments.addAll(propertySegments);
45+
rootObjectSegments.removeWhere((value) => value == '');
46+
return rootObjectSegments.join(pathDelimiter);
47+
}
48+
49+
bool _isPathExists(String path,
50+
[Function? propertyVisitor, bool? autoCreate, dynamic autoValue]) {
51+
final segments = path
52+
.split(pathDelimiter)
53+
.map((p) => p.replaceAll('~1', pathDelimiter).replaceAll('~0', '~'))
54+
.toList();
55+
dynamic current = map;
56+
var existingSegmentsCount = 0;
57+
for (var segment in segments) {
58+
final idx = int.tryParse(segment);
59+
if (segment == JsonProperty.parentReference) {
60+
final nearestParent =
61+
parentMaps!.lastWhereOrNull((element) => element.map != current);
62+
if (nearestParent != null) {
63+
current = nearestParent.map;
64+
existingSegmentsCount++;
65+
}
66+
continue;
67+
}
68+
if (current is List &&
69+
idx != null &&
70+
(current.length > idx) &&
71+
(idx >= 0) &&
72+
current.elementAt(idx) != null) {
73+
current = current.elementAt(idx);
74+
existingSegmentsCount++;
75+
}
76+
if (current is Map && current.containsKey(segment)) {
77+
current = current[segment];
78+
existingSegmentsCount++;
79+
} else {
80+
if (autoCreate == true) {
81+
existingSegmentsCount++;
82+
final isLastSegment = segments.length == existingSegmentsCount;
83+
current[segment] = isLastSegment ? autoValue : {};
84+
current = current[segment];
85+
}
86+
}
87+
}
88+
if (propertyVisitor != null && current != null) {
89+
propertyVisitor(current, segments.last);
90+
}
91+
return segments.length == existingSegmentsCount &&
92+
existingSegmentsCount > 0;
93+
}
94+
}

mapper/lib/src/mapper.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import 'package:reflectable/reflectable.dart'
1212

1313
import 'errors.dart';
1414
import 'model/index.dart';
15-
import 'utils.dart';
15+
import 'class_info.dart';
16+
import 'globals.dart';
17+
import 'json_map.dart';
1618

1719
/// Singleton class providing mostly static methods for conversion of previously
1820
/// annotated by [JsonSerializable] Dart objects from / to JSON string

mapper/lib/src/model/adapters.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import 'package:reflectable/src/reflectable_builder_based.dart'
1313
show ReflectorData;
1414

1515
import '../model/index.dart' show Enum;
16-
import '../utils.dart';
16+
import '../class_info.dart';
17+
import '../globals.dart';
1718
import 'converters.dart';
1819
import 'type_info.dart';
1920
import 'value_decorators.dart';

mapper/lib/src/model/index.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'package:collection/collection.dart';
22

33
import '../identifier_casing.dart';
4-
import '../utils.dart';
4+
import '../globals.dart';
5+
import '../json_map.dart';
56
import 'annotations.dart';
67
import 'type_info.dart';
78

mapper/lib/src/model/type_info.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'dart:collection';
22

3+
import '../class_info.dart';
34
import './value_decorators.dart';
4-
import '../utils.dart';
5+
import '../globals.dart';
56

67
/// Provides enhanced type information based on `Type.toString()` value
78
class TypeInfo {

0 commit comments

Comments
 (0)