Skip to content

Commit 72a3fd8

Browse files
committed
Merge branch 'master' of github.com:abelkuruvilla/typescriptify-golang-structs into abelkuruvilla-master
2 parents 502a92f + a4c0f5e commit 72a3fd8

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

typescriptify/typescriptify.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
tsDocTag = "ts_doc"
1717
tsTransformTag = "ts_transform"
1818
tsType = "ts_type"
19+
jsonTag = "json"
1920
tsConvertValuesFunc = `convertValues(a: any, classs: any, asMap: boolean = false): any {
2021
if (!a) {
2122
return a;
@@ -86,6 +87,7 @@ type TypeScriptify struct {
8687
BackupDir string // If empty no backup
8788
DontExport bool
8889
CreateInterface bool
90+
CustomJsonTag string
8991
customImports []string
9092

9193
structTypes []StructType
@@ -219,6 +221,11 @@ func (t *TypeScriptify) WithSuffix(s string) *TypeScriptify {
219221
return t
220222
}
221223

224+
func (t *TypeScriptify) WithCustomJsonTag(tag string) *TypeScriptify {
225+
t.CustomJsonTag = tag
226+
return t
227+
}
228+
222229
func (t *TypeScriptify) Add(obj interface{}) *TypeScriptify {
223230
switch ty := obj.(type) {
224231
case StructType:
@@ -518,7 +525,11 @@ func (t *TypeScriptify) getFieldOptions(structType reflect.Type, field reflect.S
518525

519526
func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool) string {
520527
jsonFieldName := ""
521-
jsonTag := field.Tag.Get("json")
528+
tag := jsonTag
529+
if t.CustomJsonTag != "" {
530+
tag = t.CustomJsonTag
531+
}
532+
jsonTag := field.Tag.Get(tag)
522533
if len(jsonTag) > 0 {
523534
jsonTagParts := strings.Split(jsonTag, ",")
524535
if len(jsonTagParts) > 0 {

typescriptify/typescriptify_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515

1616
type Address struct {
1717
// Used in html
18-
Duration float64 `json:"duration"`
19-
Text1 string `json:"text,omitempty"`
18+
Duration float64 `json:"duration" custom:"durationCustom"`
19+
Text1 string `json:"text,omitempty" custom:"textCustom,omitempty"`
2020
// Ignored:
21-
Text2 string `json:",omitempty"`
22-
Text3 string `json:"-"`
21+
Text2 string `json:",omitempty" custom:",omitempty"`
22+
Text3 string `json:"-" custom:"-"`
2323
}
2424

2525
type Dummy struct {
@@ -1018,3 +1018,19 @@ func TestTypescriptifyComment(t *testing.T) {
10181018
}`
10191019
testConverter(t, converter, false, desiredResult, nil)
10201020
}
1021+
1022+
func TestTypescriptifyCustomJsonTag(t *testing.T) {
1023+
t.Parallel()
1024+
1025+
converter := New().WithCustomJsonTag("custom")
1026+
1027+
converter.AddType(reflect.TypeOf(Address{}))
1028+
converter.CreateConstructor = false
1029+
converter.BackupDir = ""
1030+
1031+
desiredResult := `export class Address {
1032+
durationCustom: number;
1033+
textCustom?: string;
1034+
}`
1035+
testConverter(t, converter, false, desiredResult, nil)
1036+
}

0 commit comments

Comments
 (0)