|
@@ -4,6 +4,7 @@ import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
+ "os"
|
|
|
"strings"
|
|
|
"text/template"
|
|
|
|
|
@@ -57,15 +58,21 @@ func genFile(c fileGenConfig) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
-func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
|
|
|
+func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int, api *spec.ApiSpec) error {
|
|
|
util.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.Name(), tag, comment)
|
|
|
+ var refPropertyName = tp.Name()
|
|
|
+ if isCustomType(refPropertyName) {
|
|
|
+ strs := getRefProperty(api, refPropertyName, name)
|
|
|
+ _, err = fmt.Fprintf(writer, "%s\n", strs)
|
|
|
} else {
|
|
|
- _, err = fmt.Fprintf(writer, "%s %s %s\n", strings.Title(name), tp.Name(), tag)
|
|
|
+ if len(comment) > 0 {
|
|
|
+ comment = strings.TrimPrefix(comment, "//")
|
|
|
+ comment = "//" + comment
|
|
|
+ _, err = fmt.Fprintf(writer, "%s %s %s %s\n", strings.Title(name), tp.Name(), tag, comment)
|
|
|
+ } else {
|
|
|
+ _, err = fmt.Fprintf(writer, "%s %s %s\n", strings.Title(name), tp.Name(), tag)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return err
|
|
@@ -181,3 +188,58 @@ func golangExpr(ty spec.Type, pkg ...string) string {
|
|
|
|
|
|
return ""
|
|
|
}
|
|
|
+
|
|
|
+func isCustomType(t string) bool {
|
|
|
+ var builtinType = []string{"string", "bool", "int", "uint", "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "float32", "float64", "uintptr", "complex64", "complex128"}
|
|
|
+ var is bool = true
|
|
|
+ for _, v := range builtinType {
|
|
|
+ if t == v {
|
|
|
+ is = false
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return is
|
|
|
+}
|
|
|
+
|
|
|
+// Generate nested types recursively
|
|
|
+func getRefProperty(api *spec.ApiSpec, refPropertyName string, name string) string {
|
|
|
+ var str string = ""
|
|
|
+ for _, t := range api.Types {
|
|
|
+ if strings.TrimLeft(refPropertyName, "*") == t.Name() {
|
|
|
+ switch tm := t.(type) {
|
|
|
+ case spec.DefineStruct:
|
|
|
+ for _, m := range tm.Members {
|
|
|
+ if isCustomType(m.Type.Name()) {
|
|
|
+ // recursive
|
|
|
+ str += getRefProperty(api, m.Type.Name(), m.Name)
|
|
|
+ } else {
|
|
|
+ if len(m.Comment) > 0 {
|
|
|
+ comment := strings.TrimPrefix(m.Comment, "//")
|
|
|
+ comment = "//" + comment
|
|
|
+ str += fmt.Sprintf("%s %s %s %s\n\t", m.Name, m.Type.Name(), m.Tag, comment)
|
|
|
+ } else {
|
|
|
+ str += fmt.Sprintf("%s %s %s\n\t", m.Name, m.Type.Name(), m.Tag)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if name == "" {
|
|
|
+ temp := `${str}`
|
|
|
+ return os.Expand(temp, func(k string) string {
|
|
|
+ return str
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ temp := `${name} struct {
|
|
|
+ ${str}}`
|
|
|
+ return os.Expand(temp, func(k string) string {
|
|
|
+ return map[string]string{
|
|
|
+ "name": name,
|
|
|
+ "str": str,
|
|
|
+ }[k]
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|