* feat: add color to debug (#2433) * fix header and path type ts gen Co-authored-by: chen quan <chenquan.dev@gmail.com>
This commit is contained in:
@@ -105,20 +105,43 @@ func paramsForRoute(route spec.Route) string {
|
||||
}
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
hasHeader := hasRequestHeader(route)
|
||||
hasPath := hasRequestPath(route)
|
||||
rt, err := goTypeToTs(route.RequestType, true)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
if hasParams && hasBody {
|
||||
return fmt.Sprintf("params: %s, req: %s", rt+"Params", rt)
|
||||
} else if hasParams {
|
||||
return fmt.Sprintf("params: %s", rt+"Params")
|
||||
} else if hasBody {
|
||||
return fmt.Sprintf("req: %s", rt)
|
||||
var params []string
|
||||
if hasParams {
|
||||
params = append(params, fmt.Sprintf("params: %s", rt+"Params"))
|
||||
}
|
||||
return ""
|
||||
if hasBody {
|
||||
params = append(params, fmt.Sprintf("req: %s", rt))
|
||||
}
|
||||
if hasHeader {
|
||||
params = append(params, fmt.Sprintf("headers: %s", rt+"Headers"))
|
||||
}
|
||||
if hasPath {
|
||||
ds, ok := route.RequestType.(spec.DefineStruct)
|
||||
if !ok {
|
||||
fmt.Printf("invalid route.RequestType: {%v}\n", route.RequestType)
|
||||
}
|
||||
members := ds.GetTagMembers(pathTagKey)
|
||||
for _, member := range members {
|
||||
tags := member.Tags()
|
||||
|
||||
if len(tags) > 0 && tags[0].Key == pathTagKey {
|
||||
valueType, err := goTypeToTs(member.Type, false)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return ""
|
||||
}
|
||||
params = append(params, fmt.Sprintf("%s: %s", tags[0].Name, valueType))
|
||||
}
|
||||
}
|
||||
}
|
||||
return strings.Join(params, ", ")
|
||||
}
|
||||
|
||||
func commentForRoute(route spec.Route) string {
|
||||
@@ -128,13 +151,15 @@ func commentForRoute(route spec.Route) string {
|
||||
builder.WriteString("\n * @description " + comment)
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
if hasParams && hasBody {
|
||||
hasHeader := hasRequestHeader(route)
|
||||
if hasParams {
|
||||
builder.WriteString("\n * @param params")
|
||||
}
|
||||
if hasBody {
|
||||
builder.WriteString("\n * @param req")
|
||||
} else if hasParams {
|
||||
builder.WriteString("\n * @param params")
|
||||
} else if hasBody {
|
||||
builder.WriteString("\n * @param req")
|
||||
}
|
||||
if hasHeader {
|
||||
builder.WriteString("\n * @param headers")
|
||||
}
|
||||
builder.WriteString("\n */")
|
||||
return builder.String()
|
||||
@@ -143,26 +168,42 @@ func commentForRoute(route spec.Route) string {
|
||||
func callParamsForRoute(route spec.Route, group spec.Group) string {
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
if hasParams && hasBody {
|
||||
return fmt.Sprintf("%s, %s, %s", pathForRoute(route, group), "params", "req")
|
||||
} else if hasParams {
|
||||
return fmt.Sprintf("%s, %s", pathForRoute(route, group), "params")
|
||||
} else if hasBody {
|
||||
return fmt.Sprintf("%s, %s", pathForRoute(route, group), "req")
|
||||
hasHeader := hasRequestHeader(route)
|
||||
|
||||
var params = []string{pathForRoute(route, group)}
|
||||
if hasParams {
|
||||
params = append(params, "params")
|
||||
}
|
||||
if hasBody {
|
||||
params = append(params, "req")
|
||||
}
|
||||
if hasHeader {
|
||||
params = append(params, "headers")
|
||||
}
|
||||
|
||||
return pathForRoute(route, group)
|
||||
return strings.Join(params, ", ")
|
||||
}
|
||||
|
||||
func pathForRoute(route spec.Route, group spec.Group) string {
|
||||
prefix := group.GetAnnotation(pathPrefix)
|
||||
|
||||
routePath := route.Path
|
||||
if strings.Contains(routePath, ":") {
|
||||
pathSlice := strings.Split(routePath, "/")
|
||||
for i, part := range pathSlice {
|
||||
if strings.Contains(part, ":") {
|
||||
pathSlice[i] = fmt.Sprintf("${%s}", part[1:])
|
||||
}
|
||||
}
|
||||
routePath = strings.Join(pathSlice, "/")
|
||||
}
|
||||
if len(prefix) == 0 {
|
||||
return "\"" + route.Path + "\""
|
||||
return "`" + routePath + "`"
|
||||
}
|
||||
|
||||
prefix = strings.TrimPrefix(prefix, `"`)
|
||||
prefix = strings.TrimSuffix(prefix, `"`)
|
||||
return fmt.Sprintf(`"%s/%s"`, prefix, strings.TrimPrefix(route.Path, "/"))
|
||||
return fmt.Sprintf("`%s/%s`", prefix, strings.TrimPrefix(routePath, "/"))
|
||||
}
|
||||
|
||||
func pathHasParams(route spec.Route) bool {
|
||||
@@ -182,3 +223,21 @@ func hasRequestBody(route spec.Route) bool {
|
||||
|
||||
return len(route.RequestTypeName()) > 0 && len(ds.GetBodyMembers()) > 0
|
||||
}
|
||||
|
||||
func hasRequestPath(route spec.Route) bool {
|
||||
ds, ok := route.RequestType.(spec.DefineStruct)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return len(route.RequestTypeName()) > 0 && len(ds.GetTagMembers(pathTagKey)) > 0
|
||||
}
|
||||
|
||||
func hasRequestHeader(route spec.Route) bool {
|
||||
ds, ok := route.RequestType.(spec.DefineStruct)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return len(route.RequestTypeName()) > 0 && len(ds.GetTagMembers(headerTagKey)) > 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user