remove some runtime allocaions

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-03-23 16:56:11 +03:00
parent bce3839877
commit c7ec840ac0

32
util.go
View File

@ -10,6 +10,20 @@ import (
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
var (
httpMethodMap = map[string]string{
"GET": "MethodGet",
"HEAD": "MethodHead",
"POST": "MethodPost",
"PUT": "MethodPut",
"PATCH": "MethodPatch",
"DELETE": "MethodDelete",
"CONNECT": "MethodConnect",
"OPTIONS": "MethodOptions",
"TRACE": "MethodTrace",
}
)
func unexport(s string) string { func unexport(s string) string {
return strings.ToLower(s[:1]) + s[1:] return strings.ToLower(s[:1]) + s[1:]
} }
@ -61,9 +75,15 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("opts = append(opts,") gfile.P("opts = append(opts,")
endpoints, _ := generateEndpoints(method) endpoints, _ := generateEndpoints(method)
path, method, body := getEndpoint(endpoints[0]) path, method, body := getEndpoint(endpoints[0])
gfile.P(microClientHttpPackage.Ident("Method"), `("`, method, `"),`) if vmethod, ok := httpMethodMap[method]; ok {
gfile.P(microClientHttpPackage.Ident("Method"), `(`, httpPackage.Ident(vmethod), `),`)
} else {
gfile.P(microClientHttpPackage.Ident("Method"), `("`, method, `"),`)
}
gfile.P(microClientHttpPackage.Ident("Path"), `("`, path, `"),`) gfile.P(microClientHttpPackage.Ident("Path"), `("`, path, `"),`)
gfile.P(microClientHttpPackage.Ident("Body"), `("`, body, `"),`) if body != "" {
gfile.P(microClientHttpPackage.Ident("Body"), `("`, body, `"),`)
}
gfile.P(")") gfile.P(")")
} }
} }
@ -539,8 +559,12 @@ func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodN
path, meth, body := getEndpoint(rule) path, meth, body := getEndpoint(rule)
gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName)) gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName))
gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path)) gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
gfile.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth)) if vmethod, ok := httpMethodMap[meth]; ok {
if len(rule.GetGet()) == 0 { gfile.P("Method:", `[]string{`, httpPackage.Ident(vmethod), `},`)
} else {
gfile.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth))
}
if len(rule.GetGet()) == 0 && body != "" {
gfile.P("Body:", fmt.Sprintf(`"%s",`, body)) gfile.P("Body:", fmt.Sprintf(`"%s",`, body))
} }
if streaming { if streaming {