Skip to content

Commit 0a75b34

Browse files
committed
Add custom code
1 parent 9762273 commit 0a75b34

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

typescriptify/typescriptify.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type TypeScriptify struct {
8989
CreateInterface bool
9090
CustomJsonTag string
9191
customImports []string
92+
customCodeBefore []string
93+
customCodeAfter []string
9294
silent bool
9395

9496
structTypes []StructType
@@ -352,6 +354,14 @@ func (t *TypeScriptify) Convert(customCode map[string]string) (string, error) {
352354
}
353355
}
354356

357+
if len(t.customCodeBefore) > 0 {
358+
result += "\n"
359+
for _, code := range t.customCodeBefore {
360+
result += "\n" + code + "\n"
361+
}
362+
result += "\n"
363+
}
364+
355365
for _, enumTyp := range t.enumTypes {
356366
elements := t.enums[enumTyp.Type]
357367
typeScriptCode, err := t.convertEnum(depth, enumTyp.Type, elements)
@@ -368,6 +378,15 @@ func (t *TypeScriptify) Convert(customCode map[string]string) (string, error) {
368378
}
369379
result += "\n" + strings.Trim(typeScriptCode, " "+t.Indent+"\r\n")
370380
}
381+
382+
if len(t.customCodeAfter) > 0 {
383+
result += "\n"
384+
for _, code := range t.customCodeAfter {
385+
result += "\n" + code + "\n"
386+
}
387+
result += "\n"
388+
}
389+
371390
return result, nil
372391
}
373392

@@ -747,6 +766,14 @@ func (t *TypeScriptify) AddImport(i string) {
747766
t.customImports = append(t.customImports, i)
748767
}
749768

769+
func (t *TypeScriptify) WithCustomCodeBefore(i string) {
770+
t.customCodeBefore = append(t.customCodeBefore, i)
771+
}
772+
773+
func (t *TypeScriptify) WithCustomCodeAfter(i string) {
774+
t.customCodeAfter = append(t.customCodeAfter, i)
775+
}
776+
750777
type typeScriptClassBuilder struct {
751778
types map[reflect.Kind]string
752779
indent string

typescriptify/typescriptify_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,48 @@ export class Person {
6767
testConverter(t, converter, false, desiredResult, nil)
6868
}
6969

70+
func TestTypescriptifyWithCustomCode(t *testing.T) {
71+
t.Parallel()
72+
converter := New()
73+
74+
converter.WithCustomCodeBefore(`"a"
75+
"b"
76+
"b"`)
77+
converter.WithCustomCodeAfter(`"d"
78+
"e"
79+
"f"`)
80+
converter.AddType(reflect.TypeOf(Person{}))
81+
converter.CreateConstructor = false
82+
converter.BackupDir = ""
83+
84+
desiredResult := `"a"
85+
"b"
86+
"b"
87+
88+
89+
export class Dummy {
90+
something: string;
91+
}
92+
export class Address {
93+
duration: number;
94+
text?: string;
95+
}
96+
export class Person {
97+
name: string;
98+
nicknames: string[];
99+
addresses: Address[];
100+
address?: Address;
101+
metadata: {[key:string]:string};
102+
friends: Person[];
103+
a: Dummy;
104+
}
105+
106+
"d"
107+
"e"
108+
"f"`
109+
testConverter(t, converter, false, desiredResult, nil)
110+
}
111+
70112
func TestTypescriptifyWithCustomImports(t *testing.T) {
71113
t.Parallel()
72114
converter := New()

0 commit comments

Comments
 (0)