github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/binding/binding_test/binding_nestedfield_test.go (about)

     1  package binding_test
     2  
     3  type As struct {
     4  	B Bs `json:"b"`
     5  }
     6  
     7  type Bs struct {
     8  	Name string `json:"name"`
     9  }
    10  
    11  func (a As) Get() As {
    12  	return a
    13  }
    14  
    15  var NestedFieldTest = BindingTest{
    16  	name: "NestedField",
    17  	structs: []interface{}{
    18  		&As{},
    19  	},
    20  	exemptions:  nil,
    21  	shouldError: false,
    22  	want: `
    23  export namespace binding_test {
    24  	export class Bs {
    25  		name: string;
    26  		static createFrom(source: any = {}) {
    27  			return new Bs(source);
    28  		}
    29  		constructor(source: any = {}) {
    30  			if ('string' === typeof source) source = JSON.parse(source);
    31  			this.name = source["name"];
    32  		}
    33  	}
    34  	export class As {
    35  		b: Bs;
    36  		static createFrom(source: any = {}) {
    37  			return new As(source);
    38  		}
    39  		constructor(source: any = {}) {
    40  			if ('string' === typeof source) source = JSON.parse(source);
    41  			this.b = this.convertValues(source["b"], Bs);
    42  		}
    43  		convertValues(a: any, classs: any, asMap: boolean = false): any {
    44  			if (!a) {
    45  				return a;
    46  			}
    47  			if (a.slice) {
    48  				return (a as any[]).map(elem => this.convertValues(elem, classs));
    49  			} else if ("object" === typeof a) {
    50  				if (asMap) {
    51  					for (const key of Object.keys(a)) {
    52  						a[key] = new classs(a[key]);
    53  					}
    54  					return a;
    55  				}
    56  				return new classs(a);
    57  			}
    58  			return a;
    59  		}
    60  	}
    61  }
    62  `,
    63  }