json reader array 지원
This commit is contained in:
parent
f3a00b57b0
commit
d4065ae285
1 changed files with 60 additions and 41 deletions
|
|
@ -6,54 +6,48 @@ import 'package:oto_cli/cli/cli.dart';
|
|||
import 'package:oto_cli/oto/commands/command.dart';
|
||||
import 'package:oto_cli/oto/data/command_data.dart';
|
||||
|
||||
class JsonReader extends Command {
|
||||
@override
|
||||
Future execute(DataCommand command) async {
|
||||
var data = DataJsonReader.fromJson(getParam(command));
|
||||
class JsonBase extends Command {
|
||||
Future setValues(dynamic content, List<DataPair> pair) async {
|
||||
try {
|
||||
Map<String, dynamic> temp = data.json is String
|
||||
? jsonDecode(data.json)
|
||||
: data.json as Map<String, dynamic>;
|
||||
for (var item in data.pair) {
|
||||
Map<String, dynamic> json = content is String
|
||||
? jsonDecode(content)
|
||||
: content as Map<String, dynamic>;
|
||||
var arrReg = RegExp(r'^([a-zA-Z_]+)\[(\d+)\]$');
|
||||
for (var item in pair) {
|
||||
var temp = json;
|
||||
var keys = item.from.split('.');
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
var key = keys[i];
|
||||
if (temp.containsKey(key)) {
|
||||
if (i == keys.length - 1) {
|
||||
await setProperty(item.setValue, temp[keys[i]].toString());
|
||||
break;
|
||||
if(arrReg.hasMatch(key)) {
|
||||
//array case
|
||||
var match = arrReg.firstMatch(key);
|
||||
if(match != null && match.groupCount != 0) {
|
||||
print(match.groupCount);
|
||||
var arrKey = match.group(1);
|
||||
var arrIndex = int.parse(match.group(2)!);
|
||||
if (temp.containsKey(arrKey)) {
|
||||
var arr = temp[arrKey] as List;
|
||||
if(arr.length > arrIndex) {
|
||||
var value = arr[arrIndex];
|
||||
if (i == keys.length - 1) {
|
||||
await setProperty(item.setValue, value.toString());
|
||||
break;
|
||||
} else {
|
||||
temp = value;
|
||||
}
|
||||
} else {
|
||||
throw Exception('Out of range: "$key" in $item');
|
||||
}
|
||||
} else {
|
||||
throw Exception('Not exist key: "$key" in $item');
|
||||
}
|
||||
} else {
|
||||
temp = temp[keys[i]];
|
||||
throw Exception('Not exist key: "$key" in $item');
|
||||
}
|
||||
} else {
|
||||
throw Exception('Not exist key: "$key" in $item');
|
||||
}
|
||||
}
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
throw Exception('Json parsing error: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JsonReaderFile extends Command {
|
||||
@override
|
||||
Future execute(DataCommand command) async {
|
||||
var data = DataJsonReaderFile.fromJson(getParam(command));
|
||||
var path = Command.getAbs(data.path);
|
||||
var file = File(path);
|
||||
if (file.existsSync()) {
|
||||
var content = await file.readAsString();
|
||||
try {
|
||||
Map<String, dynamic> json = jsonDecode(content);
|
||||
for (var item in data.pair) {
|
||||
var temp = json;
|
||||
var keys = item.from.split('.');
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
var key = keys[i];
|
||||
if (temp.containsKey(key)) {
|
||||
if (i == keys.length - 1) {
|
||||
await setProperty(item.setValue, temp[keys[i]].toString());
|
||||
await setProperty(item.setValue, temp[key].toString());
|
||||
break;
|
||||
} else {
|
||||
temp = temp[keys[i]];
|
||||
|
|
@ -62,14 +56,38 @@ class JsonReaderFile extends Command {
|
|||
throw Exception('Not exist key: "$key" in $item');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
throw Exception('Json parsing error: $e');
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
throw Exception('Json parsing error: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JsonReader extends JsonBase {
|
||||
@override
|
||||
Future execute(DataCommand command) async {
|
||||
var data = DataJsonReader.fromJson(getParam(command));
|
||||
await setValues(data.json, data.pair);
|
||||
return complete(0, command);
|
||||
}
|
||||
}
|
||||
|
||||
class JsonReaderFile extends JsonBase {
|
||||
@override
|
||||
Future execute(DataCommand command) async {
|
||||
var data = DataJsonReaderFile.fromJson(getParam(command));
|
||||
var path = Command.getAbs(data.path);
|
||||
var file = File(path);
|
||||
if (file.existsSync()) {
|
||||
await setValues(await file.readAsString(), data.pair);
|
||||
} else {
|
||||
await CLI.println('Json "$path" file not exit. Return value to be null.',
|
||||
color: Color.red);
|
||||
}
|
||||
return complete(0, command);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -109,5 +127,6 @@ class JsonWriterFile extends Command {
|
|||
await CLI.println('Json "$path" file not exit. Return value to be null.',
|
||||
color: Color.red);
|
||||
}
|
||||
return complete(0, command);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue