Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 5.0.0-wip

## 4.2.0

* Internal refactoring to split the package into libraries. This allows
Expand All @@ -11,6 +13,9 @@
* Improve performance of `GeneratedMessage` members: `writeToJsonMap`,
`writeToJson`, `mergeFromJson`, `mergeFromJsonMap`. ([#1028])

* Remove `BuilderInfo.fromProto3Json` and `BuilderInfo.toProto3Json` as a part
of an internal refactoring.

[#1026]: https://github.com/google/protobuf.dart/pull/1026
[#1027]: https://github.com/google/protobuf.dart/pull/1027
[#1028]: https://github.com/google/protobuf.dart/pull/1028
Expand Down
4 changes: 3 additions & 1 deletion protobuf/lib/protobuf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export 'src/protobuf/internal.dart'
GeneratedMessageInternalExtension,
MapFieldInfoInternalExtension,
mapKeyFieldNumber,
mapValueFieldNumber;
mapValueFieldNumber,
mergeFromProto3JsonAny,
writeToProto3JsonAny;
17 changes: 5 additions & 12 deletions protobuf/lib/src/protobuf/builder_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,17 @@ class BuilderInfo {
List<FieldInfo>? _sortedByTag;

// For well-known types.
final Object? Function(GeneratedMessage message, TypeRegistry typeRegistry)?
toProto3Json;
final Function(
GeneratedMessage targetMessage,
Object json,
TypeRegistry typeRegistry,
JsonParsingContext context,
)?
fromProto3Json;
final WellKnownType? _wellKnownType;

final CreateBuilderFunc? createEmptyInstance;

BuilderInfo(
String? messageName, {
PackageName package = const PackageName(''),
this.createEmptyInstance,
this.toProto3Json,
this.fromProto3Json,
}) : qualifiedMessageName = '${package.prefix}$messageName';
WellKnownType? wellKnownType,
}) : qualifiedMessageName = '${package.prefix}$messageName',
_wellKnownType = wellKnownType;

void add<T>(
int tagNumber,
Expand Down
1 change: 1 addition & 0 deletions protobuf/lib/src/protobuf/internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:meta/meta.dart' show UseResult;
import 'consts.dart';
import 'json/json.dart' as json_lib;
import 'json_parsing_context.dart';
import 'mixins/well_known.dart';
import 'permissive_compare.dart';
import 'type_registry.dart';
import 'utils.dart';
Expand Down
54 changes: 30 additions & 24 deletions protobuf/lib/src/protobuf/mixins/well_known.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@ import 'dart:convert';

import 'package:fixnum/fixnum.dart';

import '../../../protobuf.dart';
import '../internal.dart';
import '../json_parsing_context.dart';

/// The set of well known protobuf message types which may have customized
/// serialization logic.
enum WellKnownType {
any,
timestamp,
duration,
struct,
value,
listValue,
fieldMask,
doubleValue,
floatValue,
int64Value,
uint64Value,
int32Value,
uint32Value,
boolValue,
stringValue,
bytesValue,
}

mixin AnyMixin implements GeneratedMessage {
String get typeUrl;
set typeUrl(String value);
Expand Down Expand Up @@ -98,14 +119,7 @@ mixin AnyMixin implements GeneratedMessage {
);
}
final unpacked = info.createEmptyInstance!()..mergeFromBuffer(any.value);
final proto3Json = unpacked.toProto3Json(typeRegistry: typeRegistry);
if (info.toProto3Json == null) {
final map = proto3Json as Map<String, dynamic>;
map['@type'] = any.typeUrl;
return map;
} else {
return {'@type': any.typeUrl, 'value': proto3Json};
}
return writeToProto3JsonAny(unpacked.fieldSet, any.typeUrl, typeRegistry);
}

static void fromProto3JsonHelper(
Expand Down Expand Up @@ -133,21 +147,13 @@ mixin AnyMixin implements GeneratedMessage {
);
}

final Object? subJson =
info.fromProto3Json == null
// TODO(sigurdm): avoid cloning [object] here.
? (Map<String, dynamic>.from(object)..remove('@type'))
: object['value'];
// TODO(sigurdm): We lose [context.path].
final packedMessage =
info.createEmptyInstance!()..mergeFromProto3Json(
subJson,
typeRegistry: typeRegistry,
supportNamesWithUnderscores: context.supportNamesWithUnderscores,
ignoreUnknownFields: context.ignoreUnknownFields,
permissiveEnums: context.permissiveEnums,
);

final packedMessage = info.createEmptyInstance!();
mergeFromProto3JsonAny(
json,
packedMessage.fieldSet,
typeRegistry,
context,
);
any.value = packedMessage.writeToBuffer();
any.typeUrl = typeUrl;
} else {
Expand Down
Loading
Loading