From 55855ccc4d61c4b865cd4cd38e5659e86cd78c3d Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 5 Apr 2022 21:23:07 +1000 Subject: [PATCH] fix: duplicate model generation --- v2/internal/binding/binding.go | 8 +++++++- v2/internal/typescriptify/typescriptify.go | 24 ++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/v2/internal/binding/binding.go b/v2/internal/binding/binding.go index c8cf7ca21..d6539c48f 100755 --- a/v2/internal/binding/binding.go +++ b/v2/internal/binding/binding.go @@ -83,9 +83,14 @@ func (b *Bindings) ToJSON() (string, error) { func (b *Bindings) WriteModels(modelsDir string) error { models := map[string]string{} + var seen slicer.StringSlicer for packageName, structsToGenerate := range b.structsToGenerateTS { thisPackageCode := "" - for _, structInterface := range structsToGenerate { + for structName, structInterface := range structsToGenerate { + fqstructname := packageName + "." + structName + if seen.Contains(fqstructname) { + continue + } w := typescriptify.New() w.Namespace = packageName w.WithBackupDir("") @@ -95,6 +100,7 @@ func (b *Bindings) WriteModels(modelsDir string) error { return err } thisPackageCode += str + seen.AddSlice(w.GetGeneratedStructs()) } models[packageName] = thisPackageCode } diff --git a/v2/internal/typescriptify/typescriptify.go b/v2/internal/typescriptify/typescriptify.go index 7b49c7101..48368442e 100644 --- a/v2/internal/typescriptify/typescriptify.go +++ b/v2/internal/typescriptify/typescriptify.go @@ -95,7 +95,7 @@ type TypeScriptify struct { fieldTypeOptions map[reflect.Type]TypeOptions // throwaway, used when converting - alreadyConverted map[reflect.Type]bool + alreadyConverted map[string]bool Namespace string } @@ -131,9 +131,6 @@ func New() *TypeScriptify { result.CreateFromMethod = true result.CreateConstructor = true - // if result.CreateFromMethod { - // fmt.Fprintln(os.Stderr, "FromMethod METHOD IS DEPRECATED AND WILL BE REMOVED!!!!!!") - // } return result } @@ -188,6 +185,14 @@ func (t *TypeScriptify) ManageType(fld interface{}, opts TypeOptions) *TypeScrip 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 { t.CreateFromMethod = b 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) { - t.alreadyConverted = make(map[reflect.Type]bool) + t.alreadyConverted = make(map[string]bool) depth := 0 result := "" @@ -343,6 +348,7 @@ func (t *TypeScriptify) Convert(customCode map[string]string) (string, error) { result += "\n" + strings.Trim(typeScriptCode, " "+t.Indent+"\r\n") } + fmt.Printf("t.structTypes: %+v\n", t.structTypes) for _, strctTyp := range t.structTypes { typeScriptCode, err := t.convertType(depth, strctTyp.Type, customCode) if err != nil { @@ -466,10 +472,10 @@ type TSNamer interface { func (t *TypeScriptify) convertEnum(depth int, typeOf reflect.Type, elements []enumElement) (string, error) { 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 } - t.alreadyConverted[typeOf] = true + t.alreadyConverted[typeOf.String()] = true entityName := t.Prefix + typeOf.Name() + t.Suffix 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) { - if _, found := t.alreadyConverted[typeOf]; found { // Already converted + if _, found := t.alreadyConverted[typeOf.String()]; found { // Already converted return "", nil } 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 result := ""