mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-08-06 15:21:21 +08:00
MdYamlDoc: Serialize lists and maps
This commit is contained in:
@ -10,6 +10,7 @@ import 'package:flutter/foundation.dart';
|
|||||||
|
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
import 'package:fixnum/fixnum.dart' as fixnum;
|
import 'package:fixnum/fixnum.dart' as fixnum;
|
||||||
|
import 'package:yaml/yaml.dart';
|
||||||
|
|
||||||
import 'package:gitjournal/generated/core.pb.dart' as pb;
|
import 'package:gitjournal/generated/core.pb.dart' as pb;
|
||||||
import 'package:gitjournal/utils/datetime.dart';
|
import 'package:gitjournal/utils/datetime.dart';
|
||||||
@ -84,6 +85,24 @@ pb.Union _toUnion(dynamic val) {
|
|||||||
return pb.Union(dateValue: val.toProtoBuf());
|
return pb.Union(dateValue: val.toProtoBuf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (val is YamlList || val is List) {
|
||||||
|
var list = <pb.Union>[];
|
||||||
|
for (var v in val) {
|
||||||
|
list.add(_toUnion(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
return pb.Union(listValue: list);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (val is Map) {
|
||||||
|
var map = <String, pb.Union>{};
|
||||||
|
for (var e in val.entries) {
|
||||||
|
map[e.key.toString()] = _toUnion(e.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pb.Union(mapValue: map);
|
||||||
|
}
|
||||||
|
|
||||||
throw Exception(
|
throw Exception(
|
||||||
"Type cannot be converted to Protobuf Union - ${val.runtimeType}");
|
"Type cannot be converted to Protobuf Union - ${val.runtimeType}");
|
||||||
}
|
}
|
||||||
@ -97,6 +116,18 @@ dynamic _fromUnion(pb.Union u) {
|
|||||||
return u.booleanValue;
|
return u.booleanValue;
|
||||||
} else if (u.hasDateValue()) {
|
} else if (u.hasDateValue()) {
|
||||||
return u.dateValue.toDateTime();
|
return u.dateValue.toDateTime();
|
||||||
|
} else if (u.listValue.isNotEmpty) {
|
||||||
|
var list = <dynamic>[];
|
||||||
|
for (var v in u.listValue) {
|
||||||
|
list.add(_fromUnion(v));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
} else if (u.mapValue.isNotEmpty) {
|
||||||
|
var map = <String, dynamic>{};
|
||||||
|
for (var e in u.mapValue.entries) {
|
||||||
|
map[e.key] = _fromUnion(e.value);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw Exception("Type cannot be converted from Protobuf Union");
|
throw Exception("Type cannot be converted from Protobuf Union");
|
||||||
|
@ -678,6 +678,25 @@ class Union extends $pb.GeneratedMessage {
|
|||||||
? ''
|
? ''
|
||||||
: 'intValue',
|
: 'intValue',
|
||||||
protoName: 'intValue')
|
protoName: 'intValue')
|
||||||
|
..pc<Union>(
|
||||||
|
5,
|
||||||
|
const $core.bool.fromEnvironment('protobuf.omit_field_names')
|
||||||
|
? ''
|
||||||
|
: 'listValue',
|
||||||
|
$pb.PbFieldType.PM,
|
||||||
|
protoName: 'listValue',
|
||||||
|
subBuilder: Union.create)
|
||||||
|
..m<$core.String, Union>(
|
||||||
|
6,
|
||||||
|
const $core.bool.fromEnvironment('protobuf.omit_field_names')
|
||||||
|
? ''
|
||||||
|
: 'mapValue',
|
||||||
|
protoName: 'mapValue',
|
||||||
|
entryClassName: 'Union.MapValueEntry',
|
||||||
|
keyFieldType: $pb.PbFieldType.OS,
|
||||||
|
valueFieldType: $pb.PbFieldType.OM,
|
||||||
|
valueCreator: Union.create,
|
||||||
|
packageName: const $pb.PackageName('gitjournal'))
|
||||||
..hasRequiredFields = false;
|
..hasRequiredFields = false;
|
||||||
|
|
||||||
Union._() : super();
|
Union._() : super();
|
||||||
@ -686,6 +705,8 @@ class Union extends $pb.GeneratedMessage {
|
|||||||
$core.String? stringValue,
|
$core.String? stringValue,
|
||||||
DateTimeAnyTz? dateValue,
|
DateTimeAnyTz? dateValue,
|
||||||
$fixnum.Int64? intValue,
|
$fixnum.Int64? intValue,
|
||||||
|
$core.Iterable<Union>? listValue,
|
||||||
|
$core.Map<$core.String, Union>? mapValue,
|
||||||
}) {
|
}) {
|
||||||
final _result = create();
|
final _result = create();
|
||||||
if (booleanValue != null) {
|
if (booleanValue != null) {
|
||||||
@ -700,6 +721,12 @@ class Union extends $pb.GeneratedMessage {
|
|||||||
if (intValue != null) {
|
if (intValue != null) {
|
||||||
_result.intValue = intValue;
|
_result.intValue = intValue;
|
||||||
}
|
}
|
||||||
|
if (listValue != null) {
|
||||||
|
_result.listValue.addAll(listValue);
|
||||||
|
}
|
||||||
|
if (mapValue != null) {
|
||||||
|
_result.mapValue.addAll(mapValue);
|
||||||
|
}
|
||||||
return _result;
|
return _result;
|
||||||
}
|
}
|
||||||
factory Union.fromBuffer($core.List<$core.int> i,
|
factory Union.fromBuffer($core.List<$core.int> i,
|
||||||
@ -781,6 +808,12 @@ class Union extends $pb.GeneratedMessage {
|
|||||||
$core.bool hasIntValue() => $_has(3);
|
$core.bool hasIntValue() => $_has(3);
|
||||||
@$pb.TagNumber(4)
|
@$pb.TagNumber(4)
|
||||||
void clearIntValue() => clearField(4);
|
void clearIntValue() => clearField(4);
|
||||||
|
|
||||||
|
@$pb.TagNumber(5)
|
||||||
|
$core.List<Union> get listValue => $_getList(4);
|
||||||
|
|
||||||
|
@$pb.TagNumber(6)
|
||||||
|
$core.Map<$core.String, Union> get mapValue => $_getMap(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
class DateTimeAnyTz extends $pb.GeneratedMessage {
|
class DateTimeAnyTz extends $pb.GeneratedMessage {
|
||||||
|
@ -270,15 +270,49 @@ const Union$json = const {
|
|||||||
'10': 'dateValue'
|
'10': 'dateValue'
|
||||||
},
|
},
|
||||||
const {'1': 'intValue', '3': 4, '4': 1, '5': 3, '9': 0, '10': 'intValue'},
|
const {'1': 'intValue', '3': 4, '4': 1, '5': 3, '9': 0, '10': 'intValue'},
|
||||||
|
const {
|
||||||
|
'1': 'listValue',
|
||||||
|
'3': 5,
|
||||||
|
'4': 3,
|
||||||
|
'5': 11,
|
||||||
|
'6': '.gitjournal.Union',
|
||||||
|
'10': 'listValue'
|
||||||
|
},
|
||||||
|
const {
|
||||||
|
'1': 'mapValue',
|
||||||
|
'3': 6,
|
||||||
|
'4': 3,
|
||||||
|
'5': 11,
|
||||||
|
'6': '.gitjournal.Union.MapValueEntry',
|
||||||
|
'10': 'mapValue'
|
||||||
|
},
|
||||||
],
|
],
|
||||||
|
'3': const [Union_MapValueEntry$json],
|
||||||
'8': const [
|
'8': const [
|
||||||
const {'1': 'UnionOneof'},
|
const {'1': 'UnionOneof'},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@$core.Deprecated('Use unionDescriptor instead')
|
||||||
|
const Union_MapValueEntry$json = const {
|
||||||
|
'1': 'MapValueEntry',
|
||||||
|
'2': const [
|
||||||
|
const {'1': 'key', '3': 1, '4': 1, '5': 9, '10': 'key'},
|
||||||
|
const {
|
||||||
|
'1': 'value',
|
||||||
|
'3': 2,
|
||||||
|
'4': 1,
|
||||||
|
'5': 11,
|
||||||
|
'6': '.gitjournal.Union',
|
||||||
|
'10': 'value'
|
||||||
|
},
|
||||||
|
],
|
||||||
|
'7': const {'7': true},
|
||||||
|
};
|
||||||
|
|
||||||
/// Descriptor for `Union`. Decode as a `google.protobuf.DescriptorProto`.
|
/// Descriptor for `Union`. Decode as a `google.protobuf.DescriptorProto`.
|
||||||
final $typed_data.Uint8List unionDescriptor = $convert.base64Decode(
|
final $typed_data.Uint8List unionDescriptor = $convert.base64Decode(
|
||||||
'CgVVbmlvbhIkCgxib29sZWFuVmFsdWUYASABKAhIAFIMYm9vbGVhblZhbHVlEiIKC3N0cmluZ1ZhbHVlGAIgASgJSABSC3N0cmluZ1ZhbHVlEjkKCWRhdGVWYWx1ZRgDIAEoCzIZLmdpdGpvdXJuYWwuRGF0ZVRpbWVBbnlUekgAUglkYXRlVmFsdWUSHAoIaW50VmFsdWUYBCABKANIAFIIaW50VmFsdWVCDAoKVW5pb25PbmVvZg==');
|
'CgVVbmlvbhIkCgxib29sZWFuVmFsdWUYASABKAhIAFIMYm9vbGVhblZhbHVlEiIKC3N0cmluZ1ZhbHVlGAIgASgJSABSC3N0cmluZ1ZhbHVlEjkKCWRhdGVWYWx1ZRgDIAEoCzIZLmdpdGpvdXJuYWwuRGF0ZVRpbWVBbnlUekgAUglkYXRlVmFsdWUSHAoIaW50VmFsdWUYBCABKANIAFIIaW50VmFsdWUSLwoJbGlzdFZhbHVlGAUgAygLMhEuZ2l0am91cm5hbC5VbmlvblIJbGlzdFZhbHVlEjsKCG1hcFZhbHVlGAYgAygLMh8uZ2l0am91cm5hbC5Vbmlvbi5NYXBWYWx1ZUVudHJ5UghtYXBWYWx1ZRpOCg1NYXBWYWx1ZUVudHJ5EhAKA2tleRgBIAEoCVIDa2V5EicKBXZhbHVlGAIgASgLMhEuZ2l0am91cm5hbC5VbmlvblIFdmFsdWU6AjgBQgwKClVuaW9uT25lb2Y=');
|
||||||
@$core.Deprecated('Use dateTimeAnyTzDescriptor instead')
|
@$core.Deprecated('Use dateTimeAnyTzDescriptor instead')
|
||||||
const DateTimeAnyTz$json = const {
|
const DateTimeAnyTz$json = const {
|
||||||
'1': 'DateTimeAnyTz',
|
'1': 'DateTimeAnyTz',
|
||||||
|
@ -63,6 +63,9 @@ message Union {
|
|||||||
DateTimeAnyTz dateValue = 3;
|
DateTimeAnyTz dateValue = 3;
|
||||||
int64 intValue = 4;
|
int64 intValue = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
repeated Union listValue = 5;
|
||||||
|
map<string, Union> mapValue = 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,26 +6,35 @@
|
|||||||
|
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import 'package:dart_git/utils/date_time.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
import 'package:gitjournal/core/markdown/md_yaml_doc.dart';
|
import 'package:gitjournal/core/markdown/md_yaml_doc.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('Equality', () {
|
test('Equality', () {
|
||||||
|
var now = GDateTime(const Duration(hours: 1), 2010, 1, 2, 3, 4, 5);
|
||||||
|
|
||||||
// ignore: prefer_collection_literals
|
// ignore: prefer_collection_literals
|
||||||
var aProps = LinkedHashMap<String, dynamic>();
|
var aProps = LinkedHashMap<String, dynamic>();
|
||||||
aProps['a'] = 1;
|
aProps['a'] = 1;
|
||||||
aProps['title'] = "Foo";
|
aProps['title'] = "Foo";
|
||||||
aProps['list'] = ["Foo", "Bar", 1];
|
aProps['list'] = ["Foo", "Bar", 1];
|
||||||
|
aProps['map'] = <String, dynamic>{'a': 5};
|
||||||
|
aProps['date'] = now;
|
||||||
|
|
||||||
// ignore: prefer_collection_literals
|
// ignore: prefer_collection_literals
|
||||||
var bProps = LinkedHashMap<String, dynamic>();
|
var bProps = LinkedHashMap<String, dynamic>();
|
||||||
bProps['a'] = 1;
|
bProps['a'] = 1;
|
||||||
bProps['title'] = "Foo";
|
bProps['title'] = "Foo";
|
||||||
bProps['list'] = ["Foo", "Bar", 1];
|
bProps['list'] = ["Foo", "Bar", 1];
|
||||||
|
bProps['map'] = <String, dynamic>{'a': 5};
|
||||||
|
bProps['date'] = now;
|
||||||
|
|
||||||
var a = MdYamlDoc(body: "a", props: aProps);
|
var a = MdYamlDoc(body: "a", props: aProps);
|
||||||
var b = MdYamlDoc(body: "a", props: bProps);
|
var b = MdYamlDoc(body: "a", props: bProps);
|
||||||
expect(a, b);
|
expect(a, b);
|
||||||
|
|
||||||
|
expect(a, MdYamlDoc.fromProtoBuf(a.toProtoBuf()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user