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

repair panic (#1999)

* repair panic

* Add empty struct field test

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
This commit is contained in:
ParkourLiu 2022-10-22 18:04:49 +08:00 committed by GitHub
parent c55f94a3dc
commit dc65f77baf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 2 deletions

View File

@ -187,7 +187,11 @@ func (b *Bindings) AddStructToGenerateTS(packageName string, structName string,
continue
}
fqname := field.Type.String()
sName := strings.Split(fqname, ".")[1]
sNameSplit := strings.Split(fqname, ".")
if len(sNameSplit) < 2 {
continue
}
sName := sNameSplit[1]
pName := getPackageName(fqname)
a := reflect.New(field.Type)
if b.hasExportedJSONFields(field.Type) {
@ -199,7 +203,11 @@ func (b *Bindings) AddStructToGenerateTS(packageName string, structName string,
continue
}
fqname := field.Type.Elem().String()
sName := strings.Split(fqname, ".")[1]
sNameSplit := strings.Split(fqname, ".")
if len(sNameSplit) < 2 {
continue
}
sName := sNameSplit[1]
pName := getPackageName(fqname)
typ := field.Type.Elem()
a := reflect.New(typ)

View File

@ -0,0 +1,53 @@
package binding_test
type EmptyStruct struct {
Empty struct{} `json:"empty"`
}
func (s EmptyStruct) Get() EmptyStruct {
return s
}
var EmptyStructTest = BindingTest{
name: "EmptyStruct",
structs: []interface{}{
&EmptyStruct{},
},
exemptions: nil,
shouldError: false,
want: `
export namespace binding_test {
export class EmptyStruct {
// Go type: struct {}
empty: any;
static createFrom(source: any = {}) {
return new EmptyStruct(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.empty = this.convertValues(source["empty"], null);
}
convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
}
}
}
`,
}

View File

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