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

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