diff --git a/Makefile b/Makefile index b668d5683..a32bb5b0d 100644 --- a/Makefile +++ b/Makefile @@ -88,8 +88,6 @@ fmt: ## run go fmt (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)" || \ (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)" || \ (echo "$(ONI) meta fields in proto files must have option (gogoproto.nullable) = false" && false) diff --git a/api/services/execution/execution.proto b/api/services/execution/execution.proto index 36a153447..f982609ca 100644 --- a/api/services/execution/execution.proto +++ b/api/services/execution/execution.proto @@ -26,7 +26,7 @@ service ContainerService { } message CreateRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; google.protobuf.Any spec = 2; repeated containerd.v1.types.Mount rootfs = 3; string runtime = 4; @@ -37,26 +37,26 @@ message CreateRequest { } message CreateResponse { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; uint32 pid = 2; } message StartRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; } message DeleteRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; } message DeleteResponse { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; uint32 exit_status = 2; google.protobuf.Timestamp exited_at = 3 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false]; } message InfoRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; } message ListRequest { @@ -67,7 +67,7 @@ message ListResponse { } message KillRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; uint32 signal = 2; oneof pid_or_all { bool all = 3; @@ -79,7 +79,7 @@ message EventsRequest { } message ExecRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; bool terminal = 2; string stdin = 3; string stdout = 4; @@ -92,27 +92,27 @@ message ExecResponse { } message PtyRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; uint32 pid = 2; uint32 width = 3; uint32 height = 4; } message CloseStdinRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; uint32 pid = 2; } message PauseRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; } message ResumeRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; } message ProcessesRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; } message ProcessesResponse{ diff --git a/api/services/shim/shim.proto b/api/services/shim/shim.proto index 2aa12c682..8c2afac04 100644 --- a/api/services/shim/shim.proto +++ b/api/services/shim/shim.proto @@ -31,7 +31,7 @@ service Shim { } message CreateRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; string bundle = 2; string runtime = 3; bool no_pivot = 4; @@ -89,7 +89,7 @@ message StateRequest { } message StateResponse { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; string bundle = 2; uint32 pid = 3; containerd.v1.types.Status status = 4; @@ -116,7 +116,7 @@ message CloseStdinRequest { } message ProcessesRequest { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; } message ProcessesResponse{ diff --git a/api/types/container/container.proto b/api/types/container/container.proto index 3ae378615..c05a21f52 100644 --- a/api/types/container/container.proto +++ b/api/types/container/container.proto @@ -15,7 +15,7 @@ enum Status { } message Container { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; uint32 pid = 2; Status status = 3; } @@ -39,7 +39,7 @@ message User { } message Event { - string id = 1 [(gogoproto.customname) = "ID"]; + string id = 1; enum EventType { EXIT = 0; diff --git a/cmd/protoc-gen-gogoctrd/customnameid.go b/cmd/protoc-gen-gogoctrd/customnameid.go new file mode 100644 index 000000000..c41e8db91 --- /dev/null +++ b/cmd/protoc-gen-gogoctrd/customnameid.go @@ -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) +} diff --git a/cmd/protoc-gen-gogoctrd/main.go b/cmd/protoc-gen-gogoctrd/main.go index cadf0364f..21f5c2366 100644 --- a/cmd/protoc-gen-gogoctrd/main.go +++ b/cmd/protoc-gen-gogoctrd/main.go @@ -17,6 +17,7 @@ func main() { vanity.TurnOnStringerAll, vanity.TurnOnUnmarshalerAll, vanity.TurnOnSizerAll, + CustomNameID, } { vanity.ForEachFile(files, opt) } diff --git a/snapshot/storage/proto/record.proto b/snapshot/storage/proto/record.proto index 8893a1771..feb486017 100644 --- a/snapshot/storage/proto/record.proto +++ b/snapshot/storage/proto/record.proto @@ -19,7 +19,7 @@ enum Kind { // Snapshot defines the storage type for a snapshot in the // metadata store. message Snapshot { - uint64 id = 1 [(gogoproto.customname) = "ID"]; + uint64 id = 1; string parent = 2; Kind kind = 4; bool readonly = 5;