Skip to content

Commit 496ee15

Browse files
TheBuggedYRNHeshamMegid
authored andcommitted
[MOB-10816] Unify Internal Models Style (#294)
1 parent 1997836 commit 496ee15

12 files changed

+122
-120
lines changed

lib/src/models/crash_data.dart

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import 'package:instabug_flutter/src/models/exception_data.dart';
22

33
class CrashData {
4-
CrashData(this.message, this.os, this.exception);
4+
const CrashData({
5+
required this.os,
6+
required this.message,
7+
required this.exception,
8+
});
59

6-
String message;
7-
String os;
8-
String platform = 'flutter';
9-
List<ExceptionData> exception;
10+
static const platform = 'flutter';
11+
final String message;
12+
final String os;
13+
final List<ExceptionData> exception;
1014

11-
Map<String, dynamic> toJson() => <String, dynamic>{
12-
'message': message,
13-
'os': os,
14-
'platform': platform,
15-
'exception': exception,
16-
};
17-
18-
Map<String, dynamic> toMap() {
19-
final map = <String, dynamic>{};
20-
map['message'] = message;
21-
map['os'] = os;
22-
map['platform'] = platform;
23-
map['exception'] = exception;
24-
return map;
15+
Map<String, dynamic> toJson() {
16+
return {
17+
'os': os,
18+
'message': message,
19+
'platform': platform,
20+
'exception': exception,
21+
};
2522
}
2623
}

lib/src/models/exception_data.dart

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
class ExceptionData {
2-
const ExceptionData(this.file, this.methodName, this.lineNumber, this.column);
2+
const ExceptionData({
3+
required this.file,
4+
required this.methodName,
5+
required this.lineNumber,
6+
required this.column,
7+
});
38

49
final String file;
510
final String? methodName;
611
final int? lineNumber;
712
final int column;
813

9-
Map<String, dynamic> toJson() => <String, dynamic>{
10-
'file': file,
11-
'methodName': methodName,
12-
'arguments': <dynamic>[],
13-
'lineNumber': lineNumber,
14-
'column': column,
15-
};
16-
17-
Map<String, dynamic> toMap() {
18-
final map = <String, dynamic>{};
19-
map['file'] = file;
20-
map['methodName'] = methodName;
21-
map['arguments'] = <dynamic>[];
22-
map['lineNumber'] = lineNumber;
23-
map['column'] = column;
24-
return map;
14+
Map<String, dynamic> toJson() {
15+
return {
16+
'file': file,
17+
'methodName': methodName,
18+
'arguments': <dynamic>[],
19+
'lineNumber': lineNumber,
20+
'column': column,
21+
};
2522
}
2623
}

lib/src/models/network_data.dart

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,46 @@ class NetworkData {
5252
DateTime? startTime,
5353
int? errorCode,
5454
String? errorDomain,
55-
}) =>
56-
NetworkData(
57-
url: url ?? this.url,
58-
method: method ?? this.method,
59-
requestBody: requestBody ?? this.requestBody,
60-
responseBody: responseBody ?? this.responseBody,
61-
requestBodySize: requestBodySize ?? this.requestBodySize,
62-
responseBodySize: responseBodySize ?? this.responseBodySize,
63-
status: status ?? this.status,
64-
requestHeaders: requestHeaders ?? this.requestHeaders,
65-
responseHeaders: responseHeaders ?? this.responseHeaders,
66-
duration: duration ?? this.duration,
67-
requestContentType: requestContentType ?? this.requestContentType,
68-
responseContentType: responseContentType ?? this.responseContentType,
69-
endTime: endTime ?? this.endTime,
70-
startTime: startTime ?? this.startTime,
71-
errorCode: errorCode ?? this.errorCode,
72-
errorDomain: errorDomain ?? this.errorDomain,
73-
);
55+
}) {
56+
return NetworkData(
57+
url: url ?? this.url,
58+
method: method ?? this.method,
59+
requestBody: requestBody ?? this.requestBody,
60+
responseBody: responseBody ?? this.responseBody,
61+
requestBodySize: requestBodySize ?? this.requestBodySize,
62+
responseBodySize: responseBodySize ?? this.responseBodySize,
63+
status: status ?? this.status,
64+
requestHeaders: requestHeaders ?? this.requestHeaders,
65+
responseHeaders: responseHeaders ?? this.responseHeaders,
66+
duration: duration ?? this.duration,
67+
requestContentType: requestContentType ?? this.requestContentType,
68+
responseContentType: responseContentType ?? this.responseContentType,
69+
endTime: endTime ?? this.endTime,
70+
startTime: startTime ?? this.startTime,
71+
errorCode: errorCode ?? this.errorCode,
72+
errorDomain: errorDomain ?? this.errorDomain,
73+
);
74+
}
7475

75-
Map<String, dynamic> toMap() {
76-
final map = <String, dynamic>{};
77-
map['url'] = url;
78-
map['method'] = method;
79-
map['requestBody'] = requestBody;
80-
map['responseBody'] = responseBody;
81-
map['responseCode'] = status;
82-
map['requestHeaders'] =
83-
requestHeaders.map((key, value) => MapEntry(key, value.toString()));
84-
map['responseHeaders'] =
85-
responseHeaders.map((key, value) => MapEntry(key, value.toString()));
86-
map['requestContentType'] = requestContentType;
87-
map['responseContentType'] = responseContentType;
88-
map['duration'] = duration;
89-
map['startTime'] = startTime.millisecondsSinceEpoch;
90-
map['requestBodySize'] = requestBodySize;
91-
map['responseBodySize'] = responseBodySize;
92-
map['errorDomain'] = errorDomain;
93-
map['errorCode'] = errorCode;
94-
return map;
76+
Map<String, dynamic> toJson() {
77+
return {
78+
'url': url,
79+
'method': method,
80+
'requestBody': requestBody,
81+
'responseBody': responseBody,
82+
'responseCode': status,
83+
'requestHeaders':
84+
requestHeaders.map((key, value) => MapEntry(key, value.toString())),
85+
'responseHeaders':
86+
responseHeaders.map((key, value) => MapEntry(key, value.toString())),
87+
'requestContentType': requestContentType,
88+
'responseContentType': responseContentType,
89+
'duration': duration,
90+
'startTime': startTime.millisecondsSinceEpoch,
91+
'requestBodySize': requestBodySize,
92+
'responseBodySize': responseBodySize,
93+
'errorDomain': errorDomain,
94+
'errorCode': errorCode,
95+
};
9596
}
9697
}

lib/src/models/trace.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import 'package:instabug_flutter/src/modules/apm.dart';
22

33
class Trace {
4-
String id;
5-
String name = '';
6-
Map<String, dynamic> attributes;
7-
Trace(this.id, this.name, {Map<String, dynamic>? listOfAttributes})
8-
: attributes = listOfAttributes ?? <String, dynamic>{};
4+
Trace({
5+
required this.id,
6+
required this.name,
7+
});
98

10-
Map<String, dynamic> toMap() {
11-
final map = <String, dynamic>{};
12-
map['id'] = id;
13-
map['name'] = name;
14-
map['attributes'] = attributes;
15-
return map;
16-
}
9+
final String id;
10+
final String name;
11+
final Map<String, dynamic> attributes = {};
1712

1813
void setAttribute(String key, String value) {
1914
APM.setExecutionTraceAttribute(id, key, value);
@@ -23,4 +18,12 @@ class Trace {
2318
void end() {
2419
APM.endExecutionTrace(id);
2520
}
21+
22+
Map<String, dynamic> toJson() {
23+
return {
24+
'id': id,
25+
'name': name,
26+
'attributes': attributes,
27+
};
28+
}
2629
}

lib/src/modules/apm.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ class APM {
5858
);
5959
}
6060

61-
return Trace(traceId, name);
61+
return Trace(
62+
id: traceId,
63+
name: name,
64+
);
6265
}
6366

6467
/// Sets attribute of an execution trace.
@@ -103,7 +106,7 @@ class APM {
103106

104107
static FutureOr<void> networkLogAndroid(NetworkData data) {
105108
if (IBGBuildInfo.instance.isAndroid) {
106-
return _host.networkLogAndroid(data.toMap());
109+
return _host.networkLogAndroid(data.toJson());
107110
}
108111
}
109112
}

lib/src/modules/crash_reporting.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,21 @@ class CrashReporting {
6060
bool handled,
6161
) async {
6262
final trace = Trace.from(stack);
63-
final frames = <ExceptionData>[];
64-
65-
for (var i = 0; i < trace.frames.length; i++) {
66-
frames.add(
67-
ExceptionData(
68-
trace.frames[i].uri.toString(),
69-
trace.frames[i].member,
70-
trace.frames[i].line,
71-
trace.frames[i].column ?? 0,
72-
),
73-
);
74-
}
63+
final frames = trace.frames
64+
.map(
65+
(frame) => ExceptionData(
66+
file: frame.uri.toString(),
67+
methodName: frame.member,
68+
lineNumber: frame.line,
69+
column: frame.column ?? 0,
70+
),
71+
)
72+
.toList();
7573

7674
final crashData = CrashData(
77-
exception.toString(),
78-
IBGBuildInfo.instance.operatingSystem,
79-
frames,
75+
os: IBGBuildInfo.instance.operatingSystem,
76+
message: exception.toString(),
77+
exception: frames,
8078
);
8179

8280
return _host.send(jsonEncode(crashData), handled);

lib/src/modules/network_logger.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class NetworkLogger {
1717
}
1818

1919
Future<void> networkLog(NetworkData data) async {
20-
await _host.networkLog(data.toMap());
20+
await _host.networkLog(data.toJson());
2121
await APM.networkLogAndroid(data);
2222
}
2323
}

test/apm_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void main() {
145145
await APM.networkLogAndroid(data);
146146

147147
verify(
148-
mHost.networkLogAndroid(data.toMap()),
148+
mHost.networkLogAndroid(data.toJson()),
149149
).called(1);
150150
});
151151
}

test/crash_reporting_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ void main() {
4646
final frames = trace.frames
4747
.map(
4848
(frame) => ExceptionData(
49-
frame.uri.toString(),
50-
frame.member,
51-
frame.line,
52-
frame.column ?? 0,
49+
file: frame.uri.toString(),
50+
methodName: frame.member,
51+
lineNumber: frame.line,
52+
column: frame.column ?? 0,
5353
),
5454
)
5555
.toList();
5656

5757
when(mBuildInfo.operatingSystem).thenReturn('unit-test');
5858

5959
final data = CrashData(
60-
exception.toString(),
61-
IBGBuildInfo.instance.operatingSystem,
62-
frames,
60+
os: IBGBuildInfo.instance.operatingSystem,
61+
message: exception.toString(),
62+
exception: frames,
6363
);
6464

6565
await CrashReporting.reportHandledCrash(exception, stack);

test/network_data_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void main() {
2323

2424
test('[copyWith] should return an exact copy', () async {
2525
final newData = data.copyWith();
26-
expect(newData.toMap(), data.toMap());
26+
expect(newData.toJson(), data.toJson());
2727
});
2828

2929
test('[copyWith] should return an updated copy', () async {
@@ -69,7 +69,7 @@ void main() {
6969
});
7070

7171
test('[toMap] should return exact data in a map', () async {
72-
final map = data.toMap();
72+
final map = data.toJson();
7373

7474
expect(map['url'], data.url);
7575
expect(map['method'], data.method);

0 commit comments

Comments
 (0)