Skip to content

Commit

Permalink
Fix potential syntax err (#37)
Browse files Browse the repository at this point in the history
* Fix potential syntax err

Some generated code may lead to
proto/server.gen.go:568:6: no new variables on left side of :=

* Basic example: Add a method with some request args but no return args

* Fix the syntax error
  • Loading branch information
VojtechVitek authored Aug 25, 2023
1 parent 07c019e commit c9e212d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
54 changes: 48 additions & 6 deletions _examples/golang-basics/example.gen.go

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

1 change: 1 addition & 0 deletions _examples/golang-basics/example.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ service ExampleService
- Version() => (version: Version)
- GetUser(header: map<string,string>, userID: uint64) => (user: User)
- FindUser(s: SearchFilter) => (name: string, user: User)
- LogEvent(event: string)
4 changes: 4 additions & 0 deletions _examples/golang-basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ func (rpc *ExampleServiceRPC) FindUser(ctx context.Context, s *SearchFilter) (st
Username: name,
}, nil
}

func (rpc *ExampleServiceRPC) LogEvent(ctx context.Context, event string) error {
return nil
}
6 changes: 3 additions & 3 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
{{range .Methods }}
func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context.Context, w http.ResponseWriter, r *http.Request) {
{{ if .Inputs|len}}
{{ if gt (len .Inputs) 0 -}}
reqBody, err := io.ReadAll(r.Body)
if err != nil {
RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err)))
Expand All @@ -96,13 +96,13 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context
ctx = context.WithValue(ctx, MethodNameCtxKey, "{{.Name}}")

// Call service method implementation.
{{range $i, $output := .Outputs}}ret{{$i}}, {{end}}err := s.{{$name}}.{{.Name}}(ctx{{range $i, $_ := .Inputs}}, reqPayload.Arg{{$i}}{{end}})
{{range $i, $output := .Outputs}}ret{{$i}}, {{end}}err {{if or (eq (len .Inputs) 0) (gt (len .Outputs) 0)}}:{{end}}= s.{{$name}}.{{.Name}}(ctx{{range $i, $_ := .Inputs}}, reqPayload.Arg{{$i}}{{end}})
if err != nil {
RespondWithError(w, err)
return
}

{{- if .Outputs | len}}
{{- if gt (len .Outputs) 0}}

respPayload := struct {
{{- range $i, $output := .Outputs}}
Expand Down

0 comments on commit c9e212d

Please sign in to comment.