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

     1  package binding_test
     2  
     3  import (
     4  	"github.com/AlpineAIO/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  }
   278  
   279  type IntEnum int
   280  
   281  const (
   282  	IntEnumValue1 IntEnum = iota
   283  	IntEnumValue2
   284  	IntEnumValue3
   285  )
   286  
   287  var AllIntEnumValues = []struct {
   288  	Value  IntEnum
   289  	TSName string
   290  }{
   291  	{IntEnumValue1, "Value1"},
   292  	{IntEnumValue2, "Value2"},
   293  	{IntEnumValue3, "Value3"},
   294  }
   295  
   296  type EntityWithIntEnum struct {
   297  	Name string  `json:"name"`
   298  	Enum IntEnum `json:"enum"`
   299  }
   300  
   301  func (e EntityWithIntEnum) Get() EntityWithIntEnum {
   302  	return e
   303  }
   304  
   305  var GeneratedJsEntityWithIntEnumTest = BindingTest{
   306  	name: "GeneratedJsEntityWithIntEnumTest",
   307  	structs: []interface{}{
   308  		&EntityWithIntEnum{},
   309  	},
   310  	enums: []interface{}{
   311  		AllIntEnumValues,
   312  	},
   313  	exemptions:  nil,
   314  	shouldError: false,
   315  	TsGenerationOptionsTest: TsGenerationOptionsTest{
   316  		TsPrefix: "MY_PREFIX_",
   317  		TsSuffix: "_MY_SUFFIX",
   318  	},
   319  	want: `export namespace binding_test {
   320          	
   321          	export enum MY_PREFIX_IntEnum_MY_SUFFIX {
   322          	    Value1 = 0,
   323          	    Value2 = 1,
   324          	    Value3 = 2,
   325          	}
   326          	export class MY_PREFIX_EntityWithIntEnum_MY_SUFFIX {
   327          	    name: string;
   328          	    enum: MY_PREFIX_IntEnum_MY_SUFFIX;
   329          	
   330          	    static createFrom(source: any = {}) {
   331          	        return new MY_PREFIX_EntityWithIntEnum_MY_SUFFIX(source);
   332          	    }
   333          	
   334          	    constructor(source: any = {}) {
   335          	        if ('string' === typeof source) source = JSON.parse(source);
   336          	        this.name = source["name"];
   337          	        this.enum = source["enum"];
   338          	    }
   339          	}
   340          
   341          }
   342  `,
   343  }
   344  
   345  type StringEnum string
   346  
   347  const (
   348  	StringEnumValue1 StringEnum = "value1"
   349  	StringEnumValue2 StringEnum = "value2"
   350  	StringEnumValue3 StringEnum = "value3"
   351  )
   352  
   353  var AllStringEnumValues = []struct {
   354  	Value  StringEnum
   355  	TSName string
   356  }{
   357  	{StringEnumValue1, "Value1"},
   358  	{StringEnumValue2, "Value2"},
   359  	{StringEnumValue3, "Value3"},
   360  }
   361  
   362  type EntityWithStringEnum struct {
   363  	Name string     `json:"name"`
   364  	Enum StringEnum `json:"enum"`
   365  }
   366  
   367  func (e EntityWithStringEnum) Get() EntityWithStringEnum {
   368  	return e
   369  }
   370  
   371  var GeneratedJsEntityWithStringEnumTest = BindingTest{
   372  	name: "GeneratedJsEntityWithStringEnumTest",
   373  	structs: []interface{}{
   374  		&EntityWithStringEnum{},
   375  	},
   376  	enums: []interface{}{
   377  		AllStringEnumValues,
   378  	},
   379  	exemptions:  nil,
   380  	shouldError: false,
   381  	TsGenerationOptionsTest: TsGenerationOptionsTest{
   382  		TsPrefix: "MY_PREFIX_",
   383  		TsSuffix: "_MY_SUFFIX",
   384  	},
   385  	want: `export namespace binding_test {
   386          	
   387          	export enum MY_PREFIX_StringEnum_MY_SUFFIX {
   388          	    Value1 = "value1",
   389          	    Value2 = "value2",
   390          	    Value3 = "value3",
   391          	}
   392          	export class MY_PREFIX_EntityWithStringEnum_MY_SUFFIX {
   393          	    name: string;
   394          	    enum: MY_PREFIX_StringEnum_MY_SUFFIX;
   395          	
   396          	    static createFrom(source: any = {}) {
   397          	        return new MY_PREFIX_EntityWithStringEnum_MY_SUFFIX(source);
   398          	    }
   399          	
   400          	    constructor(source: any = {}) {
   401          	        if ('string' === typeof source) source = JSON.parse(source);
   402          	        this.name = source["name"];
   403          	        this.enum = source["enum"];
   404          	    }
   405          	}
   406          
   407          }
   408  `,
   409  }
   410  
   411  type EnumWithTsName string
   412  
   413  const (
   414  	EnumWithTsName1 EnumWithTsName = "value1"
   415  	EnumWithTsName2 EnumWithTsName = "value2"
   416  	EnumWithTsName3 EnumWithTsName = "value3"
   417  )
   418  
   419  var AllEnumWithTsNameValues = []EnumWithTsName{EnumWithTsName1, EnumWithTsName2, EnumWithTsName3}
   420  
   421  func (v EnumWithTsName) TSName() string {
   422  	switch v {
   423  	case EnumWithTsName1:
   424  		return "TsName1"
   425  	case EnumWithTsName2:
   426  		return "TsName2"
   427  	case EnumWithTsName3:
   428  		return "TsName3"
   429  	default:
   430  		return "???"
   431  	}
   432  }
   433  
   434  type EntityWithEnumTsName struct {
   435  	Name string         `json:"name"`
   436  	Enum EnumWithTsName `json:"enum"`
   437  }
   438  
   439  func (e EntityWithEnumTsName) Get() EntityWithEnumTsName {
   440  	return e
   441  }
   442  
   443  var GeneratedJsEntityWithEnumTsName = BindingTest{
   444  	name: "GeneratedJsEntityWithEnumTsName",
   445  	structs: []interface{}{
   446  		&EntityWithEnumTsName{},
   447  	},
   448  	enums: []interface{}{
   449  		AllEnumWithTsNameValues,
   450  	},
   451  	exemptions:  nil,
   452  	shouldError: false,
   453  	TsGenerationOptionsTest: TsGenerationOptionsTest{
   454  		TsPrefix: "MY_PREFIX_",
   455  		TsSuffix: "_MY_SUFFIX",
   456  	},
   457  	want: `export namespace binding_test {
   458          	
   459          	export enum MY_PREFIX_EnumWithTsName_MY_SUFFIX {
   460          	    TsName1 = "value1",
   461          	    TsName2 = "value2",
   462          	    TsName3 = "value3",
   463          	}
   464          	export class MY_PREFIX_EntityWithEnumTsName_MY_SUFFIX {
   465          	    name: string;
   466          	    enum: MY_PREFIX_EnumWithTsName_MY_SUFFIX;
   467          	
   468          	    static createFrom(source: any = {}) {
   469          	        return new MY_PREFIX_EntityWithEnumTsName_MY_SUFFIX(source);
   470          	    }
   471          	
   472          	    constructor(source: any = {}) {
   473          	        if ('string' === typeof source) source = JSON.parse(source);
   474          	        this.name = source["name"];
   475          	        this.enum = source["enum"];
   476          	    }
   477          	}
   478          
   479          }
   480  `,
   481  }
   482  
   483  var GeneratedJsEntityWithNestedStructInterfacesTest = BindingTest{
   484  	name: "GeneratedJsEntityWithNestedStructInterfacesTest",
   485  	structs: []interface{}{
   486  		&ParentEntity{},
   487  	},
   488  	exemptions:  nil,
   489  	shouldError: false,
   490  	TsGenerationOptionsTest: TsGenerationOptionsTest{
   491  		TsPrefix:     "MY_PREFIX_",
   492  		TsSuffix:     "_MY_SUFFIX",
   493  		TsOutputType: "interfaces",
   494  	},
   495  	want: `export namespace binding_test {
   496          	
   497          	export interface MY_PREFIX_ChildEntity_MY_SUFFIX {
   498          	    name: string;
   499          	    childProp: number;
   500          	}
   501          	export interface MY_PREFIX_ParentEntity_MY_SUFFIX {
   502          	    name: string;
   503          	    ref: MY_PREFIX_ChildEntity_MY_SUFFIX;
   504          	    parentProp: string;
   505          	}
   506          
   507          }
   508  `,
   509  }