route support no request and response (#178)

* add more test and support no request and response

* fix slash when run on windows

* optimize test
master
kingxt 4 years ago committed by GitHub
parent 4c9315e984
commit b776468e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -185,6 +185,25 @@ service A-api {
}
`
const apiRouteTest = `
type Request struct {
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
}
type Response struct {
Message string ` + "`" + `json:"message"` + "`" + `
}
service A-api {
@handler NormalHandler
get /greet/from/:name(Request) returns (Response)
@handler NoResponseHandler
get /greet/from/:sex(Request)
@handler NoRequestHandler
get /greet/from/request returns (Response)
@handler NoRequestNoResponseHandler
get /greet/from
}
`
func TestParser(t *testing.T) {
filename := "greet.api"
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
@ -333,6 +352,21 @@ func TestApiHasNoRequestBody(t *testing.T) {
validate(t, filename)
}
func TestApiRoutes(t *testing.T) {
filename := "greet.api"
err := ioutil.WriteFile(filename, []byte(apiRouteTest), os.ModePerm)
assert.Nil(t, err)
defer os.Remove(filename)
parser, err := parser.NewParser(filename)
assert.Nil(t, err)
_, err = parser.Parse()
assert.Nil(t, err)
validate(t, filename)
}
func validate(t *testing.T, api string) {
dir := "_go"
err := DoGenProject(api, dir, true)

@ -28,6 +28,7 @@ func getParentPackage(dir string) (string, error) {
}
parent := filepath.Clean(goctlutil.JoinPackages(p.GoMod.Module, absPath[len(goModePath):]))
parent = strings.ReplaceAll(parent, "\\", "/")
parent = strings.ReplaceAll(parent, `\`, "/")
return parent, nil
}

@ -70,6 +70,12 @@ func (p *serviceEntityParser) parseLine(line string, api *spec.ApiSpec, annos []
ch, _, err := reader.ReadRune()
if err != nil {
if err == io.EOF {
if builder.Len() > 0 {
token := strings.TrimSpace(builder.String())
if len(token) > 0 && token != returnsTag {
fields = append(fields, token)
}
}
break
}
return err

@ -118,7 +118,7 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
return &Project{
Name: name,
Path: path,
Package: pkg,
Package: strings.ReplaceAll(pkg, `\`, "/"),
IsInGoEnv: isInGoEnv,
GoMod: GoMod{
Module: module,

Loading…
Cancel
Save