5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 04:42:00 +08:00

fix: duplicate model generation

This commit is contained in:
Lea Anthony 2022-04-05 21:23:07 +10:00
parent eb97f53124
commit 55855ccc4d
2 changed files with 22 additions and 10 deletions

View File

@ -83,9 +83,14 @@ func (b *Bindings) ToJSON() (string, error) {
func (b *Bindings) WriteModels(modelsDir string) error { func (b *Bindings) WriteModels(modelsDir string) error {
models := map[string]string{} models := map[string]string{}
var seen slicer.StringSlicer
for packageName, structsToGenerate := range b.structsToGenerateTS { for packageName, structsToGenerate := range b.structsToGenerateTS {
thisPackageCode := "" thisPackageCode := ""
for _, structInterface := range structsToGenerate { for structName, structInterface := range structsToGenerate {
fqstructname := packageName + "." + structName
if seen.Contains(fqstructname) {
continue
}
w := typescriptify.New() w := typescriptify.New()
w.Namespace = packageName w.Namespace = packageName
w.WithBackupDir("") w.WithBackupDir("")
@ -95,6 +100,7 @@ func (b *Bindings) WriteModels(modelsDir string) error {
return err return err
} }
thisPackageCode += str thisPackageCode += str
seen.AddSlice(w.GetGeneratedStructs())
} }
models[packageName] = thisPackageCode models[packageName] = thisPackageCode
} }

View File

@ -95,7 +95,7 @@ type TypeScriptify struct {
fieldTypeOptions map[reflect.Type]TypeOptions fieldTypeOptions map[reflect.Type]TypeOptions
// throwaway, used when converting // throwaway, used when converting
alreadyConverted map[reflect.Type]bool alreadyConverted map[string]bool
Namespace string Namespace string
} }
@ -131,9 +131,6 @@ func New() *TypeScriptify {
result.CreateFromMethod = true result.CreateFromMethod = true
result.CreateConstructor = true result.CreateConstructor = true
// if result.CreateFromMethod {
// fmt.Fprintln(os.Stderr, "FromMethod METHOD IS DEPRECATED AND WILL BE REMOVED!!!!!!")
// }
return result return result
} }
@ -188,6 +185,14 @@ func (t *TypeScriptify) ManageType(fld interface{}, opts TypeOptions) *TypeScrip
return t return t
} }
func (t *TypeScriptify) GetGeneratedStructs() []string {
var result []string
for key := range t.alreadyConverted {
result = append(result, key)
}
return result
}
func (t *TypeScriptify) WithCreateFromMethod(b bool) *TypeScriptify { func (t *TypeScriptify) WithCreateFromMethod(b bool) *TypeScriptify {
t.CreateFromMethod = b t.CreateFromMethod = b
return t return t
@ -323,7 +328,7 @@ func (t *TypeScriptify) AddEnumValues(typeOf reflect.Type, values interface{}) *
} }
func (t *TypeScriptify) Convert(customCode map[string]string) (string, error) { func (t *TypeScriptify) Convert(customCode map[string]string) (string, error) {
t.alreadyConverted = make(map[reflect.Type]bool) t.alreadyConverted = make(map[string]bool)
depth := 0 depth := 0
result := "" result := ""
@ -343,6 +348,7 @@ func (t *TypeScriptify) Convert(customCode map[string]string) (string, error) {
result += "\n" + strings.Trim(typeScriptCode, " "+t.Indent+"\r\n") result += "\n" + strings.Trim(typeScriptCode, " "+t.Indent+"\r\n")
} }
fmt.Printf("t.structTypes: %+v\n", t.structTypes)
for _, strctTyp := range t.structTypes { for _, strctTyp := range t.structTypes {
typeScriptCode, err := t.convertType(depth, strctTyp.Type, customCode) typeScriptCode, err := t.convertType(depth, strctTyp.Type, customCode)
if err != nil { if err != nil {
@ -466,10 +472,10 @@ type TSNamer interface {
func (t *TypeScriptify) convertEnum(depth int, typeOf reflect.Type, elements []enumElement) (string, error) { func (t *TypeScriptify) convertEnum(depth int, typeOf reflect.Type, elements []enumElement) (string, error) {
t.logf(depth, "Converting enum %s", typeOf.String()) t.logf(depth, "Converting enum %s", typeOf.String())
if _, found := t.alreadyConverted[typeOf]; found { // Already converted if _, found := t.alreadyConverted[typeOf.String()]; found { // Already converted
return "", nil return "", nil
} }
t.alreadyConverted[typeOf] = true t.alreadyConverted[typeOf.String()] = true
entityName := t.Prefix + typeOf.Name() + t.Suffix entityName := t.Prefix + typeOf.Name() + t.Suffix
result := "enum " + entityName + " {\n" result := "enum " + entityName + " {\n"
@ -552,7 +558,7 @@ func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool)
} }
func (t *TypeScriptify) convertType(depth int, typeOf reflect.Type, customCode map[string]string) (string, error) { func (t *TypeScriptify) convertType(depth int, typeOf reflect.Type, customCode map[string]string) (string, error) {
if _, found := t.alreadyConverted[typeOf]; found { // Already converted if _, found := t.alreadyConverted[typeOf.String()]; found { // Already converted
return "", nil return "", nil
} }
t.logf(depth, "Converting type %s", typeOf.String()) t.logf(depth, "Converting type %s", typeOf.String())
@ -563,7 +569,7 @@ func (t *TypeScriptify) convertType(depth int, typeOf reflect.Type, customCode m
} }
} }
t.alreadyConverted[typeOf] = true t.alreadyConverted[typeOf.String()] = true
entityName := t.Prefix + typeOf.Name() + t.Suffix entityName := t.Prefix + typeOf.Name() + t.Suffix
result := "" result := ""