Skip to content

Commit

Permalink
Merge pull request #18 from donutloop/api/refactoring
Browse files Browse the repository at this point in the history
API Generator: Generates for serve%sContent simpler and cleaner code
  • Loading branch information
donutloop authored Mar 5, 2018
2 parents b6f568e + 61b2272 commit 5182803
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
23 changes: 11 additions & 12 deletions generator/proto/go/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,28 +804,27 @@ func (a *API) generateServerServeMethod(service *descriptor.ServiceDescriptorPro
serveMethod.Defer(types.NewUnsafeTypeReference("transport.Closebody"), []string{"req.Body", "s.logErrorFunc"})

serveMethod.DefNew("reqContent", types.NewUnsafeTypeReference(inputType))
serveMethod.DefCall([]string{"err"}, types.NewUnsafeTypeReference("decodeRequest"), []string{"ctx", "req", "reqContent"})
serveMethod.DefIfBegin("err", token.NEQ, "nil")
s, _ := serveMethod.SCallWithDefVar([]string{"err"}, types.NewUnsafeTypeReference("decodeRequest"), []string{"ctx", "req", "reqContent"})
serveMethod.DefIfWithOwnScopeBegin(s, "err", token.NEQ, "nil")
serveMethod.Caller(types.NewUnsafeTypeReference("s.logErrorFunc"), []string{`"%v"`, "err"})
serveMethod.Caller(types.NewUnsafeTypeReference("s.writeError"), []string{"ctx", "resp", "err"})
serveMethod.Return()
serveMethod.CloseIf()

serveMethod.DefNew("respContent", types.NewUnsafeTypeReference(outputType))
responseCallWrapper, _ := types.NewAnonymousGoFunc("responseCallWrapper", nil, nil)
responseDeferWrapper, _ := types.NewAnonymousGoFunc("responseDeferWrapper", nil, nil)
responseCallWrapper, _ := types.NewAnonymousGoFunc("endpointWrapper", nil, []types.TypeReference{types.NewUnsafeTypeReference(fmt.Sprintf("*%s", outputType)), types.NewUnsafeTypeReference("error")})
responseDeferWrapper, _ := types.NewAnonymousGoFunc("deferWrapper", nil, nil)

responseDeferWrapper.DefAssginCall([]string{"r"}, types.NewUnsafeTypeReference("recover"), nil)
responseDeferWrapper.DefIfBegin("r", token.NEQ, "nil")
s, _ = responseDeferWrapper.SCallWithDefVar([]string{"r"}, types.NewUnsafeTypeReference("recover"), nil)
responseDeferWrapper.DefIfWithOwnScopeBegin(s, "r", token.NEQ, "nil")
responseDeferWrapper.DefAssginCall([]string{"terr"}, types.NewUnsafeTypeReference("errors.InternalError"), []string{`"Internal service panic"`})
responseDeferWrapper.Caller(types.NewUnsafeTypeReference("s.writeError"), []string{"ctx", "resp", "terr"})
responseDeferWrapper.Caller(types.NewUnsafeTypeReference("panic"), []string{"r"})
responseDeferWrapper.CloseIf()
responseCallWrapper.AnonymousGoFunc(responseDeferWrapper)
responseCallWrapper.Defer(types.NewUnsafeTypeReference("responseDeferWrapper"), nil)
responseCallWrapper.DefCall([]string{"respContent", "err"}, types.NewUnsafeTypeReference(fmt.Sprintf("s.%s", methName)), []string{"ctx", "reqContent"})
responseCallWrapper.Defer(types.NewUnsafeTypeReference("deferWrapper"), nil)
responseCallWrapper.ReturnCaller(types.NewUnsafeTypeReference(fmt.Sprintf("s.%s", methName)), []string{"ctx", "reqContent"})
serveMethod.AnonymousGoFunc(responseCallWrapper)
serveMethod.Caller(types.NewUnsafeTypeReference("responseCallWrapper"), nil)
serveMethod.DefAssginCall([]string{"respContent", "err"}, types.NewUnsafeTypeReference("endpointWrapper"), nil)
serveMethod.DefIfBegin("err", token.NEQ, "nil")
serveMethod.Caller(types.NewUnsafeTypeReference("s.writeError"), []string{"ctx", "resp", "err"})
serveMethod.Return()
Expand All @@ -840,8 +839,8 @@ func (a *API) generateServerServeMethod(service *descriptor.ServiceDescriptorPro
serveMethod.CloseIf()

serveMethod.DefCall([]string{"ctx"}, types.NewUnsafeTypeReference("transport.CallResponsePrepared"), []string{"ctx", "s.hooks"})
serveMethod.DefCall([]string{"err"}, types.NewUnsafeTypeReference("encodeResponse"), []string{"ctx", "resp", "respContent"})
serveMethod.DefIfBegin("err", token.NEQ, "nil")
s, _ = serveMethod.SCallWithDefVar([]string{"err"}, types.NewUnsafeTypeReference("encodeResponse"), []string{"ctx", "resp", "respContent"})
serveMethod.DefIfWithOwnScopeBegin(s, "err", token.NEQ, "nil")
serveMethod.Caller(types.NewUnsafeTypeReference("s.logErrorFunc"), []string{`"%v"`, "err"})
serveMethod.Caller(types.NewUnsafeTypeReference("s.writeError"), []string{"ctx", "resp", "err"})
serveMethod.Return()
Expand Down
21 changes: 9 additions & 12 deletions integration_tests/api_hello_world/helloworld.proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,25 @@ func (s *helloWorldServer) serveHelloContent(ctx context.Context, resp http.Resp
defer transport.Closebody(req.Body, s.logErrorFunc)

reqContent := new(HelloReq)
err = decodeRequest(ctx, req, reqContent)
if err != nil {
if err := decodeRequest(ctx, req, reqContent); err != nil {
s.logErrorFunc("%v", err)
s.writeError(ctx, resp, err)
return
}
respContent := new(HelloResp)
responseCallWrapper := func() {
responseDeferWrapper := func() {
r := recover()
if r != nil {
endpointWrapper := func() (*HelloResp, error) {
deferWrapper := func() {
if r := recover(); r != nil {
terr := errors.InternalError("Internal service panic")
s.writeError(ctx, resp, terr)
panic(r)
}
}
defer responseDeferWrapper()
defer deferWrapper()

return s.Hello(ctx, reqContent)

respContent, err = s.Hello(ctx, reqContent)
}
responseCallWrapper()
respContent, err := endpointWrapper()
if err != nil {
s.writeError(ctx, resp, err)
return
Expand All @@ -177,8 +175,7 @@ func (s *helloWorldServer) serveHelloContent(ctx context.Context, resp http.Resp
return
}
ctx = transport.CallResponsePrepared(ctx, s.hooks)
err = encodeResponse(ctx, resp, respContent)
if err != nil {
if err := encodeResponse(ctx, resp, respContent); err != nil {
s.logErrorFunc("%v", err)
s.writeError(ctx, resp, err)
return
Expand Down
8 changes: 4 additions & 4 deletions internal/xgenerator/types/goblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ func (gen *GoBlockGenerator) DefMake(vars string, params []string) error {
return nil
}

func (gen *GoBlockGenerator) SCallWithDefVar(vars []string, fnc string, params []string) (string, error) {
func (gen *GoBlockGenerator) SCallWithDefVar(vars []string, fnc TypeReference, params []string) (string, error) {

if fnc == "" {
if fnc.GetName() == "" {
return "", NewGeneratorErrorString(gen, "function is missing")
}

if err := ValidateIdent(fnc); err != nil {
if err := ValidateIdent(fnc.GetName()); err != nil {
return "", NewGeneratorError(gen, err)
}

Expand All @@ -150,7 +150,7 @@ func (gen *GoBlockGenerator) SCallWithDefVar(vars []string, fnc string, params [
return "", NewGeneratorError(gen, err)
}

return fmt.Sprintf(DefSCallTpl, identifierList(vars), fnc, ValueList(params)), nil
return fmt.Sprintf(DefSCallTpl, identifierList(vars), fnc.GetName(), ValueList(params)), nil
}

func (gen *GoBlockGenerator) SCall(fnc TypeReference, params []string) (string, error) {
Expand Down

0 comments on commit 5182803

Please sign in to comment.