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>
This commit is contained in:
Stephen J Day 2017-05-18 18:34:34 -07:00
parent 0dbe46d952
commit cb68150a2f
No known key found for this signature in database
GPG Key ID: 67B3DED84EDC823F
7 changed files with 77 additions and 21 deletions

View File

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

View File

@ -26,7 +26,7 @@ service ContainerService {
} }
message CreateRequest { message CreateRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
google.protobuf.Any spec = 2; google.protobuf.Any spec = 2;
repeated containerd.v1.types.Mount rootfs = 3; repeated containerd.v1.types.Mount rootfs = 3;
string runtime = 4; string runtime = 4;
@ -37,26 +37,26 @@ message CreateRequest {
} }
message CreateResponse { message CreateResponse {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
uint32 pid = 2; uint32 pid = 2;
} }
message StartRequest { message StartRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
} }
message DeleteRequest { message DeleteRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
} }
message DeleteResponse { message DeleteResponse {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
uint32 exit_status = 2; uint32 exit_status = 2;
google.protobuf.Timestamp exited_at = 3 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false]; google.protobuf.Timestamp exited_at = 3 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
} }
message InfoRequest { message InfoRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
} }
message ListRequest { message ListRequest {
@ -67,7 +67,7 @@ message ListResponse {
} }
message KillRequest { message KillRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
uint32 signal = 2; uint32 signal = 2;
oneof pid_or_all { oneof pid_or_all {
bool all = 3; bool all = 3;
@ -79,7 +79,7 @@ message EventsRequest {
} }
message ExecRequest { message ExecRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
bool terminal = 2; bool terminal = 2;
string stdin = 3; string stdin = 3;
string stdout = 4; string stdout = 4;
@ -92,27 +92,27 @@ message ExecResponse {
} }
message PtyRequest { message PtyRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
uint32 pid = 2; uint32 pid = 2;
uint32 width = 3; uint32 width = 3;
uint32 height = 4; uint32 height = 4;
} }
message CloseStdinRequest { message CloseStdinRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
uint32 pid = 2; uint32 pid = 2;
} }
message PauseRequest { message PauseRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
} }
message ResumeRequest { message ResumeRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
} }
message ProcessesRequest { message ProcessesRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
} }
message ProcessesResponse{ message ProcessesResponse{

View File

@ -31,7 +31,7 @@ service Shim {
} }
message CreateRequest { message CreateRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
string bundle = 2; string bundle = 2;
string runtime = 3; string runtime = 3;
bool no_pivot = 4; bool no_pivot = 4;
@ -89,7 +89,7 @@ message StateRequest {
} }
message StateResponse { message StateResponse {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
string bundle = 2; string bundle = 2;
uint32 pid = 3; uint32 pid = 3;
containerd.v1.types.Status status = 4; containerd.v1.types.Status status = 4;
@ -116,7 +116,7 @@ message CloseStdinRequest {
} }
message ProcessesRequest { message ProcessesRequest {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
} }
message ProcessesResponse{ message ProcessesResponse{

View File

@ -15,7 +15,7 @@ enum Status {
} }
message Container { message Container {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
uint32 pid = 2; uint32 pid = 2;
Status status = 3; Status status = 3;
} }
@ -39,7 +39,7 @@ message User {
} }
message Event { message Event {
string id = 1 [(gogoproto.customname) = "ID"]; string id = 1;
enum EventType { enum EventType {
EXIT = 0; EXIT = 0;

View File

@ -0,0 +1,57 @@
package main
import (
"strings"
"github.com/gogo/protobuf/gogoproto"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"github.com/gogo/protobuf/vanity"
)
// CustomNameID preprocess the field, and set the [(gogoproto.customname) = "..."]
// if necessary, in order to avoid setting `gogoproto.customname` manually.
// The automatically assigned name should conform to Golang convention.
func CustomNameID(file *descriptor.FileDescriptorProto) {
f := func(field *descriptor.FieldDescriptorProto) {
// Skip if [(gogoproto.customname) = "..."] has already been set.
if gogoproto.IsCustomName(field) {
return
}
// Skip if embedded
if gogoproto.IsEmbed(field) {
return
}
if field.OneofIndex != nil {
return
}
fieldName := generator.CamelCase(*field.Name)
switch {
case *field.Name == "id":
// id -> ID
fieldName = "ID"
case strings.HasPrefix(*field.Name, "id_"):
// id_some -> IDSome
fieldName = "ID" + fieldName[2:]
case strings.HasSuffix(*field.Name, "_id"):
// some_id -> SomeID
fieldName = fieldName[:len(fieldName)-2] + "ID"
case strings.HasSuffix(*field.Name, "_ids"):
// some_ids -> SomeIDs
fieldName = fieldName[:len(fieldName)-3] + "IDs"
default:
return
}
if field.Options == nil {
field.Options = &descriptor.FieldOptions{}
}
if err := proto.SetExtension(field.Options, gogoproto.E_Customname, &fieldName); err != nil {
panic(err)
}
}
// Iterate through all fields in file
vanity.ForEachFieldExcludingExtensions(file.MessageType, f)
}

View File

@ -17,6 +17,7 @@ func main() {
vanity.TurnOnStringerAll, vanity.TurnOnStringerAll,
vanity.TurnOnUnmarshalerAll, vanity.TurnOnUnmarshalerAll,
vanity.TurnOnSizerAll, vanity.TurnOnSizerAll,
CustomNameID,
} { } {
vanity.ForEachFile(files, opt) vanity.ForEachFile(files, opt)
} }

View File

@ -19,7 +19,7 @@ enum Kind {
// Snapshot defines the storage type for a snapshot in the // Snapshot defines the storage type for a snapshot in the
// metadata store. // metadata store.
message Snapshot { message Snapshot {
uint64 id = 1 [(gogoproto.customname) = "ID"]; uint64 id = 1;
string parent = 2; string parent = 2;
Kind kind = 4; Kind kind = 4;
bool readonly = 5; bool readonly = 5;