Skip to content

Commit 5fd4c3a

Browse files
committed
interface cmdline flag
1 parent cded84e commit 5fd4c3a

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.vscode/
33
*.iml
44
tags
5+
tmp_*

example/models/example_models.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package models
2+
3+
type Address struct {
4+
// Used in html
5+
City string `json:"city"`
6+
Number float64 `json:"number"`
7+
Country string `json:"country,omitempty"`
8+
}
9+
10+
type PersonalInfo struct {
11+
Hobbies []string `json:"hobby"`
12+
PetName string `json:"pet_name"`
13+
}
14+
15+
type Person struct {
16+
Name string `json:"name"`
17+
PersonalInfo PersonalInfo `json:"personal_info"`
18+
Nicknames []string `json:"nicknames"`
19+
Addresses []Address `json:"addresses"`
20+
Address *Address `json:"address"`
21+
Metadata []byte `json:"metadata" ts_type:"{[key:string]:string}"`
22+
Friends []*Person `json:"friends"`
23+
}

makefile

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ test: lint
1111
go test ./...
1212
go run example/example.go
1313
tsc browser_test/example_output.ts
14+
# Make sure dommandline tool works:
15+
go run tscriptify/main.go -package github.com/tkrajina/typescriptify-golang-structs/example/models -target tmp_classes.ts example/models/example_models.go
16+
go run tscriptify/main.go -package github.com/tkrajina/typescriptify-golang-structs/example/models -target tmp_interfaces.ts -interface example/models/example_models.go
1417

1518
.PHONY: lint
1619
lint:

tscriptify/main.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
3737
func main() {
3838
t := typescriptify.New()
39+
t.CreateInterface = {{ .Interface }}
3940
{{ range $key, $value := .InitParams }} t.{{ $key }}={{ $value }}
4041
{{ end }}
4142
{{ range .Structs }} t.Add({{ . }}{})
@@ -55,14 +56,17 @@ type Params struct {
5556
Structs []string
5657
InitParams map[string]interface{}
5758
CustomImports arrayImports
59+
Interface bool
5860
}
5961

6062
func main() {
61-
var packagePath, target, backupDir string
63+
var p Params
64+
var backupDir string
6265
var customImports arrayImports
63-
flag.StringVar(&packagePath, "package", "", "Path of the package with models")
64-
flag.StringVar(&target, "target", "", "Target typescript file")
66+
flag.StringVar(&p.ModelsPackage, "package", "", "Path of the package with models")
67+
flag.StringVar(&p.TargetFile, "target", "", "Target typescript file")
6568
flag.StringVar(&backupDir, "backup", "", "Directory where backup files are saved")
69+
flag.BoolVar(&p.Interface, "interface", false, "Create interfaces (not classes)")
6670
flag.Var(&customImports, "import", "Typescript import for your custom type, repeat this option for each import needed")
6771
flag.Parse()
6872

@@ -80,16 +84,16 @@ func main() {
8084
}
8185
}
8286

83-
if len(packagePath) == 0 {
87+
if len(p.ModelsPackage) == 0 {
8488
fmt.Fprintln(os.Stderr, "No package given")
8589
os.Exit(1)
8690
}
87-
if len(target) == 0 {
91+
if len(p.TargetFile) == 0 {
8892
fmt.Fprintln(os.Stderr, "No target file")
8993
os.Exit(1)
9094
}
9195

92-
packageParts := strings.Split(packagePath, "/")
96+
packageParts := strings.Split(p.ModelsPackage, "/")
9397
pckg := packageParts[len(packageParts)-1]
9498

9599
t := template.Must(template.New("").Parse(TEMPLATE))
@@ -111,14 +115,9 @@ func main() {
111115
}
112116
}
113117

114-
p := Params{
115-
Structs: structsArr,
116-
ModelsPackage: packagePath,
117-
TargetFile: target,
118-
InitParams: map[string]interface{}{
119-
"BackupDir": fmt.Sprintf(`"%s"`, backupDir),
120-
},
121-
CustomImports: customImports,
118+
p.Structs = structsArr
119+
p.InitParams = map[string]interface{}{
120+
"BackupDir": fmt.Sprintf(`"%s"`, backupDir),
122121
}
123122
err = t.Execute(f, p)
124123
handleErr(err)

0 commit comments

Comments
 (0)