Skip to content

Commit cb68150

Browse files
committed
protos: remove need for customname on ID fields
Credit to Aaron Lehmann for the implementation from SwarmKit. Signed-off-by: Stephen J Day <stephen.day@docker.com>
1 parent 0dbe46d commit cb68150

File tree

7 files changed

+77
-21
lines changed

7 files changed

+77
-21
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ fmt: ## run go fmt
8888
(echo "$(ONI) please format Go code with 'gofmt -s -w'" && false)
8989
@test -z "$$(find . -path ./vendor -prune -o ! -name timestamp.proto ! -name duration.proto -name '*.proto' -type f -exec grep -Hn -e "^ " {} \; | tee /dev/stderr)" || \
9090
(echo "$(ONI) please indent proto files with tabs only" && false)
91-
@test -z "$$(find . -path ./vendor -prune -o -name '*.proto' -type f -exec grep -EHn "[_ ]id = " {} \; | grep -v gogoproto.customname | tee /dev/stderr)" || \
92-
(echo "$(ONI) id fields in proto files must have a gogoproto.customname set" && false)
9391
@test -z "$$(find . -path ./vendor -prune -o -name '*.proto' -type f -exec grep -Hn "Meta meta = " {} \; | grep -v '(gogoproto.nullable) = false' | tee /dev/stderr)" || \
9492
(echo "$(ONI) meta fields in proto files must have option (gogoproto.nullable) = false" && false)
9593

api/services/execution/execution.proto

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ service ContainerService {
2626
}
2727

2828
message CreateRequest {
29-
string id = 1 [(gogoproto.customname) = "ID"];
29+
string id = 1;
3030
google.protobuf.Any spec = 2;
3131
repeated containerd.v1.types.Mount rootfs = 3;
3232
string runtime = 4;
@@ -37,26 +37,26 @@ message CreateRequest {
3737
}
3838

3939
message CreateResponse {
40-
string id = 1 [(gogoproto.customname) = "ID"];
40+
string id = 1;
4141
uint32 pid = 2;
4242
}
4343

4444
message StartRequest {
45-
string id = 1 [(gogoproto.customname) = "ID"];
45+
string id = 1;
4646
}
4747

4848
message DeleteRequest {
49-
string id = 1 [(gogoproto.customname) = "ID"];
49+
string id = 1;
5050
}
5151

5252
message DeleteResponse {
53-
string id = 1 [(gogoproto.customname) = "ID"];
53+
string id = 1;
5454
uint32 exit_status = 2;
5555
google.protobuf.Timestamp exited_at = 3 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
5656
}
5757

5858
message InfoRequest {
59-
string id = 1 [(gogoproto.customname) = "ID"];
59+
string id = 1;
6060
}
6161

6262
message ListRequest {
@@ -67,7 +67,7 @@ message ListResponse {
6767
}
6868

6969
message KillRequest {
70-
string id = 1 [(gogoproto.customname) = "ID"];
70+
string id = 1;
7171
uint32 signal = 2;
7272
oneof pid_or_all {
7373
bool all = 3;
@@ -79,7 +79,7 @@ message EventsRequest {
7979
}
8080

8181
message ExecRequest {
82-
string id = 1 [(gogoproto.customname) = "ID"];
82+
string id = 1;
8383
bool terminal = 2;
8484
string stdin = 3;
8585
string stdout = 4;
@@ -92,27 +92,27 @@ message ExecResponse {
9292
}
9393

9494
message PtyRequest {
95-
string id = 1 [(gogoproto.customname) = "ID"];
95+
string id = 1;
9696
uint32 pid = 2;
9797
uint32 width = 3;
9898
uint32 height = 4;
9999
}
100100

101101
message CloseStdinRequest {
102-
string id = 1 [(gogoproto.customname) = "ID"];
102+
string id = 1;
103103
uint32 pid = 2;
104104
}
105105

106106
message PauseRequest {
107-
string id = 1 [(gogoproto.customname) = "ID"];
107+
string id = 1;
108108
}
109109

110110
message ResumeRequest {
111-
string id = 1 [(gogoproto.customname) = "ID"];
111+
string id = 1;
112112
}
113113

114114
message ProcessesRequest {
115-
string id = 1 [(gogoproto.customname) = "ID"];
115+
string id = 1;
116116
}
117117

118118
message ProcessesResponse{

api/services/shim/shim.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ service Shim {
3131
}
3232

3333
message CreateRequest {
34-
string id = 1 [(gogoproto.customname) = "ID"];
34+
string id = 1;
3535
string bundle = 2;
3636
string runtime = 3;
3737
bool no_pivot = 4;
@@ -89,7 +89,7 @@ message StateRequest {
8989
}
9090

9191
message StateResponse {
92-
string id = 1 [(gogoproto.customname) = "ID"];
92+
string id = 1;
9393
string bundle = 2;
9494
uint32 pid = 3;
9595
containerd.v1.types.Status status = 4;
@@ -116,7 +116,7 @@ message CloseStdinRequest {
116116
}
117117

118118
message ProcessesRequest {
119-
string id = 1 [(gogoproto.customname) = "ID"];
119+
string id = 1;
120120
}
121121

122122
message ProcessesResponse{

api/types/container/container.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ enum Status {
1515
}
1616

1717
message Container {
18-
string id = 1 [(gogoproto.customname) = "ID"];
18+
string id = 1;
1919
uint32 pid = 2;
2020
Status status = 3;
2121
}
@@ -39,7 +39,7 @@ message User {
3939
}
4040

4141
message Event {
42-
string id = 1 [(gogoproto.customname) = "ID"];
42+
string id = 1;
4343

4444
enum EventType {
4545
EXIT = 0;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
6+
"github.com/gogo/protobuf/gogoproto"
7+
"github.com/gogo/protobuf/proto"
8+
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
9+
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
10+
"github.com/gogo/protobuf/vanity"
11+
)
12+
13+
// CustomNameID preprocess the field, and set the [(gogoproto.customname) = "..."]
14+
// if necessary, in order to avoid setting `gogoproto.customname` manually.
15+
// The automatically assigned name should conform to Golang convention.
16+
func CustomNameID(file *descriptor.FileDescriptorProto) {
17+
18+
f := func(field *descriptor.FieldDescriptorProto) {
19+
// Skip if [(gogoproto.customname) = "..."] has already been set.
20+
if gogoproto.IsCustomName(field) {
21+
return
22+
}
23+
// Skip if embedded
24+
if gogoproto.IsEmbed(field) {
25+
return
26+
}
27+
if field.OneofIndex != nil {
28+
return
29+
}
30+
fieldName := generator.CamelCase(*field.Name)
31+
switch {
32+
case *field.Name == "id":
33+
// id -> ID
34+
fieldName = "ID"
35+
case strings.HasPrefix(*field.Name, "id_"):
36+
// id_some -> IDSome
37+
fieldName = "ID" + fieldName[2:]
38+
case strings.HasSuffix(*field.Name, "_id"):
39+
// some_id -> SomeID
40+
fieldName = fieldName[:len(fieldName)-2] + "ID"
41+
case strings.HasSuffix(*field.Name, "_ids"):
42+
// some_ids -> SomeIDs
43+
fieldName = fieldName[:len(fieldName)-3] + "IDs"
44+
default:
45+
return
46+
}
47+
if field.Options == nil {
48+
field.Options = &descriptor.FieldOptions{}
49+
}
50+
if err := proto.SetExtension(field.Options, gogoproto.E_Customname, &fieldName); err != nil {
51+
panic(err)
52+
}
53+
}
54+
55+
// Iterate through all fields in file
56+
vanity.ForEachFieldExcludingExtensions(file.MessageType, f)
57+
}

cmd/protoc-gen-gogoctrd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
vanity.TurnOnStringerAll,
1818
vanity.TurnOnUnmarshalerAll,
1919
vanity.TurnOnSizerAll,
20+
CustomNameID,
2021
} {
2122
vanity.ForEachFile(files, opt)
2223
}

snapshot/storage/proto/record.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ enum Kind {
1919
// Snapshot defines the storage type for a snapshot in the
2020
// metadata store.
2121
message Snapshot {
22-
uint64 id = 1 [(gogoproto.customname) = "ID"];
22+
uint64 id = 1;
2323
string parent = 2;
2424
Kind kind = 4;
2525
bool readonly = 5;

0 commit comments

Comments
 (0)