diff --git a/Makefile b/Makefile index cdfbc05..69b50ca 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ testdata: protoc -I ./test/testdata/. \ --grpc-gateway-ts_out ./test/testdata/ \ --grpc-gateway-ts_opt logtostderr=true \ - log.proto environment.proto datasource/datasource.proto + log.proto environment.proto names.proto datasource/datasource.proto .PHONY: lint lint: diff --git a/README.md b/README.md index c4063b3..ef8842d 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ 2. Supports both one way and server side streaming gRPC calls. 3. POJO request construction guarded by message type definitions, which is way easier compare to `grpc-web`. 4. No need to use swagger/open api to generate client code for the web. +5. Fixes inconsistent field naming when fields contain numbers, e.g. `k8s_field` --> `k8sField`. ### Changes made since the fork diff --git a/generator/strings.go b/generator/strings.go new file mode 100644 index 0000000..dd67651 --- /dev/null +++ b/generator/strings.go @@ -0,0 +1,25 @@ +package generator + +// JSONCamelCase converts a snake_case identifier to a camelCase identifier, +// according to the protobuf JSON specification. +// +// Copied from: google.golang.org/protobuf/internal/strs +func JSONCamelCase(s string) string { + var b []byte + var wasUnderscore bool + for i := 0; i < len(s); i++ { // proto identifiers are always ASCII + c := s[i] + if c != '_' { + if wasUnderscore && isASCIILower(c) { + c -= 'a' - 'A' // convert to uppercase + } + b = append(b, c) + } + wasUnderscore = c == '_' + } + return string(b) +} + +func isASCIILower(c byte) bool { + return 'a' <= c && c <= 'z' +} diff --git a/generator/template.go b/generator/template.go index b45d4c1..997fdb8 100644 --- a/generator/template.go +++ b/generator/template.go @@ -11,7 +11,6 @@ import ( log "github.com/sirupsen/logrus" "github.com/Masterminds/sprig" - "github.com/iancoleman/strcase" "github.com/dpup/protoc-gen-grpc-gateway-ts/data" "github.com/dpup/protoc-gen-grpc-gateway-ts/registry" @@ -151,7 +150,7 @@ func fieldName(r *registry.Registry) func(name string) string { if r.UseProtoNames { return name } - return strcase.ToLowerCamel(name) + return JSONCamelCase(name) } } diff --git a/generator/template_test.go b/generator/template_test.go new file mode 100644 index 0000000..c99344f --- /dev/null +++ b/generator/template_test.go @@ -0,0 +1,37 @@ +package generator + +import ( + "testing" + + "github.com/dpup/protoc-gen-grpc-gateway-ts/registry" + "github.com/stretchr/testify/assert" +) + +func TestFieldName(t *testing.T) { + tests := []struct { + useProtoNames bool + input string + want string + }{ + {useProtoNames: false, input: "k8s_field", want: "k8sField"}, + + {useProtoNames: false, input: "foo_bar", want: "fooBar"}, + {useProtoNames: false, input: "foobar", want: "foobar"}, + {useProtoNames: false, input: "foo_bar_baz", want: "fooBarBaz"}, + + {useProtoNames: false, input: "foobar3", want: "foobar3"}, + {useProtoNames: false, input: "foo3bar", want: "foo3bar"}, + {useProtoNames: false, input: "foo3_bar", want: "foo3Bar"}, + {useProtoNames: false, input: "foo_3bar", want: "foo3bar"}, + {useProtoNames: false, input: "foo_3_bar", want: "foo3Bar"}, + } + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + r := ®istry.Registry{UseProtoNames: tt.useProtoNames} + fn := fieldName(r) + if got := fn(tt.input); got != tt.want { + assert.Equal(t, got, tt.want, "fieldName(%s) = %s, want %s", tt.input, got, tt.want) + } + }) + } +} diff --git a/test/integration/service.pb.go b/test/integration/service.pb.go index 4439fec..dfebf7c 100644 --- a/test/integration/service.pb.go +++ b/test/integration/service.pb.go @@ -1287,6 +1287,109 @@ func (x *OptionalFieldsSubMsg) GetOptStr() string { return "" } +type Names struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + FooBar string `protobuf:"bytes,1,opt,name=foo_bar,json=fooBar,proto3" json:"foo_bar,omitempty"` + Bazbam string `protobuf:"bytes,2,opt,name=bazbam,proto3" json:"bazbam,omitempty"` + Binbom3 string `protobuf:"bytes,3,opt,name=binbom3,proto3" json:"binbom3,omitempty"` + Tin3Tam string `protobuf:"bytes,4,opt,name=tin3tam,proto3" json:"tin3tam,omitempty"` + Ting3Tang string `protobuf:"bytes,5,opt,name=ting3_tang,json=ting3Tang,proto3" json:"ting3_tang,omitempty"` + King_3Kong string `protobuf:"bytes,6,opt,name=king_3kong,json=king3kong,proto3" json:"king_3kong,omitempty"` + Frim_3Fram string `protobuf:"bytes,7,opt,name=frim_3_fram,json=frim3Fram,proto3" json:"frim_3_fram,omitempty"` + K8SField string `protobuf:"bytes,8,opt,name=k8s_field,json=k8sField,proto3" json:"k8s_field,omitempty"` +} + +func (x *Names) Reset() { + *x = Names{} + if protoimpl.UnsafeEnabled { + mi := &file_service_proto_msgTypes[22] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Names) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Names) ProtoMessage() {} + +func (x *Names) ProtoReflect() protoreflect.Message { + mi := &file_service_proto_msgTypes[22] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Names.ProtoReflect.Descriptor instead. +func (*Names) Descriptor() ([]byte, []int) { + return file_service_proto_rawDescGZIP(), []int{22} +} + +func (x *Names) GetFooBar() string { + if x != nil { + return x.FooBar + } + return "" +} + +func (x *Names) GetBazbam() string { + if x != nil { + return x.Bazbam + } + return "" +} + +func (x *Names) GetBinbom3() string { + if x != nil { + return x.Binbom3 + } + return "" +} + +func (x *Names) GetTin3Tam() string { + if x != nil { + return x.Tin3Tam + } + return "" +} + +func (x *Names) GetTing3Tang() string { + if x != nil { + return x.Ting3Tang + } + return "" +} + +func (x *Names) GetKing_3Kong() string { + if x != nil { + return x.King_3Kong + } + return "" +} + +func (x *Names) GetFrim_3Fram() string { + if x != nil { + return x.Frim_3Fram + } + return "" +} + +func (x *Names) GetK8SField() string { + if x != nil { + return x.K8SField + } + return "" +} + var File_service_proto protoreflect.FileDescriptor var file_service_proto_rawDesc = []byte{ @@ -1441,82 +1544,97 @@ var file_service_proto_rawDesc = []byte{ 0x12, 0x10, 0x0a, 0x03, 0x73, 0x74, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, 0x74, 0x72, 0x12, 0x1c, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x06, 0x6f, 0x70, 0x74, 0x53, 0x74, 0x72, 0x88, 0x01, 0x01, - 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x6f, 0x70, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x32, 0x9c, 0x09, 0x0a, - 0x0e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, - 0x34, 0x0a, 0x09, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x2e, 0x6d, - 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x13, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x48, 0x0a, 0x13, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, - 0x6e, 0x67, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x16, 0x2e, 0x6d, - 0x61, 0x69, 0x6e, 0x2e, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x53, 0x74, 0x72, 0x65, - 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x30, 0x01, 0x12, - 0x3b, 0x0a, 0x10, 0x46, 0x61, 0x69, 0x6c, 0x69, 0x6e, 0x67, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x6d, - 0x65, 0x6e, 0x74, 0x12, 0x12, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, - 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x37, 0x0a, 0x0a, - 0x45, 0x63, 0x68, 0x6f, 0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x12, 0x13, 0x2e, 0x6d, 0x61, 0x69, - 0x6e, 0x2e, 0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, - 0x14, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x56, 0x0a, 0x07, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, - 0x12, 0x14, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x47, 0x65, 0x74, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, - 0x74, 0x70, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1e, 0x82, - 0xd3, 0xe4, 0x93, 0x02, 0x18, 0x12, 0x16, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x7b, 0x6e, 0x75, 0x6d, - 0x5f, 0x74, 0x6f, 0x5f, 0x69, 0x6e, 0x63, 0x72, 0x65, 0x61, 0x73, 0x65, 0x7d, 0x12, 0x63, 0x0a, - 0x1a, 0x48, 0x54, 0x54, 0x50, 0x50, 0x6f, 0x73, 0x74, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x65, 0x73, - 0x74, 0x65, 0x64, 0x42, 0x6f, 0x64, 0x79, 0x50, 0x61, 0x74, 0x68, 0x12, 0x15, 0x2e, 0x6d, 0x61, - 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, - 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x16, 0x82, 0xd3, 0xe4, 0x93, - 0x02, 0x10, 0x3a, 0x03, 0x72, 0x65, 0x71, 0x22, 0x09, 0x2f, 0x70, 0x6f, 0x73, 0x74, 0x2f, 0x7b, - 0x61, 0x7d, 0x12, 0x63, 0x0a, 0x18, 0x48, 0x54, 0x54, 0x50, 0x50, 0x6f, 0x73, 0x74, 0x57, 0x69, - 0x74, 0x68, 0x53, 0x74, 0x61, 0x72, 0x42, 0x6f, 0x64, 0x79, 0x50, 0x61, 0x74, 0x68, 0x12, 0x15, - 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, - 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x18, 0x82, - 0xd3, 0xe4, 0x93, 0x02, 0x12, 0x3a, 0x01, 0x2a, 0x22, 0x0d, 0x2f, 0x70, 0x6f, 0x73, 0x74, 0x2f, - 0x7b, 0x61, 0x7d, 0x2f, 0x7b, 0x63, 0x7d, 0x12, 0x4f, 0x0a, 0x09, 0x48, 0x54, 0x54, 0x50, 0x50, - 0x61, 0x74, 0x63, 0x68, 0x12, 0x16, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, - 0x50, 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6d, - 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, 0x3a, 0x01, 0x2a, - 0x32, 0x06, 0x2f, 0x70, 0x61, 0x74, 0x63, 0x68, 0x12, 0x52, 0x0a, 0x0a, 0x48, 0x54, 0x54, 0x50, - 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, - 0x74, 0x70, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, - 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, - 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x13, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0d, 0x2a, - 0x0b, 0x2f, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x2f, 0x7b, 0x61, 0x7d, 0x12, 0x36, 0x0a, 0x0f, - 0x45, 0x78, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, - 0x10, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x11, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x87, 0x01, 0x0a, 0x1a, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, - 0x57, 0x69, 0x74, 0x68, 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, - 0x61, 0x6d, 0x73, 0x12, 0x27, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x47, - 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, - 0x61, 0x72, 0x61, 0x6d, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x28, 0x2e, 0x6d, - 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x55, - 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x16, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x10, 0x12, 0x0e, - 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x71, 0x75, 0x65, 0x72, 0x79, 0x2f, 0x7b, 0x61, 0x7d, 0x12, 0x9f, - 0x01, 0x0a, 0x23, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x5a, 0x65, - 0x72, 0x6f, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, - 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x30, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x54, - 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x5a, 0x65, 0x72, 0x6f, 0x56, 0x61, 0x6c, - 0x75, 0x65, 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, - 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x31, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, - 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x5a, 0x65, 0x72, 0x6f, 0x56, - 0x61, 0x6c, 0x75, 0x65, 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, - 0x61, 0x6d, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x13, 0x82, 0xd3, 0xe4, - 0x93, 0x02, 0x0d, 0x12, 0x0b, 0x2f, 0x70, 0x61, 0x74, 0x68, 0x2f, 0x71, 0x75, 0x65, 0x72, 0x79, - 0x12, 0x69, 0x0a, 0x19, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x4f, - 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x1b, 0x2e, - 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x46, 0x69, 0x65, - 0x6c, 0x64, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x6d, 0x61, 0x69, - 0x6e, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, - 0x12, 0x09, 0x2f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x42, 0x09, 0x5a, 0x07, 0x2e, - 0x2f, 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x6f, 0x70, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x22, 0xe7, 0x01, 0x0a, + 0x05, 0x4e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x6f, 0x6f, 0x5f, 0x62, 0x61, + 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x66, 0x6f, 0x6f, 0x42, 0x61, 0x72, 0x12, + 0x16, 0x0a, 0x06, 0x62, 0x61, 0x7a, 0x62, 0x61, 0x6d, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x06, 0x62, 0x61, 0x7a, 0x62, 0x61, 0x6d, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x69, 0x6e, 0x62, 0x6f, + 0x6d, 0x33, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x69, 0x6e, 0x62, 0x6f, 0x6d, + 0x33, 0x12, 0x18, 0x0a, 0x07, 0x74, 0x69, 0x6e, 0x33, 0x74, 0x61, 0x6d, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x07, 0x74, 0x69, 0x6e, 0x33, 0x74, 0x61, 0x6d, 0x12, 0x1d, 0x0a, 0x0a, 0x74, + 0x69, 0x6e, 0x67, 0x33, 0x5f, 0x74, 0x61, 0x6e, 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x09, 0x74, 0x69, 0x6e, 0x67, 0x33, 0x54, 0x61, 0x6e, 0x67, 0x12, 0x1d, 0x0a, 0x0a, 0x6b, 0x69, + 0x6e, 0x67, 0x5f, 0x33, 0x6b, 0x6f, 0x6e, 0x67, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, + 0x6b, 0x69, 0x6e, 0x67, 0x33, 0x6b, 0x6f, 0x6e, 0x67, 0x12, 0x1e, 0x0a, 0x0b, 0x66, 0x72, 0x69, + 0x6d, 0x5f, 0x33, 0x5f, 0x66, 0x72, 0x61, 0x6d, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, + 0x66, 0x72, 0x69, 0x6d, 0x33, 0x46, 0x72, 0x61, 0x6d, 0x12, 0x1b, 0x0a, 0x09, 0x6b, 0x38, 0x73, + 0x5f, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6b, 0x38, + 0x73, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x32, 0x9c, 0x09, 0x0a, 0x0e, 0x43, 0x6f, 0x75, 0x6e, 0x74, + 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x34, 0x0a, 0x09, 0x49, 0x6e, 0x63, + 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, + 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x6d, 0x61, 0x69, + 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, + 0x48, 0x0a, 0x13, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x49, 0x6e, 0x63, 0x72, + 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x16, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x53, 0x74, + 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, + 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x30, 0x01, 0x12, 0x3b, 0x0a, 0x10, 0x46, 0x61, 0x69, + 0x6c, 0x69, 0x6e, 0x67, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x2e, + 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x13, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x37, 0x0a, 0x0a, 0x45, 0x63, 0x68, 0x6f, 0x42, 0x69, + 0x6e, 0x61, 0x72, 0x79, 0x12, 0x13, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x42, 0x69, 0x6e, 0x61, + 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x6d, 0x61, 0x69, 0x6e, + 0x2e, 0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, + 0x56, 0x0a, 0x07, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x12, 0x14, 0x2e, 0x6d, 0x61, 0x69, + 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x15, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x47, 0x65, 0x74, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1e, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x18, 0x12, + 0x16, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x7b, 0x6e, 0x75, 0x6d, 0x5f, 0x74, 0x6f, 0x5f, 0x69, 0x6e, + 0x63, 0x72, 0x65, 0x61, 0x73, 0x65, 0x7d, 0x12, 0x63, 0x0a, 0x1a, 0x48, 0x54, 0x54, 0x50, 0x50, + 0x6f, 0x73, 0x74, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x42, 0x6f, 0x64, + 0x79, 0x50, 0x61, 0x74, 0x68, 0x12, 0x15, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, + 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x6d, + 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x16, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x10, 0x3a, 0x03, 0x72, 0x65, + 0x71, 0x22, 0x09, 0x2f, 0x70, 0x6f, 0x73, 0x74, 0x2f, 0x7b, 0x61, 0x7d, 0x12, 0x63, 0x0a, 0x18, + 0x48, 0x54, 0x54, 0x50, 0x50, 0x6f, 0x73, 0x74, 0x57, 0x69, 0x74, 0x68, 0x53, 0x74, 0x61, 0x72, + 0x42, 0x6f, 0x64, 0x79, 0x50, 0x61, 0x74, 0x68, 0x12, 0x15, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, + 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x16, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x6f, 0x73, 0x74, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x18, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x12, 0x3a, + 0x01, 0x2a, 0x22, 0x0d, 0x2f, 0x70, 0x6f, 0x73, 0x74, 0x2f, 0x7b, 0x61, 0x7d, 0x2f, 0x7b, 0x63, + 0x7d, 0x12, 0x4f, 0x0a, 0x09, 0x48, 0x54, 0x54, 0x50, 0x50, 0x61, 0x74, 0x63, 0x68, 0x12, 0x16, + 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x50, 0x61, 0x74, 0x63, 0x68, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, + 0x74, 0x70, 0x50, 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, 0x3a, 0x01, 0x2a, 0x32, 0x06, 0x2f, 0x70, 0x61, 0x74, + 0x63, 0x68, 0x12, 0x52, 0x0a, 0x0a, 0x48, 0x54, 0x54, 0x50, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, + 0x12, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x74, 0x74, 0x70, 0x44, 0x65, 0x6c, 0x65, + 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, + 0x79, 0x22, 0x13, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0d, 0x2a, 0x0b, 0x2f, 0x64, 0x65, 0x6c, 0x65, + 0x74, 0x65, 0x2f, 0x7b, 0x61, 0x7d, 0x12, 0x36, 0x0a, 0x0f, 0x45, 0x78, 0x74, 0x65, 0x72, 0x6e, + 0x61, 0x6c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x10, 0x2e, 0x45, 0x78, 0x74, 0x65, + 0x72, 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x45, 0x78, + 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x87, + 0x01, 0x0a, 0x1a, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x55, 0x52, + 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x27, 0x2e, + 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, + 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x28, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x54, + 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, + 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x22, 0x16, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x10, 0x12, 0x0e, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x71, + 0x75, 0x65, 0x72, 0x79, 0x2f, 0x7b, 0x61, 0x7d, 0x12, 0x9f, 0x01, 0x0a, 0x23, 0x48, 0x54, 0x54, + 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x5a, 0x65, 0x72, 0x6f, 0x56, 0x61, 0x6c, 0x75, + 0x65, 0x55, 0x52, 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, + 0x12, 0x30, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, + 0x69, 0x74, 0x68, 0x5a, 0x65, 0x72, 0x6f, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x55, 0x52, 0x4c, 0x53, + 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x31, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x47, 0x65, + 0x74, 0x57, 0x69, 0x74, 0x68, 0x5a, 0x65, 0x72, 0x6f, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x55, 0x52, + 0x4c, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x13, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0d, 0x12, 0x0b, 0x2f, + 0x70, 0x61, 0x74, 0x68, 0x2f, 0x71, 0x75, 0x65, 0x72, 0x79, 0x12, 0x69, 0x0a, 0x19, 0x48, 0x54, + 0x54, 0x50, 0x47, 0x65, 0x74, 0x57, 0x69, 0x74, 0x68, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, + 0x6c, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x1b, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x4f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x61, 0x6c, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, 0x12, 0x09, 0x2f, 0x6f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x3b, 0x6d, 0x61, 0x69, 0x6e, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -1531,7 +1649,7 @@ func file_service_proto_rawDescGZIP() []byte { return file_service_proto_rawDescData } -var file_service_proto_msgTypes = make([]protoimpl.MessageInfo, 22) +var file_service_proto_msgTypes = make([]protoimpl.MessageInfo, 23) var file_service_proto_goTypes = []interface{}{ (*UnaryRequest)(nil), // 0: main.UnaryRequest (*UnaryResponse)(nil), // 1: main.UnaryResponse @@ -1555,15 +1673,16 @@ var file_service_proto_goTypes = []interface{}{ (*OptionalFieldsRequest)(nil), // 19: main.OptionalFieldsRequest (*OptionalFieldsResponse)(nil), // 20: main.OptionalFieldsResponse (*OptionalFieldsSubMsg)(nil), // 21: main.OptionalFieldsSubMsg - (*ExternalMessage)(nil), // 22: ExternalMessage - (*ExternalRequest)(nil), // 23: ExternalRequest - (*emptypb.Empty)(nil), // 24: google.protobuf.Empty - (*ExternalResponse)(nil), // 25: ExternalResponse + (*Names)(nil), // 22: main.Names + (*ExternalMessage)(nil), // 23: ExternalMessage + (*ExternalRequest)(nil), // 24: ExternalRequest + (*emptypb.Empty)(nil), // 25: google.protobuf.Empty + (*ExternalResponse)(nil), // 26: ExternalResponse } var file_service_proto_depIdxs = []int32{ 9, // 0: main.HttpPostRequest.req:type_name -> main.PostRequest 9, // 1: main.HTTPGetWithURLSearchParamsRequest.b:type_name -> main.PostRequest - 22, // 2: main.HTTPGetWithURLSearchParamsRequest.d:type_name -> ExternalMessage + 23, // 2: main.HTTPGetWithURLSearchParamsRequest.d:type_name -> ExternalMessage 16, // 3: main.HTTPGetWithZeroValueURLSearchParamsRequest.c:type_name -> main.ZeroValueMsg 16, // 4: main.HTTPGetWithZeroValueURLSearchParamsResponse.zero_value_msg:type_name -> main.ZeroValueMsg 21, // 5: main.OptionalFieldsResponse.empty_msg:type_name -> main.OptionalFieldsSubMsg @@ -1581,7 +1700,7 @@ var file_service_proto_depIdxs = []int32{ 8, // 17: main.CounterService.HTTPPostWithStarBodyPath:input_type -> main.HttpPostRequest 11, // 18: main.CounterService.HTTPPatch:input_type -> main.HttpPatchRequest 13, // 19: main.CounterService.HTTPDelete:input_type -> main.HttpDeleteRequest - 23, // 20: main.CounterService.ExternalMessage:input_type -> ExternalRequest + 24, // 20: main.CounterService.ExternalMessage:input_type -> ExternalRequest 14, // 21: main.CounterService.HTTPGetWithURLSearchParams:input_type -> main.HTTPGetWithURLSearchParamsRequest 17, // 22: main.CounterService.HTTPGetWithZeroValueURLSearchParams:input_type -> main.HTTPGetWithZeroValueURLSearchParamsRequest 19, // 23: main.CounterService.HTTPGetWithOptionalFields:input_type -> main.OptionalFieldsRequest @@ -1593,8 +1712,8 @@ var file_service_proto_depIdxs = []int32{ 10, // 29: main.CounterService.HTTPPostWithNestedBodyPath:output_type -> main.HttpPostResponse 10, // 30: main.CounterService.HTTPPostWithStarBodyPath:output_type -> main.HttpPostResponse 12, // 31: main.CounterService.HTTPPatch:output_type -> main.HttpPatchResponse - 24, // 32: main.CounterService.HTTPDelete:output_type -> google.protobuf.Empty - 25, // 33: main.CounterService.ExternalMessage:output_type -> ExternalResponse + 25, // 32: main.CounterService.HTTPDelete:output_type -> google.protobuf.Empty + 26, // 33: main.CounterService.ExternalMessage:output_type -> ExternalResponse 15, // 34: main.CounterService.HTTPGetWithURLSearchParams:output_type -> main.HTTPGetWithURLSearchParamsResponse 18, // 35: main.CounterService.HTTPGetWithZeroValueURLSearchParams:output_type -> main.HTTPGetWithZeroValueURLSearchParamsResponse 20, // 36: main.CounterService.HTTPGetWithOptionalFields:output_type -> main.OptionalFieldsResponse @@ -1876,6 +1995,18 @@ func file_service_proto_init() { return nil } } + file_service_proto_msgTypes[22].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Names); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } file_service_proto_msgTypes[20].OneofWrappers = []interface{}{} file_service_proto_msgTypes[21].OneofWrappers = []interface{}{} @@ -1885,7 +2016,7 @@ func file_service_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_service_proto_rawDesc, NumEnums: 0, - NumMessages: 22, + NumMessages: 23, NumExtensions: 0, NumServices: 1, }, diff --git a/test/integration/service.proto b/test/integration/service.proto index 5c613ea..c5b21e3 100644 --- a/test/integration/service.proto +++ b/test/integration/service.proto @@ -124,6 +124,17 @@ message OptionalFieldsSubMsg { optional string opt_str = 2; } +message Names { + string foo_bar = 1; + string bazbam = 2; + string binbom3 = 3; + string tin3tam = 4; + string ting3_tang = 5; + string king_3kong = 6; + string frim_3_fram = 7; + string k8s_field = 8; +} + service CounterService { rpc Increment(UnaryRequest) returns (UnaryResponse); rpc StreamingIncrements(StreamingRequest) returns (stream StreamingResponse); diff --git a/test/names_test.go b/test/names_test.go new file mode 100644 index 0000000..500b5b9 --- /dev/null +++ b/test/names_test.go @@ -0,0 +1,26 @@ +package test + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFieldsWithNumbers(t *testing.T) { + // Verifies that the proto generates the field name in the right format. + + createTestFile("fieldsWithNumbers.ts", ` +import {FieldsWithNumbers} from "./names.pb" +export const newFieldsWithNumbers = (f: FieldsWithNumbers) => f.k8sField; +export const result = newFieldsWithNumbers({k8sField: 'test'}); + `) + + defer removeTestFile("fieldsWithNumbers.ts") + + result := runTsc() + assert.Nil(t, result.err) + assert.Equal(t, 0, result.exitCode) + + assert.Empty(t, result.stderr) + assert.Empty(t, result.stdout) +} diff --git a/test/oneof_test.go b/test/oneof_test.go index 2aa9951..2785dab 100644 --- a/test/oneof_test.go +++ b/test/oneof_test.go @@ -1,28 +1,13 @@ package test import ( - "os" - "os/exec" - "path/filepath" - "strings" "testing" - "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) -var projectRoot = "" - -func init() { - wd, _ := os.Getwd() - for !strings.HasSuffix(wd, "protoc-gen-grpc-gateway-ts") { - wd = filepath.Dir(wd) - } - projectRoot = wd -} - func TestValidOneOfUseCase(t *testing.T) { - f, err := createFileWithContent("valid.ts", ` + createTestFile("valid.ts", ` import {LogEntryLevel, LogService} from "./log.pb"; import {DataSource} from "./datasource/datasource.pb" import {Environment} from "./environment.pb" @@ -64,27 +49,19 @@ import {Environment} from "./environment.pb" }) })() `) - assert.Nil(t, err) - defer f.Close() - cmd := getTSCCommand() - err = cmd.Run() - assert.Nil(t, err) - assert.Equal(t, 0, cmd.ProcessState.ExitCode()) - err = removeTestFile("valid.ts") - assert.Nil(t, err) -} + defer removeTestFile("valid.ts") + + result := runTsc() + assert.Nil(t, result.err) + assert.Equal(t, 0, result.exitCode) -func getTSCCommand() *exec.Cmd { - cmd := exec.Command("npx", "tsc", "--project", ".", "--noEmit") - cmd.Dir = projectRoot + "/test/testdata/" - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - return cmd + assert.Empty(t, result.stderr) + assert.Empty(t, result.stdout) } func TestInvalidOneOfUseCase(t *testing.T) { - f, err := createFileWithContent("invalid.ts", ` + createTestFile("invalid.ts", ` import {LogService} from "./log.pb"; import {DataSource} from "./datasource/datasource.pb" @@ -92,36 +69,18 @@ import {DataSource} from "./datasource/datasource.pb" const cloudSourceResult = await LogService.FetchLog({ source: DataSource.Cloud, service: "cloudService", - application: "cloudApplication" + application: "cloudApplication", }) })() `) - assert.Nil(t, err) - defer f.Close() - cmd := getTSCCommand() - err = cmd.Run() - assert.NotNil(t, err) - assert.NotEqual(t, 0, cmd.ProcessState.ExitCode()) + defer removeTestFile("invalid.ts") - err = removeTestFile("invalid.ts") - assert.Nil(t, err) -} - -func createFileWithContent(fname, content string) (*os.File, error) { - f, err := os.Create(projectRoot + "/test/testdata/" + fname) - if err != nil { - return nil, errors.Wrapf(err, "error creating file") - } - defer f.Close() - _, err = f.WriteString(content) - if err != nil { - return nil, errors.Wrapf(err, "error writing content into %s", fname) - } - - return f, nil -} + result := runTsc() + assert.NotNil(t, result.err) + assert.NotEqual(t, 0, result.exitCode) -func removeTestFile(fname string) error { - return os.Remove(projectRoot + "/test/testdata/" + fname) + assert.Contains(t, result.stderr, + "Argument of type '{ source: DataSource.Cloud; service: string; application: "+ + "string; }' is not assignable to parameter of type 'FetchLogRequest'.") } diff --git a/test/test.go b/test/test.go new file mode 100644 index 0000000..48bc914 --- /dev/null +++ b/test/test.go @@ -0,0 +1,62 @@ +package test + +import ( + "bytes" + "os" + "os/exec" + "path/filepath" + "strings" +) + +var projectRoot = "" + +func init() { + wd, _ := os.Getwd() + for !strings.HasSuffix(wd, "protoc-gen-grpc-gateway-ts") { + wd = filepath.Dir(wd) + } + projectRoot = wd +} + +func runTsc() cmdResult { + cmd := exec.Command("npx", "tsc", "--project", ".", "--noEmit") + cmd.Dir = projectRoot + "/test/testdata/" + + cmdOutput := new(bytes.Buffer) + cmdError := new(bytes.Buffer) + cmd.Stderr = cmdOutput + cmd.Stdout = cmdError + + err := cmd.Run() + + return cmdResult{ + stdout: cmdOutput.String(), + stderr: cmdError.String(), + err: err, + exitCode: cmd.ProcessState.ExitCode(), + } +} + +type cmdResult struct { + stdout string + stderr string + err error + exitCode int +} + +func createTestFile(fname, content string) { + f, err := os.Create(projectRoot + "/test/testdata/" + fname) + if err != nil { + panic(err) + } + defer f.Close() + if _, err = f.WriteString(content); err != nil { + panic(err) + } +} + +func removeTestFile(fname string) { + if err := os.Remove(projectRoot + "/test/testdata/" + fname); err != nil { + panic(err) + } +} diff --git a/test/testdata/names.proto b/test/testdata/names.proto new file mode 100644 index 0000000..46edb71 --- /dev/null +++ b/test/testdata/names.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package com.squareup.cash.gap; + +// See https://github.com/dpup/protoc-gen-grpc-gateway-ts/issues/1 +message FieldsWithNumbers { + string k8s_field = 1; +}