github.com/secoba/wails/v2@v2.6.4/internal/binding/binding_test/binding_tsgeneration_test.go (about)

     1  package binding_test
     2  
     3  import (
     4  	"github.com/secoba/wails/v2/internal/binding/binding_test/binding_test_import"
     5  )
     6  
     7  type GeneratedJsEntity struct {
     8  	Name string `json:"name"`
     9  }
    10  
    11  func (s GeneratedJsEntity) Get() GeneratedJsEntity {
    12  	return s
    13  }
    14  
    15  var GeneratedJsEntityTest = BindingTest{
    16  	name: "GeneratedJsEntityTest",
    17  	structs: []interface{}{
    18  		&GeneratedJsEntity{},
    19  	},
    20  	exemptions:  nil,
    21  	shouldError: false,
    22  	TsGenerationOptionsTest: TsGenerationOptionsTest{
    23  		TsPrefix: "MY_PREFIX_",
    24  		TsSuffix: "_MY_SUFFIX",
    25  	},
    26  	want: `
    27  export namespace binding_test {
    28  	
    29  	export class MY_PREFIX_GeneratedJsEntity_MY_SUFFIX {
    30  	    name: string;
    31  	
    32  	    static createFrom(source: any = {}) {
    33  	        return new MY_PREFIX_GeneratedJsEntity_MY_SUFFIX(source);
    34  	    }
    35  	
    36  	    constructor(source: any = {}) {
    37  	        if ('string' === typeof source) source = JSON.parse(source);
    38  	        this.name = source["name"];
    39  	    }
    40  	}
    41  
    42  }
    43  
    44  `,
    45  }
    46  
    47  type ParentEntity struct {
    48  	Name       string      `json:"name"`
    49  	Ref        ChildEntity `json:"ref"`
    50  	ParentProp string      `json:"parentProp"`
    51  }
    52  
    53  func (p ParentEntity) Get() ParentEntity {
    54  	return p
    55  }
    56  
    57  type ChildEntity struct {
    58  	Name      string `json:"name"`
    59  	ChildProp int    `json:"childProp"`
    60  }
    61  
    62  var GeneratedJsEntityWithNestedStructTest = BindingTest{
    63  	name: "GeneratedJsEntityWithNestedStructTest",
    64  	structs: []interface{}{
    65  		&ParentEntity{},
    66  	},
    67  	exemptions:  nil,
    68  	shouldError: false,
    69  	TsGenerationOptionsTest: TsGenerationOptionsTest{
    70  		TsPrefix: "MY_PREFIX_",
    71  		TsSuffix: "_MY_SUFFIX",
    72  	},
    73  	want: `
    74  export namespace binding_test {
    75  				
    76  				export class MY_PREFIX_ChildEntity_MY_SUFFIX {
    77  					name: string;
    78  					childProp: number;
    79  				
    80  					static createFrom(source: any = {}) {
    81  						return new MY_PREFIX_ChildEntity_MY_SUFFIX(source);
    82  					}
    83  				
    84  					constructor(source: any = {}) {
    85  						if ('string' === typeof source) source = JSON.parse(source);
    86  						this.name = source["name"];
    87  						this.childProp = source["childProp"];
    88  					}
    89  				}
    90  				export class MY_PREFIX_ParentEntity_MY_SUFFIX {
    91  					name: string;
    92  					ref: MY_PREFIX_ChildEntity_MY_SUFFIX;
    93  					parentProp: string;
    94  				
    95  					static createFrom(source: any = {}) {
    96  						return new MY_PREFIX_ParentEntity_MY_SUFFIX(source);
    97  					}
    98  				
    99  					constructor(source: any = {}) {
   100  						if ('string' === typeof source) source = JSON.parse(source);
   101  						this.name = source["name"];
   102  						this.ref = this.convertValues(source["ref"], MY_PREFIX_ChildEntity_MY_SUFFIX);
   103  						this.parentProp = source["parentProp"];
   104  					}
   105  				
   106  					convertValues(a: any, classs: any, asMap: boolean = false): any {
   107  						if (!a) {
   108  							return a;
   109  						}
   110  						if (a.slice) {
   111  							return (a as any[]).map(elem => this.convertValues(elem, classs));
   112  						} else if ("object" === typeof a) {
   113  							if (asMap) {
   114  								for (const key of Object.keys(a)) {
   115  									a[key] = new classs(a[key]);
   116  								}
   117  								return a;
   118  							}
   119  							return new classs(a);
   120  						}
   121  						return a;
   122  					}
   123  				}
   124  
   125  			}
   126  `,
   127  }
   128  
   129  type ParentPackageEntity struct {
   130  	Name string             `json:"name"`
   131  	Ref  ChildPackageEntity `json:"ref"`
   132  }
   133  
   134  func (p ParentPackageEntity) Get() ParentPackageEntity {
   135  	return p
   136  }
   137  
   138  type ChildPackageEntity struct {
   139  	Name            string                       `json:"name"`
   140  	ImportedPackage binding_test_import.AWrapper `json:"importedPackage"`
   141  }
   142  
   143  var EntityWithDiffNamespaces = BindingTest{
   144  	name: "EntityWithDiffNamespaces ",
   145  	structs: []interface{}{
   146  		&ParentPackageEntity{},
   147  	},
   148  	exemptions:  nil,
   149  	shouldError: false,
   150  	TsGenerationOptionsTest: TsGenerationOptionsTest{
   151  		TsPrefix: "MY_PREFIX_",
   152  		TsSuffix: "_MY_SUFFIX",
   153  	},
   154  	want: `
   155  export namespace binding_test {
   156              	
   157              	export class MY_PREFIX_ChildPackageEntity_MY_SUFFIX {
   158              	    name: string;
   159              	    importedPackage: binding_test_import.MY_PREFIX_AWrapper_MY_SUFFIX;
   160              	
   161              	    static createFrom(source: any = {}) {
   162              	        return new MY_PREFIX_ChildPackageEntity_MY_SUFFIX(source);
   163              	    }
   164              	
   165              	    constructor(source: any = {}) {
   166              	        if ('string' === typeof source) source = JSON.parse(source);
   167              	        this.name = source["name"];
   168              	        this.importedPackage = this.convertValues(source["importedPackage"], binding_test_import.MY_PREFIX_AWrapper_MY_SUFFIX);
   169              	    }
   170              	
   171              		convertValues(a: any, classs: any, asMap: boolean = false): any {
   172              		    if (!a) {
   173              		        return a;
   174              		    }
   175              		    if (a.slice) {
   176              		        return (a as any[]).map(elem => this.convertValues(elem, classs));
   177              		    } else if ("object" === typeof a) {
   178              		        if (asMap) {
   179              		            for (const key of Object.keys(a)) {
   180              		                a[key] = new classs(a[key]);
   181              		            }
   182              		            return a;
   183              		        }
   184              		        return new classs(a);
   185              		    }
   186              		    return a;
   187              		}
   188              	}
   189              	export class MY_PREFIX_ParentPackageEntity_MY_SUFFIX {
   190              	    name: string;
   191              	    ref: MY_PREFIX_ChildPackageEntity_MY_SUFFIX;
   192              	
   193              	    static createFrom(source: any = {}) {
   194              	        return new MY_PREFIX_ParentPackageEntity_MY_SUFFIX(source);
   195              	    }
   196              	
   197              	    constructor(source: any = {}) {
   198              	        if ('string' === typeof source) source = JSON.parse(source);
   199              	        this.name = source["name"];
   200              	        this.ref = this.convertValues(source["ref"], MY_PREFIX_ChildPackageEntity_MY_SUFFIX);
   201              	    }
   202              	
   203              		convertValues(a: any, classs: any, asMap: boolean = false): any {
   204              		    if (!a) {
   205              		        return a;
   206              		    }
   207              		    if (a.slice) {
   208              		        return (a as any[]).map(elem => this.convertValues(elem, classs));
   209              		    } else if ("object" === typeof a) {
   210              		        if (asMap) {
   211              		            for (const key of Object.keys(a)) {
   212              		                a[key] = new classs(a[key]);
   213              		            }
   214              		            return a;
   215              		        }
   216              		        return new classs(a);
   217              		    }
   218              		    return a;
   219              		}
   220              	}
   221  
   222              }
   223  
   224              export namespace binding_test_import {
   225              	
   226              	export class MY_PREFIX_AWrapper_MY_SUFFIX {
   227              	    AWrapper: binding_test_nestedimport.MY_PREFIX_A_MY_SUFFIX;
   228              	
   229              	    static createFrom(source: any = {}) {
   230              	        return new MY_PREFIX_AWrapper_MY_SUFFIX(source);
   231              	    }
   232              	
   233              	    constructor(source: any = {}) {
   234              	        if ('string' === typeof source) source = JSON.parse(source);
   235              	        this.AWrapper = this.convertValues(source["AWrapper"], binding_test_nestedimport.MY_PREFIX_A_MY_SUFFIX);
   236              	    }
   237              	
   238              		convertValues(a: any, classs: any, asMap: boolean = false): any {
   239              		    if (!a) {
   240              		        return a;
   241              		    }
   242              		    if (a.slice) {
   243              		        return (a as any[]).map(elem => this.convertValues(elem, classs));
   244              		    } else if ("object" === typeof a) {
   245              		        if (asMap) {
   246              		            for (const key of Object.keys(a)) {
   247              		                a[key] = new classs(a[key]);
   248              		            }
   249              		            return a;
   250              		        }
   251              		        return new classs(a);
   252              		    }
   253              		    return a;
   254              		}
   255              	}
   256  
   257              }
   258  
   259              export namespace binding_test_nestedimport {
   260              	
   261              	export class MY_PREFIX_A_MY_SUFFIX {
   262              	    A: string;
   263              	
   264              	    static createFrom(source: any = {}) {
   265              	        return new MY_PREFIX_A_MY_SUFFIX(source);
   266              	    }
   267              	
   268              	    constructor(source: any = {}) {
   269              	        if ('string' === typeof source) source = JSON.parse(source);
   270              	        this.A = source["A"];
   271              	    }
   272              	}
   273  
   274              }
   275  
   276  `,
   277  }