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

Sort structs in models.ts. Fixes #1958 (#1961)

This commit is contained in:
Lea Anthony 2022-10-14 07:50:22 +11:00 committed by GitHub
parent de1d032f10
commit 2d4f7f4de8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 1 deletions

View File

@ -88,17 +88,25 @@ func (b *Bindings) GenerateModels() ([]byte, error) {
models := map[string]string{} models := map[string]string{}
var seen slicer.StringSlicer var seen slicer.StringSlicer
allStructNames := b.getAllStructNames() allStructNames := b.getAllStructNames()
allStructNames.Sort()
for packageName, structsToGenerate := range b.structsToGenerateTS { for packageName, structsToGenerate := range b.structsToGenerateTS {
thisPackageCode := "" thisPackageCode := ""
w := typescriptify.New() w := typescriptify.New()
w.Namespace = packageName w.Namespace = packageName
w.WithBackupDir("") w.WithBackupDir("")
w.KnownStructs = allStructNames 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 fqstructname := packageName + "." + structName
if seen.Contains(fqstructname) { if seen.Contains(fqstructname) {
continue continue
} }
structInterface := structsToGenerate[structName]
w.Add(structInterface) w.Add(structInterface)
} }
str, err := w.Convert(nil) str, err := w.Convert(nil)

View File

@ -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"];
}
}
}
`,
}

View File

@ -28,6 +28,7 @@ func TestBindings_GenerateModels(t *testing.T) {
NestedFieldTest, NestedFieldTest,
NonStringMapKeyTest, NonStringMapKeyTest,
SingleFieldTest, SingleFieldTest,
MultistructTest,
} }
testLogger := &logger.Logger{} testLogger := &logger.Logger{}