diff --git a/typescriptify/typescriptify.go b/typescriptify/typescriptify.go index fbb71b2..41048c7 100644 --- a/typescriptify/typescriptify.go +++ b/typescriptify/typescriptify.go @@ -539,7 +539,7 @@ func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool) break } } - if !ignored && isPtr || hasOmitEmpty { + if !ignored && hasOmitEmpty { jsonFieldName = fmt.Sprintf("%s?", jsonFieldName) } } else if /*field.IsExported()*/ field.PkgPath == "" { diff --git a/typescriptify/typescriptify_test.go b/typescriptify/typescriptify_test.go index 75adde9..ec57a4a 100644 --- a/typescriptify/typescriptify_test.go +++ b/typescriptify/typescriptify_test.go @@ -35,6 +35,7 @@ type Person struct { Nicknames []string `json:"nicknames"` Addresses []Address `json:"addresses"` Address *Address `json:"address"` + Address2 *Address `json:"address2,omitempty"` Metadata string `json:"metadata" ts_type:"{[key:string]:string}" ts_transform:"JSON.parse(__VALUE__ || \"{}\")"` Friends []*Person `json:"friends"` Dummy Dummy `json:"a"` @@ -59,7 +60,8 @@ export class Person { name: string; nicknames: string[]; addresses: Address[]; - address?: Address; + address: Address; + address2?: Address; metadata: {[key:string]:string}; friends: Person[]; a: Dummy; @@ -90,7 +92,8 @@ export class Person { name: string; nicknames: string[]; addresses: Address[]; - address?: Address; + address: Address; + address2?: Address; metadata: {[key:string]:string}; friends: Person[]; a: Dummy; @@ -119,7 +122,8 @@ class Person { name: string; nicknames: string[]; addresses: Address[]; - address?: Address; + address: Address; + address2?: Address; metadata: {[key:string]:string}; friends: Person[]; a: Dummy; @@ -148,7 +152,8 @@ interface Person { name: string; nicknames: string[]; addresses: Address[]; - address?: Address; + address: Address; + address2?: Address; metadata: {[key:string]:string}; friends: Person[]; a: Dummy; @@ -176,7 +181,8 @@ export class Person { name: string; nicknames: string[]; addresses: Address[]; - address?: Address; + address: Address; + address2?: Address; metadata: {[key:string]:string}; friends: Person[]; a: Dummy; @@ -217,7 +223,8 @@ class test_Person_test { name: string; nicknames: string[]; addresses: test_Address_test[]; - address?: test_Address_test; + address: test_Address_test; + address2?: test_Address_test; metadata: {[key:string]:string}; friends: test_Person_test[]; a: test_Dummy_test; @@ -228,6 +235,7 @@ class test_Person_test { this.nicknames = source["nicknames"]; this.addresses = this.convertValues(source["addresses"], test_Address_test); this.address = this.convertValues(source["address"], test_Address_test); + this.address2 = this.convertValues(source["address2"], test_Address_test); this.metadata = JSON.parse(source["metadata"] || "{}"); this.friends = this.convertValues(source["friends"], test_Person_test); this.a = this.convertValues(source["a"], test_Dummy_test); @@ -752,8 +760,9 @@ export class Person { name: string; nicknames: string[]; addresses: Address[]; - address?: Address; - metadata: {[key:string]:string}; + address: Address; + address2?: Address; +metadata: {[key:string]:string}; friends: Person[]; a: Dummy; @@ -763,6 +772,7 @@ export class Person { this.nicknames = source["nicknames"]; this.addresses = this.convertValues(source["addresses"], Address); this.address = this.convertValues(source["address"], Address); + this.address2 = this.convertValues(source["address2"], Address); this.metadata = JSON.parse(source["metadata"] || "{}"); this.friends = this.convertValues(source["friends"], Person); this.a = this.convertValues(source["a"], Dummy); @@ -776,7 +786,7 @@ export class Person { type WithMap struct { Map map[string]int `json:"simpleMap"` MapObjects map[string]Address `json:"mapObjects"` - PtrMap *map[string]Address `json:"ptrMapObjects"` + PtrMap *map[string]Address `json:"ptrMapObjects,omitempty"` } func TestMaps(t *testing.T) { @@ -836,7 +846,8 @@ func TestMaps(t *testing.T) { func TestPTR(t *testing.T) { t.Parallel() type Person struct { - Name *string `json:"name"` + Name *string `json:"name"` + OptionalName *string `json:"optionalName,omitempty"` } converter := New() @@ -845,7 +856,8 @@ func TestPTR(t *testing.T) { converter.Add(Person{}) desiredResult := `export class Person { - name?: string; + name: string; + optionalName?: string; }` testConverter(t, converter, true, desiredResult, nil) } @@ -896,7 +908,7 @@ const converter = new Converter(); class Address { street: string; number: number; - + constructor(a: any) { this.street = a["street"]; this.number = a["number"];