mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
opt.: use json_serializable
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'package:hive_flutter/adapters.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'custom.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@HiveType(typeId: 7)
|
||||
final class ServerCustom {
|
||||
// @HiveField(0)
|
||||
@@ -28,44 +30,11 @@ final class ServerCustom {
|
||||
this.logoUrl,
|
||||
});
|
||||
|
||||
static ServerCustom fromJson(Map<String, dynamic> json) {
|
||||
//final temperature = json["temperature"] as String?;
|
||||
final pveAddr = json['pveAddr'] as String?;
|
||||
final pveIgnoreCert = json['pveIgnoreCert'] as bool;
|
||||
final cmds = json['cmds'] as Map<String, dynamic>?;
|
||||
final preferTempDev = json['preferTempDev'] as String?;
|
||||
final logoUrl = json['logoUrl'] as String?;
|
||||
return ServerCustom(
|
||||
//temperature: temperature,
|
||||
pveAddr: pveAddr,
|
||||
pveIgnoreCert: pveIgnoreCert,
|
||||
cmds: cmds?.cast<String, String>(),
|
||||
preferTempDev: preferTempDev,
|
||||
logoUrl: logoUrl,
|
||||
);
|
||||
}
|
||||
factory ServerCustom.fromJson(Map<String, dynamic> json) =>
|
||||
_$ServerCustomFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final json = <String, dynamic>{};
|
||||
// if (temperature != null) {
|
||||
// json["temperature"] = temperature;
|
||||
// }
|
||||
if (pveAddr != null) {
|
||||
json['pveAddr'] = pveAddr;
|
||||
}
|
||||
json['pveIgnoreCert'] = pveIgnoreCert;
|
||||
Map<String, dynamic> toJson() => _$ServerCustomToJson(this);
|
||||
|
||||
if (cmds != null) {
|
||||
json['cmds'] = cmds;
|
||||
}
|
||||
if (preferTempDev != null) {
|
||||
json['preferTempDev'] = preferTempDev;
|
||||
}
|
||||
if (logoUrl != null) {
|
||||
json['logoUrl'] = logoUrl;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
|
||||
@@ -51,3 +51,26 @@ class ServerCustomAdapter extends TypeAdapter<ServerCustom> {
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
ServerCustom _$ServerCustomFromJson(Map<String, dynamic> json) => ServerCustom(
|
||||
pveAddr: json['pveAddr'] as String?,
|
||||
pveIgnoreCert: json['pveIgnoreCert'] as bool? ?? false,
|
||||
cmds: (json['cmds'] as Map<String, dynamic>?)?.map(
|
||||
(k, e) => MapEntry(k, e as String),
|
||||
),
|
||||
preferTempDev: json['preferTempDev'] as String?,
|
||||
logoUrl: json['logoUrl'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$ServerCustomToJson(ServerCustom instance) =>
|
||||
<String, dynamic>{
|
||||
'pveAddr': instance.pveAddr,
|
||||
'pveIgnoreCert': instance.pveIgnoreCert,
|
||||
'cmds': instance.cmds,
|
||||
'preferTempDev': instance.preferTempDev,
|
||||
'logoUrl': instance.logoUrl,
|
||||
};
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'private_key_info.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@HiveType(typeId: 1)
|
||||
class PrivateKeyInfo {
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
@JsonKey(name: 'private_key')
|
||||
@HiveField(1)
|
||||
final String key;
|
||||
|
||||
@@ -14,6 +17,11 @@ class PrivateKeyInfo {
|
||||
required this.key,
|
||||
});
|
||||
|
||||
factory PrivateKeyInfo.fromJson(Map<String, dynamic> json) =>
|
||||
_$PrivateKeyInfoFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$PrivateKeyInfoToJson(this);
|
||||
|
||||
String? get type {
|
||||
final lines = key.split('\n');
|
||||
if (lines.length < 2) {
|
||||
@@ -26,15 +34,4 @@ class PrivateKeyInfo {
|
||||
}
|
||||
return splited[1];
|
||||
}
|
||||
|
||||
PrivateKeyInfo.fromJson(Map<String, dynamic> json)
|
||||
: id = json['id'].toString(),
|
||||
key = json['private_key'].toString();
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, String>{};
|
||||
data['id'] = id;
|
||||
data['private_key'] = key;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,3 +42,19 @@ class PrivateKeyInfoAdapter extends TypeAdapter<PrivateKeyInfo> {
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
PrivateKeyInfo _$PrivateKeyInfoFromJson(Map<String, dynamic> json) =>
|
||||
PrivateKeyInfo(
|
||||
id: json['id'] as String,
|
||||
key: json['private_key'] as String,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$PrivateKeyInfoToJson(PrivateKeyInfo instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'private_key': instance.key,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:server_box/data/model/server/custom.dart';
|
||||
import 'package:server_box/data/model/server/server.dart';
|
||||
import 'package:server_box/data/model/server/wol_cfg.dart';
|
||||
@@ -11,6 +12,7 @@ import 'package:server_box/data/model/app/error.dart';
|
||||
part 'server_private_info.g.dart';
|
||||
|
||||
/// In former version, it's called `ServerPrivateInfo`.
|
||||
@JsonSerializable()
|
||||
@HiveType(typeId: 3)
|
||||
class ServerPrivateInfo {
|
||||
@HiveField(0)
|
||||
@@ -25,6 +27,7 @@ class ServerPrivateInfo {
|
||||
final String? pwd;
|
||||
|
||||
/// [id] of private key
|
||||
@JsonKey(name: 'pubKeyId')
|
||||
@HiveField(5)
|
||||
final String? keyId;
|
||||
@HiveField(6)
|
||||
@@ -66,83 +69,10 @@ class ServerPrivateInfo {
|
||||
this.envs,
|
||||
}) : id = '$user@$ip:$port';
|
||||
|
||||
static ServerPrivateInfo fromJson(Map<String, dynamic> json) {
|
||||
final ip = json['ip'] as String? ?? '';
|
||||
final port = json['port'] as int? ?? 22;
|
||||
final user = json['user'] as String? ?? 'root';
|
||||
final name = json['name'] as String? ?? '';
|
||||
final pwd = json['pwd'] as String? ?? json['authorization'] as String?;
|
||||
final keyId = json['pubKeyId'] as String?;
|
||||
final tags = (json['tags'] as List?)?.cast<String>();
|
||||
final alterUrl = json['alterUrl'] as String?;
|
||||
final autoConnect = json['autoConnect'] as bool? ?? true;
|
||||
final jumpId = json['jumpId'] as String?;
|
||||
final custom = json['customCmd'] == null
|
||||
? null
|
||||
: ServerCustom.fromJson(json['custom'].cast<String, dynamic>());
|
||||
final wolCfg = json['wolCfg'] == null
|
||||
? null
|
||||
: WakeOnLanCfg.fromJson(json['wolCfg'].cast<String, dynamic>());
|
||||
final envs_ = json['envs'] as Map<String, dynamic>?;
|
||||
final envs = <String, String>{};
|
||||
if (envs_ != null) {
|
||||
envs_.forEach((key, value) {
|
||||
if (value is String) {
|
||||
envs[key] = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return ServerPrivateInfo(
|
||||
name: name,
|
||||
ip: ip,
|
||||
port: port,
|
||||
user: user,
|
||||
pwd: pwd,
|
||||
keyId: keyId,
|
||||
tags: tags,
|
||||
alterUrl: alterUrl,
|
||||
autoConnect: autoConnect,
|
||||
jumpId: jumpId,
|
||||
custom: custom,
|
||||
wolCfg: wolCfg,
|
||||
envs: envs.isEmpty ? null : envs,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['name'] = name;
|
||||
data['ip'] = ip;
|
||||
data['port'] = port;
|
||||
data['user'] = user;
|
||||
if (pwd != null) {
|
||||
data['pwd'] = pwd;
|
||||
}
|
||||
if (keyId != null) {
|
||||
data['pubKeyId'] = keyId;
|
||||
}
|
||||
if (tags != null) {
|
||||
data['tags'] = tags;
|
||||
}
|
||||
if (alterUrl != null) {
|
||||
data['alterUrl'] = alterUrl;
|
||||
}
|
||||
data['autoConnect'] = autoConnect;
|
||||
if (jumpId != null) {
|
||||
data['jumpId'] = jumpId;
|
||||
}
|
||||
if (custom != null) {
|
||||
data['custom'] = custom?.toJson();
|
||||
}
|
||||
if (wolCfg != null) {
|
||||
data['wolCfg'] = wolCfg?.toJson();
|
||||
}
|
||||
if (envs != null) {
|
||||
data['envs'] = envs;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
factory ServerPrivateInfo.fromJson(Map<String, dynamic> json) =>
|
||||
_$ServerPrivateInfoFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$ServerPrivateInfoToJson(this);
|
||||
|
||||
String toJsonString() => json.encode(toJson());
|
||||
|
||||
|
||||
@@ -75,3 +75,47 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
ServerPrivateInfo _$ServerPrivateInfoFromJson(Map<String, dynamic> json) =>
|
||||
ServerPrivateInfo(
|
||||
name: json['name'] as String,
|
||||
ip: json['ip'] as String,
|
||||
port: (json['port'] as num).toInt(),
|
||||
user: json['user'] as String,
|
||||
pwd: json['pwd'] as String?,
|
||||
keyId: json['pubKeyId'] as String?,
|
||||
tags: (json['tags'] as List<dynamic>?)?.map((e) => e as String).toList(),
|
||||
alterUrl: json['alterUrl'] as String?,
|
||||
autoConnect: json['autoConnect'] as bool? ?? true,
|
||||
jumpId: json['jumpId'] as String?,
|
||||
custom: json['custom'] == null
|
||||
? null
|
||||
: ServerCustom.fromJson(json['custom'] as Map<String, dynamic>),
|
||||
wolCfg: json['wolCfg'] == null
|
||||
? null
|
||||
: WakeOnLanCfg.fromJson(json['wolCfg'] as Map<String, dynamic>),
|
||||
envs: (json['envs'] as Map<String, dynamic>?)?.map(
|
||||
(k, e) => MapEntry(k, e as String),
|
||||
),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$ServerPrivateInfoToJson(ServerPrivateInfo instance) =>
|
||||
<String, dynamic>{
|
||||
'name': instance.name,
|
||||
'ip': instance.ip,
|
||||
'port': instance.port,
|
||||
'user': instance.user,
|
||||
'pwd': instance.pwd,
|
||||
'pubKeyId': instance.keyId,
|
||||
'tags': instance.tags,
|
||||
'alterUrl': instance.alterUrl,
|
||||
'autoConnect': instance.autoConnect,
|
||||
'jumpId': instance.jumpId,
|
||||
'custom': instance.custom,
|
||||
'wolCfg': instance.wolCfg,
|
||||
'envs': instance.envs,
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
|
||||
import 'package:fl_lib/fl_lib.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:server_box/data/model/server/server_private_info.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
@@ -9,6 +10,7 @@ import 'package:server_box/data/model/app/tag_pickable.dart';
|
||||
|
||||
part 'snippet.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@HiveType(typeId: 2)
|
||||
class Snippet implements TagPickable {
|
||||
@HiveField(0)
|
||||
@@ -32,22 +34,9 @@ class Snippet implements TagPickable {
|
||||
this.autoRunOn,
|
||||
});
|
||||
|
||||
Snippet.fromJson(Map<String, dynamic> json)
|
||||
: name = json['name'].toString(),
|
||||
script = json['script'].toString(),
|
||||
tags = json['tags']?.cast<String>(),
|
||||
note = json['note']?.toString(),
|
||||
autoRunOn = json['autoRunOn']?.cast<String>();
|
||||
factory Snippet.fromJson(Map<String, dynamic> json) => _$SnippetFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final data = <String, dynamic>{};
|
||||
data['name'] = name;
|
||||
data['script'] = script;
|
||||
data['tags'] = tags;
|
||||
data['note'] = note;
|
||||
data['autoRunOn'] = autoRunOn;
|
||||
return data;
|
||||
}
|
||||
Map<String, dynamic> toJson() => _$SnippetToJson(this);
|
||||
|
||||
@override
|
||||
bool containsTag(String tag) {
|
||||
|
||||
@@ -51,3 +51,25 @@ class SnippetAdapter extends TypeAdapter<Snippet> {
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Snippet _$SnippetFromJson(Map<String, dynamic> json) => Snippet(
|
||||
name: json['name'] as String,
|
||||
script: json['script'] as String,
|
||||
tags: (json['tags'] as List<dynamic>?)?.map((e) => e as String).toList(),
|
||||
note: json['note'] as String?,
|
||||
autoRunOn: (json['autoRunOn'] as List<dynamic>?)
|
||||
?.map((e) => e as String)
|
||||
.toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SnippetToJson(Snippet instance) => <String, dynamic>{
|
||||
'name': instance.name,
|
||||
'script': instance.script,
|
||||
'tags': instance.tags,
|
||||
'note': instance.note,
|
||||
'autoRunOn': instance.autoRunOn,
|
||||
};
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wake_on_lan/wake_on_lan.dart';
|
||||
|
||||
part 'wol_cfg.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@HiveType(typeId: 8)
|
||||
final class WakeOnLanCfg {
|
||||
@HiveField(0)
|
||||
@@ -54,22 +56,8 @@ final class WakeOnLanCfg {
|
||||
);
|
||||
}
|
||||
|
||||
static WakeOnLanCfg fromJson(Map<String, dynamic> json) {
|
||||
return WakeOnLanCfg(
|
||||
mac: json['mac'] as String,
|
||||
ip: json['ip'] as String,
|
||||
pwd: json['pwd'] as String?,
|
||||
);
|
||||
}
|
||||
factory WakeOnLanCfg.fromJson(Map<String, dynamic> json) =>
|
||||
_$WakeOnLanCfgFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{
|
||||
'mac': mac,
|
||||
'ip': ip,
|
||||
};
|
||||
if (pwd != null) {
|
||||
map['pwd'] = pwd;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
Map<String, dynamic> toJson() => _$WakeOnLanCfgToJson(this);
|
||||
}
|
||||
|
||||
@@ -45,3 +45,20 @@ class WakeOnLanCfgAdapter extends TypeAdapter<WakeOnLanCfg> {
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
WakeOnLanCfg _$WakeOnLanCfgFromJson(Map<String, dynamic> json) => WakeOnLanCfg(
|
||||
mac: json['mac'] as String,
|
||||
ip: json['ip'] as String,
|
||||
pwd: json['pwd'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$WakeOnLanCfgToJson(WakeOnLanCfg instance) =>
|
||||
<String, dynamic>{
|
||||
'mac': instance.mac,
|
||||
'ip': instance.ip,
|
||||
'pwd': instance.pwd,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user