Add basic support for all google.protobuf types

This commit is contained in:
boukeversteegh 2020-05-29 15:37:14 +02:00
parent 53ce1255d3
commit 2a3e1e1827
3 changed files with 13 additions and 1 deletions

View File

@ -88,6 +88,14 @@ def get_ref_type(
cased = [stringcase.pascalcase(part) for part in parts] cased = [stringcase.pascalcase(part) for part in parts]
type_name = f'"{"".join(cased)}"' type_name = f'"{"".join(cased)}"'
# Use precompiled classes for google.protobuf.* objects
if type_name.startswith("google.protobuf.") and type_name.count(".") == 2:
type_name = type_name.rsplit(".", maxsplit=1)[1]
import_package = "betterproto.lib.google.protobuf"
import_alias = safe_snake_case(import_package)
imports.add(f"import {import_package} as {import_alias}")
return f"{import_alias}.{type_name}"
if "." in type_name: if "." in type_name:
# This is imported from another package. No need # This is imported from another package. No need
# to use a forward ref and we need to add the import. # to use a forward ref and we need to add the import.

View File

@ -1,5 +1,7 @@
{ {
"maybe": false, "maybe": false,
"ts": "1972-01-01T10:00:20.021Z", "ts": "1972-01-01T10:00:20.021Z",
"duration": "1.200s" "duration": "1.200s",
"important": 10,
"empty": {}
} }

View File

@ -3,10 +3,12 @@ syntax = "proto3";
import "google/protobuf/duration.proto"; import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto"; import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto"; import "google/protobuf/wrappers.proto";
import "google/protobuf/empty.proto";
message Test { message Test {
google.protobuf.BoolValue maybe = 1; google.protobuf.BoolValue maybe = 1;
google.protobuf.Timestamp ts = 2; google.protobuf.Timestamp ts = 2;
google.protobuf.Duration duration = 3; google.protobuf.Duration duration = 3;
google.protobuf.Int32Value important = 4; google.protobuf.Int32Value important = 4;
google.protobuf.Empty empty = 5;
} }