You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add camelCase fieldNames and json:",omitempty" support
- Add ability to convert to JSII compatible camelCased fields
- Support valid `json:",omitempty"` tag (with test cases)
- Add `-local-pkg` flag for running tscriptify from local directory
References:
- tkrajina#73
- tkrajina#70
- https://github.com/rogpeppe/gohack
Copy file name to clipboardExpand all lines: README.md
+42-5Lines changed: 42 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,19 +50,22 @@ Command line options:
50
50
51
51
```
52
52
$ tscriptify --help
53
-
Usage of tscriptify:
54
53
-all-optional
55
-
Create interfaces with all fields optional
54
+
Set all fields optional
56
55
-backup string
57
56
Directory where backup files are saved
57
+
-camel-case
58
+
Convert all field names to camelCase
58
59
-import value
59
60
Typescript import for your custom type, repeat this option for each import needed
60
61
-interface
61
62
Create interfaces (not classes)
63
+
-local-pkg
64
+
Replace github.com/GoodNotes/typescriptify-golang-structs with the current directory in go.mod file. Useful for local development.
62
65
-package string
63
66
Path of the package with models
64
67
-readonly
65
-
Create interfaces with readonly fields
68
+
Set all fields readonly
66
69
-target string
67
70
Target typescript file
68
71
-verbose
@@ -71,7 +74,7 @@ Usage of tscriptify:
71
74
72
75
## Models and conversion
73
76
74
-
If the `Person` structs contain a reference to the `Address` struct, then you don't have to add `Address` explicitly. Only fields with a valid `json` tag will be converted to TypeScript models.
77
+
If the `Person` structs contain a reference to the `Address` struct, then you don't have to add `Address` explicitly. Any public field will be converted to TypeScript models.
75
78
76
79
Example input structs:
77
80
@@ -217,7 +220,7 @@ class Address {
217
220
218
221
The lines between `//[Address:]` and `//[end]` will be left intact after `ConvertToFile()`.
219
222
220
-
If your custom code contain methods, then just casting yout object to the target class (with `<Person> {...}`) won't work because the casted object won't contain your methods.
223
+
If your custom code contain methods, then just casting your object to the target class (with `<Person> {...}`) won't work because the casted object won't contain your methods.
221
224
222
225
In that case use the constructor:
223
226
@@ -459,6 +462,40 @@ Below snippet shows how to set the field `ObjectType` of the above `SecretDescri
459
462
AddTypeWithName(sdTypeTagged, "SecretDescriptor")
460
463
```
461
464
465
+
Conversion of field names to camelCase can be achieved using the `WithCamelCase` method:
466
+
467
+
```golang
468
+
typePersonalInfostruct {
469
+
Hobbies []string`json:",omitempty"`
470
+
PetNamestring`json:",omitempty"`
471
+
}
472
+
typeCloudKitDevstruct {
473
+
Namestring
474
+
PersonalInfoPersonalInfo
475
+
}
476
+
477
+
t:= typescriptify.New()
478
+
t.CreateInterface = true
479
+
t.ReadOnlyFields = true
480
+
t.CamelCaseFields = true
481
+
t.BackupDir = ""
482
+
483
+
t.AddType(reflect.TypeOf(CloudKitDev{}))
484
+
```
485
+
486
+
The resulting code will be:
487
+
488
+
```typescript
489
+
exportinterfacePersonalInfo {
490
+
readonly hobbies?:string[];
491
+
readonly petName?:string;
492
+
}
493
+
exportinterfaceCloudKitDev {
494
+
readonly name:string;
495
+
readonly personalInfo:PersonalInfo;
496
+
}
497
+
```
498
+
462
499
> Note: In both of these cases use the `AddTypeWithName` method to explicitly provide the name for the generated TypeScript interface.
463
500
> By design `reflect.Type` returns an empty string for non-defined types.
Copy file name to clipboardExpand all lines: makefile
+4-3Lines changed: 4 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,10 @@ test: node_modules lint
12
12
go run example/example.go
13
13
npx tsc browser_test/example_output.ts
14
14
# Make sure dommandline tool works:
15
-
go run tscriptify/main.go -package github.com/GoodNotes/typescriptify-golang-structs/example/example-models -verbose -target tmp_classes.ts example/example-models/example_models.go
16
-
go run tscriptify/main.go -package github.com/GoodNotes/typescriptify-golang-structs/example/example-models -verbose -target tmp_interfaces.ts -interface example/example-models/example_models.go
17
-
go run tscriptify/main.go -package=github.com/aws/secrets-store-csi-driver-provider-aws/provider -verbose -target=tmp_jsiiIntefaces.ts -interface -readonly -all-optional SecretDescriptor
15
+
go run tscriptify/main.go -package github.com/GoodNotes/typescriptify-golang-structs/example/example-models -local-pkg -verbose -target tmp_classes.ts example/example-models/example_models.go
16
+
go run tscriptify/main.go -package github.com/GoodNotes/typescriptify-golang-structs/example/example-models -local-pkg -verbose -target tmp_interfaces.ts -interface example/example-models/example_models.go
17
+
go run tscriptify/main.go -package github.com/GoodNotes/typescriptify-golang-structs/example/example-models -local-pkg -verbose -target tmp_jsiiInterfaces.ts -readonly -all-optional -camel-case -interface CloudKitDev
18
+
go run tscriptify/main.go -package=github.com/aws/secrets-store-csi-driver-provider-aws/provider -local-pkg -verbose -target=tmp_jsiiSecretDescriptor.ts -interface -readonly -all-optional SecretDescriptor
flag.BoolVar(&p.Readonly, "readonly", false, "Create interfaces with readonly fields")
85
-
flag.BoolVar(&p.AllOptional, "all-optional", false, "Create interfaces with all fields optional")
87
+
flag.BoolVar(&p.Readonly, "readonly", false, "Set all fields readonly")
88
+
flag.BoolVar(&p.AllOptional, "all-optional", false, "Set all fields optional")
89
+
flag.BoolVar(&p.CamelCase, "camel-case", false, "Convert all field names to camelCase")
86
90
flag.Var(&p.CustomImports, "import", "Typescript import for your custom type, repeat this option for each import needed")
91
+
flag.BoolVar(&p.LocalPkg, "local-pkg", false, "Replace github.com/GoodNotes/typescriptify-golang-structs with the current directory in go.mod file. Useful for local development.")
0 commit comments