Skip to content

Commit 74c737d

Browse files
committed
Make ptr fields follow omitempty directive
1 parent 90155a4 commit 74c737d

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
@@ -36,6 +36,7 @@ type Person struct {
3636
Nicknames []string `json:"nicknames"`
3737
Addresses []Address `json:"addresses"`
3838
Address *Address `json:"address"`
39+
Address2 *Address `json:"address2,omitempty"`
3940
Metadata string `json:"metadata" ts_type:"{[key:string]:string}" ts_transform:"JSON.parse(__VALUE__ || \"{}\")"`
4041
Friends []*Person `json:"friends"`
4142
Dummy Dummy `json:"a"`
@@ -60,7 +61,8 @@ export class Person {
6061
name: string;
6162
nicknames: string[];
6263
addresses: Address[];
63-
address?: Address;
64+
address: Address;
65+
address2?: Address;
6466
metadata: {[key:string]:string};
6567
friends: Person[];
6668
a: Dummy;
@@ -91,7 +93,8 @@ export class Person {
9193
name: string;
9294
nicknames: string[];
9395
addresses: Address[];
94-
address?: Address;
96+
address: Address;
97+
address2?: Address;
9598
metadata: {[key:string]:string};
9699
friends: Person[];
97100
a: Dummy;
@@ -120,7 +123,8 @@ class Person {
120123
name: string;
121124
nicknames: string[];
122125
addresses: Address[];
123-
address?: Address;
126+
address: Address;
127+
address2?: Address;
124128
metadata: {[key:string]:string};
125129
friends: Person[];
126130
a: Dummy;
@@ -149,7 +153,8 @@ interface Person {
149153
name: string;
150154
nicknames: string[];
151155
addresses: Address[];
152-
address?: Address;
156+
address: Address;
157+
address2?: Address;
153158
metadata: {[key:string]:string};
154159
friends: Person[];
155160
a: Dummy;
@@ -177,7 +182,8 @@ export class Person {
177182
name: string;
178183
nicknames: string[];
179184
addresses: Address[];
180-
address?: Address;
185+
address: Address;
186+
address2?: Address;
181187
metadata: {[key:string]:string};
182188
friends: Person[];
183189
a: Dummy;
@@ -218,7 +224,8 @@ class test_Person_test {
218224
name: string;
219225
nicknames: string[];
220226
addresses: test_Address_test[];
221-
address?: test_Address_test;
227+
address: test_Address_test;
228+
address2?: test_Address_test;
222229
metadata: {[key:string]:string};
223230
friends: test_Person_test[];
224231
a: test_Dummy_test;
@@ -229,6 +236,7 @@ class test_Person_test {
229236
this.nicknames = source["nicknames"];
230237
this.addresses = this.convertValues(source["addresses"], test_Address_test);
231238
this.address = this.convertValues(source["address"], test_Address_test);
239+
this.address2 = this.convertValues(source["address2"], test_Address_test);
232240
this.metadata = JSON.parse(source["metadata"] || "{}");
233241
this.friends = this.convertValues(source["friends"], test_Person_test);
234242
this.a = this.convertValues(source["a"], test_Dummy_test);
@@ -749,8 +757,9 @@ export class Person {
749757
name: string;
750758
nicknames: string[];
751759
addresses: Address[];
752-
address?: Address;
753-
metadata: {[key:string]:string};
760+
address: Address;
761+
address2?: Address;
762+
metadata: {[key:string]:string};
754763
friends: Person[];
755764
a: Dummy;
756765
@@ -760,6 +769,7 @@ export class Person {
760769
this.nicknames = source["nicknames"];
761770
this.addresses = this.convertValues(source["addresses"], Address);
762771
this.address = this.convertValues(source["address"], Address);
772+
this.address2 = this.convertValues(source["address2"], Address);
763773
this.metadata = JSON.parse(source["metadata"] || "{}");
764774
this.friends = this.convertValues(source["friends"], Person);
765775
this.a = this.convertValues(source["a"], Dummy);
@@ -773,7 +783,7 @@ export class Person {
773783
type WithMap struct {
774784
Map map[string]int `json:"simpleMap"`
775785
MapObjects map[string]Address `json:"mapObjects"`
776-
PtrMap *map[string]Address `json:"ptrMapObjects"`
786+
PtrMap *map[string]Address `json:"ptrMapObjects,omitempty"`
777787
}
778788

779789
func TestMaps(t *testing.T) {
@@ -833,7 +843,8 @@ func TestMaps(t *testing.T) {
833843
func TestPTR(t *testing.T) {
834844
t.Parallel()
835845
type Person struct {
836-
Name *string `json:"name"`
846+
Name *string `json:"name"`
847+
OptionalName *string `json:"optionalName,omitempty"`
837848
}
838849

839850
converter := New()
@@ -842,7 +853,8 @@ func TestPTR(t *testing.T) {
842853
converter.Add(Person{})
843854

844855
desiredResult := `export class Person {
845-
name?: string;
856+
name: string;
857+
optionalName?: string;
846858
}`
847859
testConverter(t, converter, true, desiredResult, nil)
848860
}
@@ -893,7 +905,7 @@ const converter = new Converter();
893905
class Address {
894906
street: string;
895907
number: number;
896-
908+
897909
constructor(a: any) {
898910
this.street = a["street"];
899911
this.number = a["number"];

0 commit comments

Comments
 (0)