mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
Fix binding generation special cases (#1902)
* Make binding.go easier to test * Fix non-deterministic namespace order for bindings * Add binding tests * Fix nested import structs, non-string map keys, and escape invalid variable names Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package binding_test
|
||||
|
||||
import "github.com/wailsapp/wails/v2/internal/binding/binding_test/binding_test_import"
|
||||
|
||||
type ImportedStruct struct {
|
||||
AWrapperContainer binding_test_import.AWrapper `json:"AWrapperContainer"`
|
||||
}
|
||||
|
||||
func (s ImportedStruct) Get() ImportedStruct {
|
||||
return s
|
||||
}
|
||||
|
||||
var ImportedStructTest = BindingTest{
|
||||
name: "ImportedStruct",
|
||||
structs: []interface{}{
|
||||
&ImportedStruct{},
|
||||
},
|
||||
exemptions: nil,
|
||||
shouldError: false,
|
||||
want: `
|
||||
export namespace binding_test {
|
||||
export class ImportedStruct {
|
||||
AWrapperContainer: binding_test_import.AWrapper;
|
||||
static createFrom(source: any = {}) {
|
||||
return new ImportedStruct(source);
|
||||
}
|
||||
constructor(source: any = {}) {
|
||||
if ('string' === typeof source) source = JSON.parse(source);
|
||||
this.AWrapperContainer = this.convertValues(source["AWrapperContainer"], binding_test_import.AWrapper);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export namespace binding_test_import {
|
||||
export class AWrapper {
|
||||
AWrapper: binding_test_nestedimport.A;
|
||||
static createFrom(source: any = {}) {
|
||||
return new AWrapper(source);
|
||||
}
|
||||
constructor(source: any = {}) {
|
||||
if ('string' === typeof source) source = JSON.parse(source);
|
||||
this.AWrapper = this.convertValues(source["AWrapper"], binding_test_nestedimport.A);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export namespace binding_test_nestedimport {
|
||||
export class A {
|
||||
A: string;
|
||||
static createFrom(source: any = {}) {
|
||||
return new A(source);
|
||||
}
|
||||
constructor(source: any = {}) {
|
||||
if ('string' === typeof source) source = JSON.parse(source);
|
||||
this.A = source["A"];
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
||||
Reference in New Issue
Block a user