diff --git a/v2/internal/binding/binding.go b/v2/internal/binding/binding.go index f50b633b4..d26242156 100755 --- a/v2/internal/binding/binding.go +++ b/v2/internal/binding/binding.go @@ -88,17 +88,25 @@ func (b *Bindings) GenerateModels() ([]byte, error) { models := map[string]string{} var seen slicer.StringSlicer allStructNames := b.getAllStructNames() + allStructNames.Sort() for packageName, structsToGenerate := range b.structsToGenerateTS { thisPackageCode := "" w := typescriptify.New() w.Namespace = packageName w.WithBackupDir("") w.KnownStructs = allStructNames - for structName, structInterface := range structsToGenerate { + // sort the structs + var structNames []string + for structName := range structsToGenerate { + structNames = append(structNames, structName) + } + sort.Strings(structNames) + for _, structName := range structNames { fqstructname := packageName + "." + structName if seen.Contains(fqstructname) { continue } + structInterface := structsToGenerate[structName] w.Add(structInterface) } str, err := w.Convert(nil) diff --git a/v2/internal/binding/binding_test/binding_multiplestructs_test.go b/v2/internal/binding/binding_test/binding_multiplestructs_test.go new file mode 100644 index 000000000..144867813 --- /dev/null +++ b/v2/internal/binding/binding_test/binding_multiplestructs_test.go @@ -0,0 +1,88 @@ +package binding_test + +type Multistruct1 struct { + Name string `json:"name"` +} + +func (s Multistruct1) Get() Multistruct1 { + return s +} + +type Multistruct2 struct { + Name string `json:"name"` +} + +func (s Multistruct2) Get() Multistruct2 { + return s +} + +type Multistruct3 struct { + Name string `json:"name"` +} + +func (s Multistruct3) Get() Multistruct3 { + return s +} + +type Multistruct4 struct { + Name string `json:"name"` +} + +func (s Multistruct4) Get() Multistruct4 { + return s +} + +var MultistructTest = BindingTest{ + name: "Multistruct", + structs: []interface{}{ + &Multistruct1{}, + &Multistruct2{}, + &Multistruct3{}, + &Multistruct4{}, + }, + exemptions: nil, + shouldError: false, + want: `export namespace binding_test { + export class Multistruct1 { + name: string; + static createFrom(source: any = {}) { + return new Multistruct1(source); + } + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.name = source["name"]; + } + } + export class Multistruct2 { + name: string; + static createFrom(source: any = {}) { + return new Multistruct2(source); + } + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.name = source["name"]; + } + } + export class Multistruct3 { + name: string; + static createFrom(source: any = {}) { + return new Multistruct3(source); + } + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.name = source["name"]; + } + } + export class Multistruct4 { + name: string; + static createFrom(source: any = {}) { + return new Multistruct4(source); + } + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.name = source["name"]; + } + } +} +`, +} diff --git a/v2/internal/binding/binding_test/binding_test.go b/v2/internal/binding/binding_test/binding_test.go index f41507112..fb315ac1e 100644 --- a/v2/internal/binding/binding_test/binding_test.go +++ b/v2/internal/binding/binding_test/binding_test.go @@ -28,6 +28,7 @@ func TestBindings_GenerateModels(t *testing.T) { NestedFieldTest, NonStringMapKeyTest, SingleFieldTest, + MultistructTest, } testLogger := &logger.Logger{}