Skip to content

Commit

Permalink
[bugfix] fix types referencing itself in imports
Browse files Browse the repository at this point in the history
happens when there's a deep nesting of structtype globaltype etc

will flesh this out later
  • Loading branch information
lolopinto committed Sep 27, 2023
1 parent b0f7348 commit 13d26a0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/tscode/custom_interface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
{{ $pkg := .Package }}
{{ $cfg := .Config }}
{{ $schema := .Schema }}
{{ $root := .Root }}


{{ range $ci.GetAllCustomTypes -}}
{{ if .IsCustomInterface -}}
{{ template "interface.tmpl" (dict "RootInterface" $ci "Interface" . "Package" $pkg "Config" $cfg)}}
{{ if $root -}}
{{ template "interface.tmpl" (dict "RootInterface" $root "Interface" . "Package" $pkg "Config" $cfg)}}
{{ else -}}
{{ template "interface.tmpl" (dict "RootInterface" $ci "Interface" . "Package" $pkg "Config" $cfg)}}
{{ end -}}
{{ if .HasConvertFunction $cfg }}
{{ if .Exported -}}
export function {{.GetConvertMethod}}(input: any): {{.TSType}} {
Expand Down
29 changes: 29 additions & 0 deletions internal/tscode/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,28 @@ func writeLoadAnyFile(nodeData []*enum.Data, processor *codegen.Processor) error
})
}

type typesRoot struct {
s *schema.Schema
m map[string]bool
}

func (t *typesRoot) init() {
t.m = make(map[string]bool)
for _, ci := range t.s.CustomInterfaces {
for _, tt := range ci.GetAllCustomTypes() {
t.m[tt.GetTSType()] = true
}

for _, tt := range ci.GetAllEnums() {
t.m[tt.Name] = true
}
}
}

func (t *typesRoot) ForeignImport(name string) bool {
return !t.m[name]
}

func writeTypesFile(processor *codegen.Processor, nodeData []*enum.Data, edgeData []*enum.Data) error {
cfg := processor.Config
filePath := getFilePathForTypesFile(cfg)
Expand All @@ -910,6 +932,9 @@ func writeTypesFile(processor *codegen.Processor, nodeData []*enum.Data, edgeDat
return edgeData[i].Name < edgeData[j].Name
})

root := &typesRoot{s: processor.Schema}
root.init()

return file.Write(&file.TemplatedBasedFileWriter{
Config: processor.Config,
Data: struct {
Expand All @@ -918,6 +943,7 @@ func writeTypesFile(processor *codegen.Processor, nodeData []*enum.Data, edgeDat
Config *codegen.Config
NodeType *enum.Enum
EdgeType *enum.Enum
Root *typesRoot
}{
processor.Schema,
cfg.GetImportPackage(),
Expand All @@ -930,6 +956,9 @@ func writeTypesFile(processor *codegen.Processor, nodeData []*enum.Data, edgeDat
Name: "EdgeType",
Values: edgeData,
},
// used to know if we should import a foreign type
// e.g. in types.tmpl, we don't wanna import types defined in that file
root,
},
AbsPathToTemplate: util.GetAbsolutePath("types.tmpl"),
OtherTemplateFiles: []string{
Expand Down
3 changes: 2 additions & 1 deletion internal/tscode/types.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{{ $package := .Package }}
{{ $config := .Config }}

{{ $root := .Root }}
{{ range .Schema.CustomInterfaces }}
{{ template "custom_interface.tmpl" (dict "Interface" . "Package" $package "Schema" $schema "Config" $config) }}
{{ template "custom_interface.tmpl" (dict "Interface" . "Package" $package "Schema" $schema "Config" $config "Root" $root) }}
{{ end -}}

0 comments on commit 13d26a0

Please sign in to comment.