Skip to content

Commit 366c8b1

Browse files
committed
Make ptr fields follow omitempty directive
1 parent ac0debc commit 366c8b1

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

typescriptify/typescriptify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool)
539539
break
540540
}
541541
}
542-
if !ignored && isPtr || hasOmitEmpty {
542+
if !ignored && hasOmitEmpty {
543543
jsonFieldName = fmt.Sprintf("%s?", jsonFieldName)
544544
}
545545
} else if /*field.IsExported()*/ field.PkgPath == "" {

typescriptify/typescriptify_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Person struct {
3535
Nicknames []string `json:"nicknames"`
3636
Addresses []Address `json:"addresses"`
3737
Address *Address `json:"address"`
38+
Address2 *Address `json:"address2,omitempty"`
3839
Metadata string `json:"metadata" ts_type:"{[key:string]:string}" ts_transform:"JSON.parse(__VALUE__ || \"{}\")"`
3940
Friends []*Person `json:"friends"`
4041
Dummy Dummy `json:"a"`
@@ -59,7 +60,8 @@ export class Person {
5960
name: string;
6061
nicknames: string[];
6162
addresses: Address[];
62-
address?: Address;
63+
address: Address;
64+
address2?: Address;
6365
metadata: {[key:string]:string};
6466
friends: Person[];
6567
a: Dummy;
@@ -90,7 +92,8 @@ export class Person {
9092
name: string;
9193
nicknames: string[];
9294
addresses: Address[];
93-
address?: Address;
95+
address: Address;
96+
address2?: Address;
9497
metadata: {[key:string]:string};
9598
friends: Person[];
9699
a: Dummy;
@@ -119,7 +122,8 @@ class Person {
119122
name: string;
120123
nicknames: string[];
121124
addresses: Address[];
122-
address?: Address;
125+
address: Address;
126+
address2?: Address;
123127
metadata: {[key:string]:string};
124128
friends: Person[];
125129
a: Dummy;
@@ -148,7 +152,8 @@ interface Person {
148152
name: string;
149153
nicknames: string[];
150154
addresses: Address[];
151-
address?: Address;
155+
address: Address;
156+
address2?: Address;
152157
metadata: {[key:string]:string};
153158
friends: Person[];
154159
a: Dummy;
@@ -176,7 +181,8 @@ export class Person {
176181
name: string;
177182
nicknames: string[];
178183
addresses: Address[];
179-
address?: Address;
184+
address: Address;
185+
address2?: Address;
180186
metadata: {[key:string]:string};
181187
friends: Person[];
182188
a: Dummy;
@@ -217,7 +223,8 @@ class test_Person_test {
217223
name: string;
218224
nicknames: string[];
219225
addresses: test_Address_test[];
220-
address?: test_Address_test;
226+
address: test_Address_test;
227+
address2?: test_Address_test;
221228
metadata: {[key:string]:string};
222229
friends: test_Person_test[];
223230
a: test_Dummy_test;
@@ -228,6 +235,7 @@ class test_Person_test {
228235
this.nicknames = source["nicknames"];
229236
this.addresses = this.convertValues(source["addresses"], test_Address_test);
230237
this.address = this.convertValues(source["address"], test_Address_test);
238+
this.address2 = this.convertValues(source["address2"], test_Address_test);
231239
this.metadata = JSON.parse(source["metadata"] || "{}");
232240
this.friends = this.convertValues(source["friends"], test_Person_test);
233241
this.a = this.convertValues(source["a"], test_Dummy_test);
@@ -752,8 +760,9 @@ export class Person {
752760
name: string;
753761
nicknames: string[];
754762
addresses: Address[];
755-
address?: Address;
756-
metadata: {[key:string]:string};
763+
address: Address;
764+
address2?: Address;
765+
metadata: {[key:string]:string};
757766
friends: Person[];
758767
a: Dummy;
759768
@@ -763,6 +772,7 @@ export class Person {
763772
this.nicknames = source["nicknames"];
764773
this.addresses = this.convertValues(source["addresses"], Address);
765774
this.address = this.convertValues(source["address"], Address);
775+
this.address2 = this.convertValues(source["address2"], Address);
766776
this.metadata = JSON.parse(source["metadata"] || "{}");
767777
this.friends = this.convertValues(source["friends"], Person);
768778
this.a = this.convertValues(source["a"], Dummy);
@@ -776,7 +786,7 @@ export class Person {
776786
type WithMap struct {
777787
Map map[string]int `json:"simpleMap"`
778788
MapObjects map[string]Address `json:"mapObjects"`
779-
PtrMap *map[string]Address `json:"ptrMapObjects"`
789+
PtrMap *map[string]Address `json:"ptrMapObjects,omitempty"`
780790
}
781791

782792
func TestMaps(t *testing.T) {
@@ -836,7 +846,8 @@ func TestMaps(t *testing.T) {
836846
func TestPTR(t *testing.T) {
837847
t.Parallel()
838848
type Person struct {
839-
Name *string `json:"name"`
849+
Name *string `json:"name"`
850+
OptionalName *string `json:"optionalName,omitempty"`
840851
}
841852

842853
converter := New()
@@ -845,7 +856,8 @@ func TestPTR(t *testing.T) {
845856
converter.Add(Person{})
846857

847858
desiredResult := `export class Person {
848-
name?: string;
859+
name: string;
860+
optionalName?: string;
849861
}`
850862
testConverter(t, converter, true, desiredResult, nil)
851863
}
@@ -896,7 +908,7 @@ const converter = new Converter();
896908
class Address {
897909
street: string;
898910
number: number;
899-
911+
900912
constructor(a: any) {
901913
this.street = a["street"];
902914
this.number = a["number"];

0 commit comments

Comments
 (0)