Skip to content

Commit

Permalink
serverwriter: Correctly escape go keywords (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoylan authored May 13, 2019
1 parent 6315e72 commit b3b7750
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
15 changes: 9 additions & 6 deletions conjure/serverwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func getHandleMethodBody(serviceDefinition spec.ServiceDefinition, endpoint spec
}

for _, arg := range endpoint.Args {
varsToPassIntoImpl = append(varsToPassIntoImpl, expression.VariableVal(arg.ArgName))
varsToPassIntoImpl = append(varsToPassIntoImpl, expression.VariableVal(transforms.SafeName(string(arg.ArgName))))
}

returnStatements, err := getReturnStatements(serviceDefinition, endpoint, varsToPassIntoImpl, info)
Expand Down Expand Up @@ -444,7 +444,7 @@ func getBodyParamStatements(bodyParam *visitors.ArgumentDefinitionBodyParam, inf
return nil, nil
}
var body []astgen.ASTStmt
argName := string(bodyParam.ArgumentDefinition.ArgName)
argName := transforms.SafeName(string(bodyParam.ArgumentDefinition.ArgName))
typer, err := visitors.NewConjureTypeProviderTyper(bodyParam.ArgumentDefinition.Type, info)
if err != nil {
typJSON, _ := bodyParam.ArgumentDefinition.Type.MarshalJSON()
Expand Down Expand Up @@ -609,7 +609,7 @@ func getPathParamStatements(pathParams []visitors.ArgumentDefinitionPathParam, i

var strVar expression.VariableVal
if isString {
strVar = expression.VariableVal(arg.ArgName)
strVar = expression.VariableVal(transforms.SafeName(string(arg.ArgName)))
} else {
strVar = expression.VariableVal(arg.ArgName + "Str")
}
Expand Down Expand Up @@ -646,7 +646,8 @@ func getPathParamStatements(pathParams []visitors.ArgumentDefinitionPathParam, i

// type-specific unmarshal behavior
if !isString {
paramStmts, err := visitors.ParseStringParam(arg.ArgName, arg.Type, strVar, info)
argName := spec.ArgumentName(transforms.SafeName(string(arg.ArgName)))
paramStmts, err := visitors.ParseStringParam(argName, arg.Type, strVar, info)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -676,7 +677,8 @@ func getHeaderParamStatements(headerParams []visitors.ArgumentDefinitionHeaderPa
}
// type-specific unmarshal behavior
// TODO (bmoylan): lists are unimplemented right now, but we _could_ iterate through the raw map and pull them out.
paramStmts, err := visitors.ParseStringParam(arg.ArgName, arg.Type, getHeader, info)
argName := spec.ArgumentName(transforms.SafeName(string(arg.ArgName)))
paramStmts, err := visitors.ParseStringParam(argName, arg.Type, getHeader, info)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -711,7 +713,8 @@ func getQueryParamStatements(queryParams []visitors.ArgumentDefinitionQueryParam
ifErrNotNilReturnErrStatement("err", nil)
// type-specific unmarshal behavior
// TODO(bmoylan): lists are unimplemented right now, but we _could_ iterate through the raw map and pull them out.
paramStmts, err := visitors.ParseStringParam(arg.ArgName, arg.Type, getQuery, info)
argName := spec.ArgumentName(transforms.SafeName(string(arg.ArgName)))
paramStmts, err := visitors.ParseStringParam(argName, arg.Type, getQuery, info)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions godel/config/godel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ exclude:
- "conjure-go-verifier/conjure"
- "integration_test/testgenerated/errors/api"
- "integration_test/testgenerated/objects/api"
- "integration_test/testgenerated/server/api"
27 changes: 27 additions & 0 deletions integration_test/testgenerated/server/api/servers.conjure.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type TestService interface {
GetBinary(ctx context.Context) (io.ReadCloser, error)
PostBinary(ctx context.Context, myBytesArg io.ReadCloser) (io.ReadCloser, error)
PutBinary(ctx context.Context, myBytesArg io.ReadCloser) error
// An endpoint that uses go keywords
Chan(ctx context.Context, varArg string, importArg map[string]string, typeArg string, returnArg safelong.SafeLong) error
}

// RegisterRoutesTestService registers handlers for the TestService endpoints with a witchcraft wrouter.
Expand Down Expand Up @@ -61,6 +63,9 @@ func RegisterRoutesTestService(router wrouter.Router, impl TestService) error {
if err := resource.Put("PutBinary", "/binary", rest.NewJSONHandler(handler.HandlePutBinary, rest.StatusCodeMapper, rest.ErrHandler)); err != nil {
return werror.Wrap(err, "failed to add route", werror.SafeParam("routeName", "PutBinary"))
}
if err := resource.Post("Chan", "/chan/{var}", rest.NewJSONHandler(handler.HandleChan, rest.StatusCodeMapper, rest.ErrHandler)); err != nil {
return werror.Wrap(err, "failed to add route", werror.SafeParam("routeName", "Chan"))
}
return nil
}

Expand Down Expand Up @@ -214,3 +219,25 @@ func (t *testServiceHandler) HandlePutBinary(rw http.ResponseWriter, req *http.R
myBytes := req.Body
return t.impl.PutBinary(req.Context(), myBytes)
}

func (t *testServiceHandler) HandleChan(rw http.ResponseWriter, req *http.Request) error {
pathParams := wrouter.PathParams(req)
if pathParams == nil {
return werror.Error("path params not found on request: ensure this endpoint is registered with wrouter")
}
var_, ok := pathParams["var"]
if !ok {
err := werror.Error("path param not present", werror.SafeParam("pathParamName", "var"))
return rest.NewError(err, rest.StatusCode(http.StatusBadRequest))
}
type_ := req.URL.Query().Get("type")
return_, err := safelong.ParseSafeLong(req.Header.Get("X-My-Header2"))
if err != nil {
return err
}
var import_ map[string]string
if err := codecs.JSON.Decode(req.Body, &import_); err != nil {
return rest.NewError(err, rest.StatusCode(http.StatusBadRequest))
}
return t.impl.Chan(req.Context(), var_, import_, type_, return_)
}
26 changes: 26 additions & 0 deletions integration_test/testgenerated/server/api/services.conjure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type TestServiceClient interface {
GetBinary(ctx context.Context) (io.ReadCloser, error)
PostBinary(ctx context.Context, myBytesArg func() io.ReadCloser) (io.ReadCloser, error)
PutBinary(ctx context.Context, myBytesArg func() io.ReadCloser) error
// An endpoint that uses go keywords
Chan(ctx context.Context, varArg string, importArg map[string]string, typeArg string, returnArg safelong.SafeLong) error
}

type testServiceClient struct {
Expand Down Expand Up @@ -169,6 +171,24 @@ func (c *testServiceClient) PutBinary(ctx context.Context, myBytesArg func() io.
return nil
}

func (c *testServiceClient) Chan(ctx context.Context, varArg string, importArg map[string]string, typeArg string, returnArg safelong.SafeLong) error {
var requestParams []httpclient.RequestParam
requestParams = append(requestParams, httpclient.WithRPCMethodName("Chan"))
requestParams = append(requestParams, httpclient.WithRequestMethod("POST"))
requestParams = append(requestParams, httpclient.WithPathf("/chan/%s", url.PathEscape(fmt.Sprint(varArg))))
requestParams = append(requestParams, httpclient.WithJSONRequest(importArg))
requestParams = append(requestParams, httpclient.WithHeader("X-My-Header2", fmt.Sprint(returnArg)))
queryParams := make(url.Values)
queryParams.Set("type", fmt.Sprint(typeArg))
requestParams = append(requestParams, httpclient.WithQueryValues(queryParams))
resp, err := c.client.Do(ctx, requestParams...)
if err != nil {
return err
}
_ = resp
return nil
}

type TestServiceClientWithAuth interface {
Echo(ctx context.Context) error
GetPathParam(ctx context.Context, myPathParamArg string) error
Expand All @@ -178,6 +198,8 @@ type TestServiceClientWithAuth interface {
GetBinary(ctx context.Context) (io.ReadCloser, error)
PostBinary(ctx context.Context, myBytesArg func() io.ReadCloser) (io.ReadCloser, error)
PutBinary(ctx context.Context, myBytesArg func() io.ReadCloser) error
// An endpoint that uses go keywords
Chan(ctx context.Context, varArg string, importArg map[string]string, typeArg string, returnArg safelong.SafeLong) error
}

func NewTestServiceClientWithAuth(client TestServiceClient, authHeader bearertoken.Token, cookieToken bearertoken.Token) TestServiceClientWithAuth {
Expand Down Expand Up @@ -221,3 +243,7 @@ func (c *testServiceClientWithAuth) PostBinary(ctx context.Context, myBytesArg f
func (c *testServiceClientWithAuth) PutBinary(ctx context.Context, myBytesArg func() io.ReadCloser) error {
return c.client.PutBinary(ctx, myBytesArg)
}

func (c *testServiceClientWithAuth) Chan(ctx context.Context, varArg string, importArg map[string]string, typeArg string, returnArg safelong.SafeLong) error {
return c.client.Chan(ctx, varArg, importArg, typeArg, returnArg)
}
13 changes: 13 additions & 0 deletions integration_test/testgenerated/server/server-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ services:
http: PUT /binary
args:
myBytes: binary
chan:
docs: An endpoint that uses go keywords
http: POST /chan/{var}
args:
var: string
import: map<string, string>
type:
param-type: query
type: string
return:
param-type: header
param-id: X-My-Header2
type: safelong

0 comments on commit b3b7750

Please sign in to comment.