From 5a6312b7f91c2b0aecdcb147a7b8b6685a71a511 Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Tue, 2 Aug 2022 14:56:27 -0500 Subject: [PATCH] Refactor getTagName function (#16) * move request_test so coverage is accurate * go mod tidy * update Makefile test definition * getTagNames refactor * getTagNames unit tests * rename files * code defensively for empty * add CHANGELOG entry --- CHANGELOG.md | 1 + Makefile | 4 +- go.mod | 1 - go.sum | 18 -- runtime/getTagName.go | 52 +++++ runtime/getTagName_test.go | 288 ++++++++++++++++++++++++ runtime/{tests => }/request_test.go | 13 +- runtime/response.go | 19 -- runtime/{tests => testpb}/Makefile | 0 runtime/{tests => testpb}/request.pb.go | 6 +- runtime/{tests => testpb}/request.proto | 2 +- 11 files changed, 354 insertions(+), 50 deletions(-) create mode 100644 runtime/getTagName.go create mode 100644 runtime/getTagName_test.go rename runtime/{tests => }/request_test.go (90%) rename runtime/{tests => testpb}/Makefile (100%) rename runtime/{tests => testpb}/request.pb.go (98%) rename runtime/{tests => testpb}/request.proto (92%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7733efc..d5c9c17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Resolve camelCase enum values ([#13](https://github.com/alehechka/grpc-graphql-gateway/pull/13)) - Remove field_camel and add field_proto CLI arg ([#14](https://github.com/alehechka/grpc-graphql-gateway/pull/14)) +- Refactor getTagName function ([#16](https://github.com/alehechka/grpc-graphql-gateway/pull/16)) ## [v0.2.4](https://github.com/alehechka/grpc-graphql-gateway/releases/tag/v0.2.4) diff --git a/Makefile b/Makefile index e700a85..5718196 100644 --- a/Makefile +++ b/Makefile @@ -25,8 +25,8 @@ lint: golangci-lint run test: - cd runtime/tests ; make build - go list ./... | xargs go test + cd runtime/testpb ; make build + go test ./... --cover build: test plugin diff --git a/go.mod b/go.mod index 09809ee..64287ec 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.18 require ( github.com/friendsofgo/graphiql v0.2.2 - github.com/gogo/protobuf v1.3.2 github.com/golang/protobuf v1.5.2 github.com/graphql-go/graphql v0.8.0 github.com/iancoleman/strcase v0.2.0 diff --git a/go.sum b/go.sum index c4ff577..8498550 100644 --- a/go.sum +++ b/go.sum @@ -23,8 +23,6 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/friendsofgo/graphiql v0.2.2 h1:ccnuxpjgIkB+Lr9YB2ZouiZm7wvciSfqwpa9ugWzmn0= github.com/friendsofgo/graphiql v0.2.2/go.mod h1:8Y2kZ36AoTGWs78+VRpvATyt3LJBx0SZXmay80ZTRWo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= -github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -55,8 +53,6 @@ github.com/graphql-go/graphql v0.8.0/go.mod h1:nKiHzRM0qopJEwCITUuIsxk9PlVlwIiiI github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= -github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -70,26 +66,19 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= @@ -99,8 +88,6 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -116,11 +103,6 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/runtime/getTagName.go b/runtime/getTagName.go new file mode 100644 index 0000000..a21290d --- /dev/null +++ b/runtime/getTagName.go @@ -0,0 +1,52 @@ +package runtime + +import ( + "reflect" + "strings" +) + +func getTagName(tag reflect.StructTag) (name string) { + + name, protoOK := getProtoTagName(tag) + if !protoOK { + name, _ = getJsonTagName(tag) + } + + return +} + +func getProtoTagName(tag reflect.StructTag) (name string, ok bool) { + protoTag, protoOK := tag.Lookup("protobuf") + if !protoOK { + return + } + + namePrefix := "name=" + jsonPrefix := "json=" + + options := strings.Split(protoTag, ",") + for _, option := range options { + if strings.HasPrefix(option, jsonPrefix) { + name = option[len(jsonPrefix):] + if ok = len(name) > 0; ok { + return // found the first valid `jsonPrefix` so we are able to return + } + } else if !ok && strings.HasPrefix(option, namePrefix) { + name = option[len(namePrefix):] + ok = len(name) > 0 + } + } + + return +} + +func getJsonTagName(tag reflect.StructTag) (name string, ok bool) { + jsonTag, jsonOK := tag.Lookup("json") + if !jsonOK { + return + } + + name = strings.Split(jsonTag, ",")[0] + ok = len(name) > 0 + return +} diff --git a/runtime/getTagName_test.go b/runtime/getTagName_test.go new file mode 100644 index 0000000..5c5df73 --- /dev/null +++ b/runtime/getTagName_test.go @@ -0,0 +1,288 @@ +package runtime + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_getJsonTagName_NoCommas(t *testing.T) { + data := struct { + item string `json:"item"` + }{item: "hello"} + + name, exists := getJsonTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getJsonTagName_OmitEmpty(t *testing.T) { + data := struct { + item string `json:"item,omitempty"` + }{item: "hello"} + + name, exists := getJsonTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getJsonTagName_StarterComma(t *testing.T) { + data := struct { + item string `json:",omitempty"` + }{item: "hello"} + + name, exists := getJsonTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getJsonTagName_EndComma(t *testing.T) { + data := struct { + item string `json:"item,"` + }{item: "hello"} + + name, exists := getJsonTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getJsonTagName_MultipleCommas(t *testing.T) { + data := struct { + item string `json:"item,omitempty,required"` + }{item: "hello"} + + name, exists := getJsonTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getJsonTagName_Empty(t *testing.T) { + data := struct { + item string `json:""` + }{item: "hello"} + + name, exists := getJsonTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getJsonTagName_NoJsonTag(t *testing.T) { + data := struct { + item string `graphql:"item,omitempty"` + }{item: "hello"} + + name, exists := getJsonTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getProtoTagName_NoJson(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=item,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getProtoTagName_NameAndJson(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=item,json=itemName,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "itemName", name) + assert.True(t, exists) +} + +func Test_getProtoTagName_NoName(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,json=item,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getProtoTagName_NoNameNoJson(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getProtoTagName_NoProtobufTag(t *testing.T) { + data := struct { + item string `graphql:"item,omitempty"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getProtoTagName_EmptyTag(t *testing.T) { + data := struct { + item string `protobuf:""` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getProtoTagName_OnlyCommas(t *testing.T) { + data := struct { + item string `protobuf:","` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getProtoTagName_FindsFirstName(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=item,name=itemName,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getProtoTagName_FindsFirstJson(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,json=item,json=itemName,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getProtoTagName_EmptyName(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getProtoTagName_EmptyJson(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,json=,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "", name) + assert.False(t, exists) +} + +func Test_getProtoTagName_SecondNameValid(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=,name=item,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getProtoTagName_SecondJsonValid(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,json=,json=item,proto3"` + }{item: "hello"} + + name, exists := getProtoTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) + assert.True(t, exists) +} + +func Test_getTagName_ProtoName(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=item,proto3"` + }{item: "hello"} + + name := getTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) +} + +func Test_getTagName_ProtoJson(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=item,json=itemName,proto3"` + }{item: "hello"} + + name := getTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "itemName", name) +} + +func Test_getTagName_NoProto_Json(t *testing.T) { + data := struct { + item string `json:"item"` + }{item: "hello"} + + name := getTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) +} + +func Test_getTagName_InvalidProto_Json(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,proto3" json:"item"` + }{item: "hello"} + + name := getTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) +} + +func Test_getTagName_InvalidProtoEmptyName_Json(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,name=,proto3" json:"item"` + }{item: "hello"} + + name := getTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) +} + +func Test_getTagName_InvalidProtoEmptyJsson_Json(t *testing.T) { + data := struct { + item string `protobuf:"bytes,2,opt,json=,proto3" json:"item"` + }{item: "hello"} + + name := getTagName(reflect.ValueOf(data).Type().Field(0).Tag) + + assert.Equal(t, "item", name) +} diff --git a/runtime/tests/request_test.go b/runtime/request_test.go similarity index 90% rename from runtime/tests/request_test.go rename to runtime/request_test.go index 37dba87..34a6b03 100644 --- a/runtime/tests/request_test.go +++ b/runtime/request_test.go @@ -1,13 +1,14 @@ -package tests +package runtime_test import ( "testing" "github.com/alehechka/grpc-graphql-gateway/runtime" + "github.com/alehechka/grpc-graphql-gateway/runtime/testpb" "github.com/stretchr/testify/assert" ) -func assertStruct(t *testing.T, s *A) { +func assertStruct(t *testing.T, s *testpb.A) { assert.NotNil(t, s) assert.Equal(t, "string", s.StringValue) assert.Equal(t, int64(1), s.IntValue) @@ -41,7 +42,7 @@ func TestMarshalRequest(t *testing.T) { "int_value": 1, "string_slice": []string{"A", "B", "C"}, }, - "struct_slice": []C{ + "struct_slice": []testpb.C{ { StringValue: "string01", IntValue: 2, @@ -52,7 +53,7 @@ func TestMarshalRequest(t *testing.T) { }, }, } - var v A + var v testpb.A err := runtime.MarshalRequest(data, &v) assert.NoError(t, err) assertStruct(t, &v) @@ -68,7 +69,7 @@ func TestMarshalRequestWithCamelCaseInput(t *testing.T) { "valueInt": 1, "sliceString": []string{"A", "B", "C"}, }, - "structSlice": []C{ + "structSlice": []testpb.C{ { StringValue: "string01", IntValue: 2, @@ -79,7 +80,7 @@ func TestMarshalRequestWithCamelCaseInput(t *testing.T) { }, }, } - var v A + var v testpb.A err := runtime.MarshalRequest(data, &v) assert.NoError(t, err) assertStruct(t, &v) diff --git a/runtime/response.go b/runtime/response.go index 416b082..cee12e4 100644 --- a/runtime/response.go +++ b/runtime/response.go @@ -2,7 +2,6 @@ package runtime import ( "reflect" - "strings" ) func derefValue(v reflect.Value) reflect.Value { @@ -78,24 +77,6 @@ func marshalStruct(v reflect.Value) map[string]interface{} { return ret } -func getTagName(tag reflect.StructTag) (name string) { - - protoTag := tag.Get("protobuf") - options := strings.Split(protoTag, ",") - - for _, option := range options { - if strings.HasPrefix(option, "name") { - name = option[5:] - } - if strings.HasPrefix(option, "json") { - name = option[5:] - break - } - } - - return -} - type mapValue struct { Key interface{} `json:"key"` Value interface{} `json:"value"` diff --git a/runtime/tests/Makefile b/runtime/testpb/Makefile similarity index 100% rename from runtime/tests/Makefile rename to runtime/testpb/Makefile diff --git a/runtime/tests/request.pb.go b/runtime/testpb/request.pb.go similarity index 98% rename from runtime/tests/request.pb.go rename to runtime/testpb/request.pb.go index e15adc7..d7398df 100644 --- a/runtime/tests/request.pb.go +++ b/runtime/testpb/request.pb.go @@ -4,7 +4,7 @@ // protoc v3.21.4 // source: request.proto -package tests +package testpb import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" @@ -243,8 +243,8 @@ var file_request_proto_rawDesc = []byte{ 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x69, 0x6e, 0x74, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x69, 0x6e, 0x74, - 0x56, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x74, 0x65, 0x73, 0x74, - 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x56, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x0b, 0x5a, 0x09, 0x2e, 0x2f, 0x3b, 0x74, 0x65, 0x73, 0x74, + 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/runtime/tests/request.proto b/runtime/testpb/request.proto similarity index 92% rename from runtime/tests/request.proto rename to runtime/testpb/request.proto index 8a27a04..ef04eae 100644 --- a/runtime/tests/request.proto +++ b/runtime/testpb/request.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -option go_package = "./;tests"; +option go_package = "./;testpb"; message A { string string_value = 1;