Skip to content

Commit

Permalink
make renderURL function use fieldName function to cope with UseProtoN…
Browse files Browse the repository at this point in the history
…ames params
  • Loading branch information
lyonlai committed Apr 12, 2021
1 parent 440e2c4 commit cca0b90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
47 changes: 27 additions & 20 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,36 +207,43 @@ func GetTemplate(r *registry.Registry) *template.Template {
"tsType": func(fieldType data.Type) string {
return tsType(r, fieldType)
},
"renderURL": renderURL,
"renderURL": renderURL(r),
"buildInitReq": buildInitReq,
"fieldName": func(name string) string {
if r.UseProtoNames {
return name
}

return strcase.ToLowerCamel(name)
},
"fieldName": fieldName(r),
})

t = template.Must(t.Parse(tmpl))
return t
}

func renderURL(method data.Method) string {
url := method.URL
reg := regexp.MustCompile("{([^}]+)}")
matches := reg.FindAllStringSubmatch(url, -1)
if len(matches) > 0 {
log.Debugf("url matches %v", matches)
for _, m := range matches {
expToReplace := m[0]
fieldName := m[1]
part := fmt.Sprintf(`${req["%s"]}`, fieldName)
url = strings.ReplaceAll(url, expToReplace, part)
func fieldName(r *registry.Registry) func(name string) string {
return func(name string) string {
if r.UseProtoNames {
return name
}

return strcase.ToLowerCamel(name)
}
}

return url
func renderURL(r *registry.Registry) func(method data.Method) string {
fieldNameFn := fieldName(r)
return func(method data.Method) string {
url := method.URL
reg := regexp.MustCompile("{([^}]+)}")
matches := reg.FindAllStringSubmatch(url, -1)
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)
url = strings.ReplaceAll(url, expToReplace, part)
}
}

return url
}
}

func buildInitReq(method data.Method) string {
Expand Down
11 changes: 11 additions & 0 deletions integration_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Integration test

The integration test first runs `./scripts/gen-protos.sh` again to generate Typescript file for the proto `service.proto`.
Then it starts `main.go` server that loads up the protos and run tests via `Karma` to verify if the generated client works properly.
The JS integration test file is `integration_test.ts`.

Changes on the server side needs to run `./scripts/gen-server-proto.sh` to update the protos and the implementation is in `service.go`.

Changes on the test client side is in `integration_test.ts`.

CI test script starts with `test-ci.sh` will make sure the client typescript file to be regenerated before running the test.

0 comments on commit cca0b90

Please sign in to comment.