-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[protobuf] Add support for expanding template names. (#2861)
There is a bug in the protobuf emitter currently where we use the base name of a template when emitting corresponding models. This patch adds support for extracting an instance name, using friendlyName if it is available or otherwise constructing a name by prepending the name of the argument types to the template's name. Closes #2857 --------- Co-authored-by: Will Temple <will@wtemple.net>
- Loading branch information
1 parent
32050cd
commit a3d6acf
Showing
7 changed files
with
174 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@typespec/protobuf": minor | ||
--- | ||
|
||
Added support for template name expansion to the protobuf emitter. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/protobuf/test/scenarios/templates-error/diagnostics.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/test/main.tsp:15:7 - error @typespec/protobuf/unspeakable-template-argument: template Templated cannot be converted to a Protobuf message because it has an unspeakable argument (try using the '@friendlyName' decorator on the template) | ||
/test/main.tsp:12:40 - error @typespec/protobuf/anonymous-model: anonymous models cannot be used in Protobuf messages |
18 changes: 18 additions & 0 deletions
18
packages/protobuf/test/scenarios/templates-error/input/main.tsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import "@typespec/protobuf"; | ||
|
||
using TypeSpec.Protobuf; | ||
|
||
@package({ | ||
name: "com.azure.Test", | ||
}) | ||
namespace Test; | ||
|
||
@Protobuf.service | ||
interface Service { | ||
foo(...Templated<string>): Templated<{}>; | ||
} | ||
|
||
model Templated<T> { | ||
@field(1) value: T; | ||
@field(2) other: int32; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import "@typespec/protobuf"; | ||
|
||
using TypeSpec.Protobuf; | ||
|
||
@package({ | ||
name: "com.azure.Test", | ||
}) | ||
namespace Test; | ||
|
||
@Protobuf.service | ||
interface Service { | ||
foo(...Templated<string>): Templated<uint32>; | ||
|
||
listOperation(...List<string>): List<uint32>; | ||
|
||
sameTemplate(...Templated<string>): void; | ||
} | ||
|
||
@friendlyName("{name}List", T) | ||
model List<T> { | ||
@field(1) values: T[]; | ||
} | ||
|
||
model Templated<T> { | ||
@field(1) value: T; | ||
@field(2) other: int32; | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/protobuf/test/scenarios/templates/output/@typespec/protobuf/com/azure/Test.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Generated by Microsoft TypeSpec | ||
|
||
syntax = "proto3"; | ||
|
||
package com.azure.Test; | ||
|
||
import "google/protobuf/empty.proto"; | ||
|
||
message StringTemplated { | ||
string value = 1; | ||
int32 other = 2; | ||
} | ||
|
||
message Uint32Templated { | ||
uint32 value = 1; | ||
int32 other = 2; | ||
} | ||
|
||
message StringList { | ||
repeated string values = 1; | ||
} | ||
|
||
message Uint32List { | ||
repeated uint32 values = 1; | ||
} | ||
|
||
service Service { | ||
rpc Foo(StringTemplated) returns (Uint32Templated); | ||
rpc ListOperation(StringList) returns (Uint32List); | ||
rpc SameTemplate(StringTemplated) returns (google.protobuf.Empty); | ||
} |