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
3 changes: 3 additions & 0 deletions protoc_plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* Fix factory argument types for protobuf `Map` fields. ([#975])
* Fix import order changes when files are passed in different order to `protoc`.
([#952])
* Add fromDart() and toDart() methods to convert between core Duration and proto
Duration ([#986])

[#975]: https://github.com/google/protobuf.dart/issues/975
[#952]: https://github.com/google/protobuf.dart/issues/952
[#986]: https://github.com/google/protobuf.dart/issues/986

## 22.0.1

Expand Down
10 changes: 5 additions & 5 deletions protoc_plugin/lib/src/base_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ class BaseType {
'SF3', '$coreImportPrefix.int', r'$_setSignedInt32', null);
case FieldDescriptorProto_Type.TYPE_INT64:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_INT64, '6',
'$_fixnumImportPrefix.Int64', r'$_setInt64', null);
'$fixnumImportPrefix.Int64', r'$_setInt64', null);
case FieldDescriptorProto_Type.TYPE_UINT64:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_UINT64, 'U6',
'$_fixnumImportPrefix.Int64', r'$_setInt64', null);
'$fixnumImportPrefix.Int64', r'$_setInt64', null);
case FieldDescriptorProto_Type.TYPE_SINT64:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_SINT64, 'S6',
'$_fixnumImportPrefix.Int64', r'$_setInt64', null);
'$fixnumImportPrefix.Int64', r'$_setInt64', null);
case FieldDescriptorProto_Type.TYPE_FIXED64:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_FIXED64, 'F6',
'$_fixnumImportPrefix.Int64', r'$_setInt64', null);
'$fixnumImportPrefix.Int64', r'$_setInt64', null);
case FieldDescriptorProto_Type.TYPE_SFIXED64:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_SFIXED64,
'SF6', '$_fixnumImportPrefix.Int64', r'$_setInt64', null);
'SF6', '$fixnumImportPrefix.Int64', r'$_setInt64', null);
case FieldDescriptorProto_Type.TYPE_STRING:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_STRING, 'S',
'$coreImportPrefix.String', r'$_setString', null);
Expand Down
3 changes: 1 addition & 2 deletions protoc_plugin/lib/src/file_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const String _convertImportPrefix = r'$convert';
const String _convertImportUrl = 'dart:convert';

const String _coreImportUrl = 'dart:core';
const String _fixnumImportPrefix = r'$fixnum';
const String _grpcImportUrl = 'package:grpc/service_api.dart';
const String _protobufImportUrl = 'package:protobuf/protobuf.dart';

Expand Down Expand Up @@ -318,7 +317,7 @@ class FileGenerator extends ProtobufContainer {

if (_needsFixnumImport) {
importWriter.addImport('package:fixnum/fixnum.dart',
prefix: _fixnumImportPrefix);
prefix: fixnumImportPrefix);
}

if (_needsProtobufImport) {
Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/lib/src/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ class MessageGenerator extends ProtobufContainer {
}
return '\$_getI($index, $defaultExpr)';
}
if (fieldType == '$_fixnumImportPrefix.Int64' && defaultExpr == 'null') {
if (fieldType == '$fixnumImportPrefix.Int64' && defaultExpr == 'null') {
return '\$_getI64($index)';
}
if (defaultExpr == 'null') {
Expand Down
8 changes: 4 additions & 4 deletions protoc_plugin/lib/src/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ProtobufField {

/// Whether this field uses the Int64 from the fixnum package.
bool get needsFixnumImport =>
baseType.unprefixed == '$_fixnumImportPrefix.Int64';
baseType.unprefixed == '$fixnumImportPrefix.Int64';

/// Whether this field is a map field definition:
/// `map<key_type, value_type> map_field = N`.
Expand Down Expand Up @@ -302,8 +302,8 @@ class ProtobufField {
break;
}
} else {
if (makeDefault == '$_fixnumImportPrefix.Int64.ZERO' &&
type == '$_fixnumImportPrefix.Int64' &&
if (makeDefault == '$fixnumImportPrefix.Int64.ZERO' &&
type == '$fixnumImportPrefix.Int64' &&
typeConstant == '$protobufImportPrefix.PbFieldType.O6') {
invocation = 'aInt64';
} else {
Expand Down Expand Up @@ -388,7 +388,7 @@ class ProtobufField {
case FieldDescriptorProto_Type.TYPE_SFIXED64:
var value = '0';
if (descriptor.hasDefaultValue()) value = descriptor.defaultValue;
if (value == '0') return '$_fixnumImportPrefix.Int64.ZERO';
if (value == '0') return '$fixnumImportPrefix.Int64.ZERO';
return "$protobufImportPrefix.parseLongInt('$value')";
case FieldDescriptorProto_Type.TYPE_STRING:
return _getDefaultAsStringExpr(null);
Expand Down
1 change: 1 addition & 0 deletions protoc_plugin/lib/src/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const protobufImportPrefix = r'$pb';
const asyncImportPrefix = r'$async';
const coreImportPrefix = r'$core';
const grpcImportPrefix = r'$grpc';
const fixnumImportPrefix = r'$fixnum';
const mixinImportPrefix = r'$mixin';

extension FileDescriptorProtoExt on FileGenerator {
Expand Down
18 changes: 18 additions & 0 deletions protoc_plugin/lib/src/well_known_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ static Timestamp fromDateTime($coreImportPrefix.DateTime dateTime) {
'google.protobuf.Duration': PbMixin(
'DurationMixin',
importFrom: _wellKnownImportPath,
injectedHelpers: [
'''
/// Converts the [Duration] to [$coreImportPrefix.Duration].
///
/// This is a lossy conversion, as [$coreImportPrefix.Duration] is limited to [int]
/// microseconds and also does not support nanosecond precision.
$coreImportPrefix.Duration toDart() =>
$coreImportPrefix.Duration(
seconds: seconds.toInt(),
microseconds: nanos ~/ 1000,
);

/// Creates a new instance from [$coreImportPrefix.Duration].
static Duration fromDart($coreImportPrefix.Duration duration) => Duration()
..seconds = $fixnumImportPrefix.Int64(duration.inSeconds)
..nanos = (duration.inMicroseconds % $coreImportPrefix.Duration.microsecondsPerSecond) * 1000;
''',
],
hasProto3JsonHelpers: true,
),
'google.protobuf.Struct': PbMixin(
Expand Down
47 changes: 47 additions & 0 deletions protoc_plugin/test/duration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:fixnum/fixnum.dart';
import 'package:test/test.dart';

import '../out/protos/google/protobuf/duration.pb.dart' as pb;

void main() {
test('core duration -> proto duration', () {
final coreDuration = Duration(
days: 1,
hours: 2,
minutes: 3,
seconds: 4,
milliseconds: 5,
microseconds: 6,
);

final protoDuration = pb.Duration.fromDart(coreDuration);

expect(protoDuration.seconds, Int64(86400 + 3600 * 2 + 60 * 3 + 4));
expect(protoDuration.nanos, 5006000);
});

test('core duration -> proto duration -> core duration', () {
final coreDuration = Duration(
days: 1,
hours: 2,
minutes: 3,
seconds: 4,
milliseconds: 5,
microseconds: 6,
);

expect(pb.Duration.fromDart(coreDuration).toDart(), coreDuration);
});

test('proto duration -> core duration -> proto duration', () {
final protoDuration = pb.Duration()
..seconds = Int64(987654321)
..nanos = 999999000;

expect(pb.Duration.fromDart(protoDuration.toDart()), protoDuration);
});
}