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
9 changes: 9 additions & 0 deletions example/gen/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"os"
"path/filepath"

"github.com/starius/api2"
"github.com/starius/api2/example"
"github.com/starius/api2/typegen"
)

func main() {
Expand All @@ -15,6 +19,11 @@ func main() {
&example.CustomType{},
&example.CustomType2{},
},
OnDone: func(options *api2.TypesGenConfig, parser *typegen.Parser, routes []api2.Route) {
_ = os.RemoveAll(filepath.Join(options.OutDir, "schema.ts"))
schemaFile, _ := os.OpenFile(filepath.Join(options.OutDir, "schema.ts"), os.O_WRONLY|os.O_CREATE, 0755)
_ = typegen.PrintJDT(parser, schemaFile)
Comment on lines +23 to +25

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check errors, please.

},
})
api2.GenerateOpenApiSpec(&api2.TypesGenConfig{
OutDir: "./openapi",
Expand Down
75 changes: 75 additions & 0 deletions example/ts-types/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// prettier-ignore
import * as t from './gen'
import type {JTDSchema} from 'libs/validator'

export const schema = {
ExampleCustomType: {
"metadata": {
"allOf": [
"ExampleUserSettings"
]
}
} as JTDSchema<t.example.CustomType>,
ExampleCustomType2: {
"metadata": {
"allOf": [
"ExampleUserSettings"
]
}
} as JTDSchema<t.example.CustomType2>,
ExampleDirection: {
"metadata": {
"enumType": "int",
"enumValues": [
1,
0,
2,
3
]
},
"type": "int32"
} as JTDSchema<t.example.Direction>,
ExampleEchoRequest: {
"properties": {
"dir": {
"ref": "ExampleDirection"
},
"items": {
"elements": {}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running go generate ./example results in modification in this file:

@@ -35,7 +35,9 @@ ExampleEchoRequest: {
       "ref": "ExampleDirection"
     },
     "items": {
-      "elements": {}
+      "elements": {
+        "ref": "ExampleCustomType2"
+      }
     },
     "maps": {
       "values": {

},
"maps": {
"values": {
"ref": "ExampleDirection"
}
},
"session": {
"type": "string"
},
"text": {
"type": "string"
}
}
} as JTDSchema<t.example.EchoRequest>,
ExampleEchoResponse: {
"properties": {
"text": {
"type": "string"
}
}
} as JTDSchema<t.example.EchoResponse>,
ExampleHelloRequest: {
"properties": {
"key": {
"type": "string"
}
}
} as JTDSchema<t.example.HelloRequest>,
ExampleHelloResponse: {
"properties": {
"session": {
"type": "string"
}
}
} as JTDSchema<t.example.HelloResponse>,
ExampleUserSettings: {} as JTDSchema<t.example.UserSettings>,
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.14
require (
github.com/getkin/kin-openapi v0.49.0
github.com/grpc-ecosystem/grpc-gateway v1.9.5
github.com/jsontypedef/json-typedef-go v0.0.0-20200503043955-4280071bd745
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/tools v0.0.0-20191114200427-caa0b0f7d508
google.golang.org/grpc v1.29.1
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/grpc-ecosystem/grpc-gateway v1.9.5 h1:UImYN5qQ8tuGpGE16ZmjvcTtTw24zw1QAp/SlnNrZhI=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/jsontypedef/json-typedef-go v0.0.0-20200503043955-4280071bd745 h1:g5e59oKweCDh4NCkwPVTCSvkA/xI4rEJ9gHpGwtc7gQ=
github.com/jsontypedef/json-typedef-go v0.0.0-20200503043955-4280071bd745/go.mod h1:vt/4bvsIJyrXdW084b66lVgihz1sOcKAC9l4tqJuty8=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -44,6 +46,12 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
13 changes: 11 additions & 2 deletions ts_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions typegen/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ func (this *EnumValue) Stringify() string {
}
return ""
}
func (this *EnumValue) RawValue() (interface{}, bool) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line before the func.

k := this.value.Kind()
switch k {
case reflect.String:
return this.value.String(), false
case reflect.Int:
_, hasToString := this.value.Type().MethodByName("String")
if hasToString {
return fmt.Sprintf("%v", this.value), false
} else {
return this.value.Int(), true
}
}
return "", false
}

func getTypedEnumValues(t reflect.Type) []EnumValue {
values, err := getEnumsFromAst(t.PkgPath(), t.String())
Expand Down
7 changes: 7 additions & 0 deletions typegen/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package typegen
import (
"path"
"reflect"
"strings"
)

type RawType struct {
Expand All @@ -15,6 +16,7 @@ type IType interface {
SetName(name, pkg string)
GetPackage() string
RefName() string
IdName() string
GetType() reflect.Type
}

Expand Down Expand Up @@ -75,6 +77,11 @@ func (this *BaseType) RefName() string {
return path.Base(pkg) + "." + this.Name
}

func (this *BaseType) IdName() string {
pkg := this.GetPackage()
return strings.Title(path.Base(pkg)) + this.Name
}

func (this *BaseType) GetType() reflect.Type {
return this.T
}
Expand Down
Loading