mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 04:11:05 +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:
parent
c55f94a3dc
commit
dc65f77baf
@ -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)
|
||||
|
53
v2/internal/binding/binding_test/binding_emptystruct_test.go
Normal file
53
v2/internal/binding/binding_test/binding_emptystruct_test.go
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
@ -29,6 +29,7 @@ func TestBindings_GenerateModels(t *testing.T) {
|
||||
NonStringMapKeyTest,
|
||||
SingleFieldTest,
|
||||
MultistructTest,
|
||||
EmptyStructTest,
|
||||
}
|
||||
|
||||
testLogger := &logger.Logger{}
|
||||
|
Loading…
Reference in New Issue
Block a user