Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested fields, path segments with merged master and working ci #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.46
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version. Fails on 1.46
version: v1.51.2

# Runs a single command using the runners shell
- name: Get go dependencies
Expand Down
18 changes: 15 additions & 3 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,27 @@ func renderURL(r *registry.Registry) func(method data.Method) string {
fieldNameFn := fieldName(r)
return func(method data.Method) string {
methodURL := method.URL
reg := regexp.MustCompile("{([^}]+)}")
// capture fields like {abc} or {abc=def/ghi/*}.
// discard the pattern after the equal sign.
// relies on greedy capture within first part
// to avoid matching 2nd group when no equal sign present.
reg := regexp.MustCompile("{([^=}]+)=?([^}]+)?}")
matches := reg.FindAllStringSubmatch(methodURL, -1)
fieldsInPath := make([]string, 0, len(matches))
if len(matches) > 0 {
log.Debugf("url matches %v", matches)
for _, m := range matches {
expToReplace := m[0]
fieldName := fieldNameFn(m[1])
part := fmt.Sprintf(`${req["%s"]}`, fieldName)
// convert foo_bar.baz_qux to fieldName `fooBar.bazQux`, part `${req["fooBar"]["bazQux"]}`
subFields := strings.Split(m[1], ".")
var subFieldNames, partNames []string
for _, subField := range subFields {
subFieldName := fieldNameFn(subField)
subFieldNames = append(subFieldNames, subFieldName)
partNames = append(partNames, fmt.Sprintf(`["%s"]`, subFieldName))
}
fieldName := strings.Join(subFieldNames, ".")
part := fmt.Sprintf(`${req%s}`, strings.Join(partNames, "?."))
methodURL = strings.ReplaceAll(methodURL, expToReplace, part)
fieldsInPath = append(fieldsInPath, fmt.Sprintf(`"%s"`, fieldName))
}
Expand Down
21 changes: 18 additions & 3 deletions integration_tests/integration_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("test grpc-gateway-ts communication", () => {

it("failing unary request", async () => {
try {
await CounterService.FailingIncrement({ counter: 199 }, { pathPrefix: "http://localhost:8081" });
await CounterService.FailingIncrement({ counter: 199 }, { pathPrefix: "http://localhost:8081" });
expect.fail("expected call to throw");
} catch (e) {
expect(e).to.have.property("message", "this increment does not work")
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("test grpc-gateway-ts communication", () => {
const result = await CounterService.HTTPDelete({ a: 10 }, { pathPrefix: "http://localhost:8081" })
expect(result).to.be.empty
})

it('http get request with url search parameters', async () => {
const result = await CounterService.HTTPGetWithURLSearchParams({ a: 10, [getFieldName('post_req')]: { b: 0 }, c: [23, 25], [getFieldName('ext_msg')]: { d: 12 } }, { pathPrefix: "http://localhost:8081" })
expect(getField(result, 'url_search_params_result')).to.equal(70)
Expand All @@ -87,4 +87,19 @@ describe("test grpc-gateway-ts communication", () => {
const result = await CounterService.HTTPGetWithZeroValueURLSearchParams({ a: "A", b: "", [getFieldName('zero_value_msg')]: { c: 1, d: [1, 0, 2], e: false } }, { pathPrefix: "http://localhost:8081" })
expect(result).to.deep.equal({ a: "A", b: "hello", [getFieldName('zero_value_msg')]: { c: 2, d: [2, 1, 3], e: true } })
})
})

it('http get request with path segments', async () => {
const result = await CounterService.HTTPGetWithPathSegments({ a: "segmented/foo" }, { pathPrefix: "http://localhost:8081" })
expect(result.a).to.equal("segmented/foo/hello")
})

it('http post with field paths', async () => {
const result = await CounterService.HTTPPostWithFieldPath({ y: { x: 5, [getFieldName("nested_value")]: "foo" } }, { pathPrefix: "http://localhost:8081" })
expect(result).to.deep.equal({ xout: 5, yout: "hello/foo" })
})

it('http post with field paths and path segments', async () => {
const result = await CounterService.HTTPPostWithFieldPathAndSegments({ y: { x: 10, [getFieldName("nested_value")]: "segmented/foo" } }, { pathPrefix: "http://localhost:8081" })
expect(result).to.deep.equal({ xout: 10, yout: "hello/segmented/foo" })
})
})
9 changes: 2 additions & 7 deletions integration_tests/msg.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions integration_tests/scripts/gen-server-proto.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/bin/bash

# remove binaries to ensure that binaries present in tools.go are installed
rm -f $GOBIN/protoc-gen-go $GOBIN/protoc-gen-grpc-gateway $GOBIN/protoc-gen-swagger
rm -f $GOBIN/protoc-gen-go $GOBIN/protoc-gen-go-grpc $GOBIN/protoc-gen-grpc-gateway $GOBIN/protoc-gen-swagger

go install \
github.com/golang/protobuf/protoc-gen-go \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

protoc -I . -I ../.. --go_out ./ --go_opt plugins=grpc --go_opt paths=source_relative \
protoc -I . -I ../.. --go_out ./ --go-grpc_out ./ --go-grpc_opt paths=source_relative \
--grpc-gateway_out ./ --grpc-gateway_opt logtostderr=true \
--grpc-gateway_opt paths=source_relative \
--grpc-gateway_opt generate_unbound_methods=true \
service.proto msg.proto
service.proto msg.proto
20 changes: 20 additions & 0 deletions integration_tests/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,23 @@ func (r *RealCounterService) HTTPGetWithZeroValueURLSearchParams(ctx context.Con
},
}, nil
}

func (r *RealCounterService) HTTPGetWithPathSegments(ctx context.Context, in *HTTPGetWithPathSegmentsRequest) (*HTTPGetWithPathSegmentsResponse, error) {
return &HTTPGetWithPathSegmentsResponse{
A: in.GetA() + "/hello",
}, nil
}

func (r *RealCounterService) HTTPPostWithFieldPath(ctx context.Context, in *HTTPPostWithFieldPathRequest) (*HTTPPostWithFieldPathResponse, error) {
return &HTTPPostWithFieldPathResponse{
Xout: in.GetY().GetX(),
Yout: "hello/" + in.GetY().GetNestedValue(),
}, nil
}

func (r *RealCounterService) HTTPPostWithFieldPathAndSegments(ctx context.Context, in *HTTPPostWithFieldPathRequest) (*HTTPPostWithFieldPathResponse, error) {
return &HTTPPostWithFieldPathResponse{
Xout: in.GetY().GetX(),
Yout: "hello/" + in.GetY().GetNestedValue(),
}, nil
}
Loading