Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion typescriptify/typescriptify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
36 changes: 24 additions & 12 deletions typescriptify/typescriptify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down Expand Up @@ -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"];
Expand Down