more freezed cleanup

This commit is contained in:
Christien Rioux
2023-07-05 22:53:08 -05:00
parent d6999c36a9
commit 6a47363d8c
10 changed files with 3516 additions and 341 deletions

View File

@@ -52,7 +52,7 @@ class LatencyStats with _$LatencyStats {
required TimestampDuration slowest,
}) = _LatencyStats;
factory LatencyStats.fromJson(Map<String, Object?> json) =>
factory LatencyStats.fromJson(Map<String, dynamic> json) =>
_$LatencyStatsFromJson(json);
}
@@ -67,7 +67,7 @@ class TransferStats with _$TransferStats {
required BigInt minimum,
}) = _TransferStats;
factory TransferStats.fromJson(Map<String, Object?> json) =>
factory TransferStats.fromJson(Map<String, dynamic> json) =>
_$TransferStatsFromJson(json);
}
@@ -80,7 +80,7 @@ class TransferStatsDownUp with _$TransferStatsDownUp {
required TransferStats up,
}) = _TransferStatsDownUp;
factory TransferStatsDownUp.fromJson(Map<String, Object?> json) =>
factory TransferStatsDownUp.fromJson(Map<String, dynamic> json) =>
_$TransferStatsDownUpFromJson(json);
}
@@ -99,7 +99,7 @@ class RPCStats with _$RPCStats {
required int failedToSend,
}) = _RPCStats;
factory RPCStats.fromJson(Map<String, Object?> json) =>
factory RPCStats.fromJson(Map<String, dynamic> json) =>
_$RPCStatsFromJson(json);
}
@@ -114,7 +114,7 @@ class PeerStats with _$PeerStats {
required TransferStatsDownUp transfer,
}) = _PeerStats;
factory PeerStats.fromJson(Map<String, Object?> json) =>
factory PeerStats.fromJson(Map<String, dynamic> json) =>
_$PeerStatsFromJson(json);
}
@@ -128,314 +128,109 @@ class PeerTableData with _$PeerTableData {
required PeerStats peerStats,
}) = _PeerTableData;
factory PeerTableData.fromJson(Map<String, Object?> json) =>
factory PeerTableData.fromJson(Map<String, dynamic> json) =>
_$PeerTableDataFromJson(json);
}
//////////////////////////////////////
/// VeilidUpdate
abstract class VeilidUpdate {
factory VeilidUpdate.fromJson(dynamic json) {
switch (json["kind"]) {
case "Log":
{
return VeilidLog(
logLevel: VeilidLogLevel.fromJson(json["log_level"]),
message: json["message"],
backtrace: json["backtrace"]);
}
case "AppMessage":
{
return VeilidAppMessage(
sender: json["sender"], message: json["message"]);
}
case "AppCall":
{
return VeilidAppCall(
sender: json["sender"],
message: json["message"],
callId: json["call_id"]);
}
case "Attachment":
{
return VeilidUpdateAttachment(
state: VeilidStateAttachment.fromJson(json));
}
case "Network":
{
return VeilidUpdateNetwork(state: VeilidStateNetwork.fromJson(json));
}
case "Config":
{
return VeilidUpdateConfig(state: VeilidStateConfig.fromJson(json));
}
case "RouteChange":
{
return VeilidUpdateRouteChange(
deadRoutes: List<String>.from(json['dead_routes'].map((j) => j)),
deadRemoteRoutes:
List<String>.from(json['dead_remote_routes'].map((j) => j)));
}
case "ValueChange":
{
return VeilidUpdateValueChange(
key: TypedKey.fromJson(json['key']),
subkeys: List<ValueSubkeyRange>.from(
json['subkeys'].map((j) => ValueSubkeyRange.fromJson(j))),
count: json['count'],
valueData: ValueData.fromJson(json['value_data']));
}
default:
{
throw VeilidAPIExceptionInternal(
"Invalid VeilidAPIException type: ${json['kind']}");
}
}
}
Map<String, dynamic> toJson();
}
@Freezed(unionKey: 'kind', unionValueCase: FreezedUnionCase.pascal)
sealed class VeilidUpdate with _$VeilidUpdate {
const factory VeilidUpdate.log({
required VeilidLogLevel logLevel,
required String message,
String? backtrace,
}) = VeilidLog;
const factory VeilidUpdate.appMessage({
TypedKey? sender,
@Uint8ListJsonConverter() required Uint8List message,
}) = VeilidAppMessage;
const factory VeilidUpdate.appCall({
TypedKey? sender,
@Uint8ListJsonConverter() required Uint8List message,
required String callId,
}) = VeilidAppCall;
const factory VeilidUpdate.attachment(
{required AttachmentState state,
required bool publicInternetReady,
required bool localNetworkReady}) = VeilidUpdateAttachment;
const factory VeilidUpdate.network(
{required bool started,
required BigInt bpsDown,
required BigInt bpsUp,
required List<PeerTableData> peers}) = VeilidUpdateNetwork;
const factory VeilidUpdate.config({
required VeilidConfig config,
}) = VeilidUpdateConfig;
const factory VeilidUpdate.routeChange({
required List<String> deadRoutes,
required List<String> deadRemoteRoutes,
}) = VeilidUpdateRouteChange;
const factory VeilidUpdate.valueChange({
required TypedKey key,
required List<ValueSubkeyRange> subkeys,
required int count,
required ValueData valueData,
}) = VeilidUpdateValueChange;
class VeilidLog implements VeilidUpdate {
final VeilidLogLevel logLevel;
final String message;
final String? backtrace;
//
VeilidLog({
required this.logLevel,
required this.message,
required this.backtrace,
});
@override
Map<String, dynamic> toJson() {
return {
'kind': "Log",
'log_level': logLevel.toJson(),
'message': message,
'backtrace': backtrace
};
}
}
class VeilidAppMessage implements VeilidUpdate {
final TypedKey? sender;
final Uint8List message;
//
VeilidAppMessage({
required this.sender,
required this.message,
});
@override
Map<String, dynamic> toJson() {
return {
'kind': "AppMessage",
'sender': sender,
'message': base64UrlNoPadEncode(message)
};
}
}
class VeilidAppCall implements VeilidUpdate {
final String? sender;
final Uint8List message;
final String callId;
//
VeilidAppCall({
required this.sender,
required this.message,
required this.callId,
});
@override
Map<String, dynamic> toJson() {
return {
'kind': "AppCall",
'sender': sender,
'message': base64UrlNoPadEncode(message),
'call_id': callId,
};
}
}
class VeilidUpdateAttachment implements VeilidUpdate {
final VeilidStateAttachment state;
//
VeilidUpdateAttachment({required this.state});
@override
Map<String, dynamic> toJson() {
var jsonRep = state.toJson();
jsonRep['kind'] = "Attachment";
return jsonRep;
}
}
class VeilidUpdateNetwork implements VeilidUpdate {
final VeilidStateNetwork state;
//
VeilidUpdateNetwork({required this.state});
@override
Map<String, dynamic> toJson() {
var jsonRep = state.toJson();
jsonRep['kind'] = "Network";
return jsonRep;
}
}
class VeilidUpdateConfig implements VeilidUpdate {
final VeilidStateConfig state;
//
VeilidUpdateConfig({required this.state});
@override
Map<String, dynamic> toJson() {
var jsonRep = state.toJson();
jsonRep['kind'] = "Config";
return jsonRep;
}
}
class VeilidUpdateRouteChange implements VeilidUpdate {
final List<String> deadRoutes;
final List<String> deadRemoteRoutes;
//
VeilidUpdateRouteChange({
required this.deadRoutes,
required this.deadRemoteRoutes,
});
@override
Map<String, dynamic> toJson() {
return {
'dead_routes': deadRoutes.map((p) => p).toList(),
'dead_remote_routes': deadRemoteRoutes.map((p) => p).toList()
};
}
}
class VeilidUpdateValueChange implements VeilidUpdate {
final TypedKey key;
final List<ValueSubkeyRange> subkeys;
final int count;
final ValueData valueData;
//
VeilidUpdateValueChange({
required this.key,
required this.subkeys,
required this.count,
required this.valueData,
});
@override
Map<String, dynamic> toJson() {
return {
'key': key.toJson(),
'subkeys': subkeys.map((p) => p.toJson()).toList(),
'count': count,
'value_data': valueData.toJson(),
};
}
factory VeilidUpdate.fromJson(Map<String, dynamic> json) =>
_$VeilidUpdateFromJson(json);
}
//////////////////////////////////////
/// VeilidStateAttachment
class VeilidStateAttachment {
final AttachmentState state;
final bool publicInternetReady;
final bool localNetworkReady;
@freezed
class VeilidStateAttachment with _$VeilidStateAttachment {
const factory VeilidStateAttachment(
{required AttachmentState state,
required bool publicInternetReady,
required bool localNetworkReady}) = _VeilidStateAttachment;
VeilidStateAttachment(
this.state, this.publicInternetReady, this.localNetworkReady);
VeilidStateAttachment.fromJson(dynamic json)
: state = AttachmentState.fromJson(json['state']),
publicInternetReady = json['public_internet_ready'],
localNetworkReady = json['local_network_ready'];
Map<String, dynamic> toJson() {
return {
'state': state.toJson(),
'public_internet_ready': publicInternetReady,
'local_network_ready': localNetworkReady,
};
}
factory VeilidStateAttachment.fromJson(Map<String, dynamic> json) =>
_$VeilidStateAttachmentFromJson(json);
}
//////////////////////////////////////
/// VeilidStateNetwork
class VeilidStateNetwork {
final bool started;
final BigInt bpsDown;
final BigInt bpsUp;
final List<PeerTableData> peers;
@freezed
class VeilidStateNetwork with _$VeilidStateNetwork {
const factory VeilidStateNetwork(
{required bool started,
required BigInt bpsDown,
required BigInt bpsUp,
required List<PeerTableData> peers}) = _VeilidStateNetwork;
VeilidStateNetwork(
{required this.started,
required this.bpsDown,
required this.bpsUp,
required this.peers});
VeilidStateNetwork.fromJson(dynamic json)
: started = json['started'],
bpsDown = BigInt.parse(json['bps_down']),
bpsUp = BigInt.parse(json['bps_up']),
peers = List<PeerTableData>.from(
json['peers'].map((j) => PeerTableData.fromJson(j)));
Map<String, dynamic> toJson() {
return {
'started': started,
'bps_down': bpsDown.toString(),
'bps_up': bpsUp.toString(),
'peers': peers.map((p) => p.toJson()).toList(),
};
}
factory VeilidStateNetwork.fromJson(Map<String, dynamic> json) =>
_$VeilidStateNetworkFromJson(json);
}
//////////////////////////////////////
/// VeilidStateConfig
class VeilidStateConfig {
final VeilidConfig config;
@freezed
class VeilidStateConfig with _$VeilidStateConfig {
const factory VeilidStateConfig({
required VeilidConfig config,
}) = _VeilidStateConfig;
VeilidStateConfig({
required this.config,
});
VeilidStateConfig.fromJson(dynamic json)
: config = VeilidConfig.fromJson(json['config']);
Map<String, dynamic> toJson() {
return {'config': config.toJson()};
}
factory VeilidStateConfig.fromJson(Map<String, dynamic> json) =>
_$VeilidStateConfigFromJson(json);
}
//////////////////////////////////////
/// VeilidState
class VeilidState {
final VeilidStateAttachment attachment;
final VeilidStateNetwork network;
final VeilidStateConfig config;
@freezed
class VeilidState with _$VeilidState {
const factory VeilidState({
required VeilidStateAttachment attachment,
required VeilidStateNetwork network,
required VeilidStateConfig config,
}) = _VeilidState;
VeilidState.fromJson(dynamic json)
: attachment = VeilidStateAttachment.fromJson(json['attachment']),
network = VeilidStateNetwork.fromJson(json['network']),
config = VeilidStateConfig.fromJson(json['config']);
Map<String, dynamic> toJson() {
return {
'attachment': attachment.toJson(),
'network': network.toJson(),
'config': config.toJson()
};
}
factory VeilidState.fromJson(Map<String, dynamic> json) =>
_$VeilidStateFromJson(json);
}