mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
chore: refactor BuildTypes in tsgen. (#4266)
This commit is contained in:
@@ -14,13 +14,31 @@ import (
|
||||
//go:embed components.tpl
|
||||
var componentsTemplate string
|
||||
|
||||
// BuildTypes generates the typescript code for the types.
|
||||
func BuildTypes(types []spec.Type) (string, error) {
|
||||
var builder strings.Builder
|
||||
first := true
|
||||
for _, tp := range types {
|
||||
if first {
|
||||
first = false
|
||||
} else {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
if err := writeType(&builder, tp); err != nil {
|
||||
return "", apiutil.WrapErr(err, "Type "+tp.Name()+" generate error")
|
||||
}
|
||||
}
|
||||
|
||||
return builder.String(), nil
|
||||
}
|
||||
|
||||
func genComponents(dir string, api *spec.ApiSpec) error {
|
||||
types := api.Types
|
||||
if len(types) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
val, err := buildTypes(types)
|
||||
val, err := BuildTypes(types)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -45,24 +63,3 @@ func genComponents(dir string, api *spec.ApiSpec) error {
|
||||
"componentTypes": val,
|
||||
})
|
||||
}
|
||||
|
||||
func buildTypes(types []spec.Type) (string, error) {
|
||||
var builder strings.Builder
|
||||
first := true
|
||||
for _, tp := range types {
|
||||
if first {
|
||||
first = false
|
||||
} else {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
if err := writeType(&builder, tp); err != nil {
|
||||
return "", apiutil.WrapErr(err, "Type "+tp.Name()+" generate error")
|
||||
}
|
||||
}
|
||||
|
||||
return builder.String(), nil
|
||||
}
|
||||
|
||||
func BuildTypes(types []spec.Type) (string, error) {
|
||||
return buildTypes(types)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user