You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package gogen
|
|
|
|
import (
|
|
"fmt"
|
|
goformat "go/format"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"zero/core/collection"
|
|
"zero/tools/goctl/api/spec"
|
|
"zero/tools/goctl/api/util"
|
|
)
|
|
|
|
func getParentPackage(dir string) (string, error) {
|
|
absDir, err := filepath.Abs(dir)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
gopath := os.Getenv("GOPATH")
|
|
parent := path.Join(gopath, "src")
|
|
pos := strings.Index(absDir, parent)
|
|
if pos < 0 {
|
|
return "", fmt.Errorf("%s not in GOPATH %s directory", absDir, gopath)
|
|
}
|
|
return absDir[len(parent)+1:], nil
|
|
}
|
|
|
|
func writeIndent(writer io.Writer, indent int) {
|
|
for i := 0; i < indent; i++ {
|
|
fmt.Fprint(writer, "\t")
|
|
}
|
|
}
|
|
|
|
func writeProperty(writer io.Writer, name, tp, tag, comment string, indent int) error {
|
|
writeIndent(writer, indent)
|
|
var err error
|
|
if len(comment) > 0 {
|
|
comment = strings.TrimPrefix(comment, "//")
|
|
comment = "//" + comment
|
|
_, err = fmt.Fprintf(writer, "%s %s %s %s\n", strings.Title(name), tp, tag, comment)
|
|
} else {
|
|
_, err = fmt.Fprintf(writer, "%s %s %s\n", strings.Title(name), tp, tag)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func getAuths(api *spec.ApiSpec) []string {
|
|
var authNames = collection.NewSet()
|
|
for _, g := range api.Service.Groups {
|
|
if value, ok := util.GetAnnotationValue(g.Annotations, "server", "jwt"); ok {
|
|
authNames.Add(value)
|
|
}
|
|
if value, ok := util.GetAnnotationValue(g.Annotations, "server", "signature"); ok {
|
|
authNames.Add(value)
|
|
}
|
|
}
|
|
return authNames.KeysStr()
|
|
}
|
|
|
|
func formatCode(code string) string {
|
|
ret, err := goformat.Source([]byte(code))
|
|
if err != nil {
|
|
return code
|
|
}
|
|
return string(ret)
|
|
}
|