Skip to content

Commit b41cebc

Browse files
committed
feat: add schema
1 parent b605ee6 commit b41cebc

4 files changed

Lines changed: 199 additions & 18 deletions

File tree

config/config.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ import (
1111
)
1212

1313
type Target struct {
14-
Package string `yaml:"package"`
15-
Services map[string]string `yaml:"services"`
16-
TypeScriptModule string `yaml:"module"`
17-
Out string `yaml:"out"`
18-
GoRPC []string `yaml:"gorpc"`
19-
TSRPC []string `yaml:"tsrpc"`
20-
SkipTSRPCClient bool `yaml:"skipTSRPCClient"`
14+
// Go package name
15+
Package string `json:"package" yaml:"package"`
16+
// Map of default routes to service names
17+
Services map[string]string `json:"services" yaml:"services"`
18+
// TypeScript module name
19+
TypeScriptModule string `json:"module" yaml:"module"`
20+
// TypeScript output filename
21+
Out string `json:"out" yaml:"out"`
22+
// List of go rpc services to generate
23+
GoRPC []string `json:"gorpc" yaml:"gorpc"`
24+
// List of ts rpc services to generate
25+
TSRPC []string `json:"tsrpc" yaml:"tsrpc"`
26+
// Skip generating go rpc client
27+
SkipTSRPCClient bool `json:"skipTSRPCClient" yaml:"skipTSRPCClient"`
2128
}
2229

2330
func (t *Target) IsGoRPC(service string) bool {
@@ -42,25 +49,36 @@ func (t *Target) IsTSRPC(service string) bool {
4249
}
4350

4451
type Mapping struct {
45-
GoPackage string `yaml:"-"`
46-
Out string `yaml:"out"`
47-
Structs []string `yaml:"structs"`
48-
Scalars []string `yaml:"scalars"`
49-
TypeScriptModule string `yaml:"module"`
52+
// Internal go package name
53+
GoPackage string `json:"-" yaml:"-"`
54+
// TypeScript output filename
55+
Out string `json:"out" yaml:"out"`
56+
// List of go types to generate
57+
Structs []string `json:"structs" yaml:"structs"`
58+
// List of go types to generate
59+
Scalars []string `json:"scalars" yaml:"scalars"`
60+
// Optional TypeScript module name
61+
TypeScriptModule string `json:"module" yaml:"module"`
5062
}
5163

5264
type TypeScriptMappings map[string]*Mapping
5365

5466
type Namespace struct {
55-
Name string `yaml:"name"`
56-
Path string `yaml:"path"`
57-
ModFile *modfile.File `yaml:"-"`
67+
// Go module name
68+
Name string `json:"name" yaml:"name"`
69+
// Go module path
70+
Path string `json:"path" yaml:"path"`
71+
// Internally loaded mod file
72+
ModFile *modfile.File `json:"-" yaml:"-"`
5873
}
5974

6075
type Config struct {
61-
Module Namespace
62-
Targets map[string]*Target
63-
Mappings TypeScriptMappings
76+
// Go module settings
77+
Module Namespace `json:"module" yaml:"module"`
78+
// Map of target names to target settings
79+
Targets map[string]*Target `json:"targets" yaml:"targets"`
80+
// Map of go module names to TypeScript mapping settings
81+
Mappings TypeScriptMappings `json:"mappings" yaml:"mappings"`
6482
}
6583

6684
func LoadConfigFile(file string) (conf *Config, err error) {

gotsrpc.example.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# yaml-language-server: $schema=gotsrpc.schema.json
2+
module:
3+
name: github.com/foomo/gotsrpc/v2
4+
path: ./
5+
6+
targets:
7+
basic:
8+
services:
9+
/service: Service
10+
package: github.com/foomo/gotsrpc/v2/example/basic/service
11+
out: ./client/src/service-client.ts
12+
gorpc:
13+
- Service
14+
tsrpc:
15+
- Service
16+
17+
mappings:
18+
github.com/foomo/gotsrpc/v2/example/basic/service:
19+
out: ./client/src/service-vo.ts

gotsrpc.schema.json

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://github.com/foomo/gotsrpc/v2/config/config",
4+
"$ref": "#/$defs/Config",
5+
"$defs": {
6+
"Config": {
7+
"properties": {
8+
"module": {
9+
"$ref": "#/$defs/Namespace"
10+
},
11+
"targets": {
12+
"additionalProperties": {
13+
"$ref": "#/$defs/Target"
14+
},
15+
"type": "object"
16+
},
17+
"mappings": {
18+
"$ref": "#/$defs/TypeScriptMappings"
19+
}
20+
},
21+
"additionalProperties": false,
22+
"type": "object"
23+
},
24+
"Mapping": {
25+
"properties": {
26+
"out": {
27+
"type": "string"
28+
},
29+
"structs": {
30+
"items": {
31+
"type": "string"
32+
},
33+
"type": "array"
34+
},
35+
"scalars": {
36+
"items": {
37+
"type": "string"
38+
},
39+
"type": "array"
40+
},
41+
"module": {
42+
"type": "string"
43+
}
44+
},
45+
"additionalProperties": false,
46+
"type": "object"
47+
},
48+
"Namespace": {
49+
"properties": {
50+
"name": {
51+
"type": "string"
52+
},
53+
"path": {
54+
"type": "string"
55+
}
56+
},
57+
"additionalProperties": false,
58+
"type": "object"
59+
},
60+
"Target": {
61+
"properties": {
62+
"package": {
63+
"type": "string"
64+
},
65+
"services": {
66+
"additionalProperties": {
67+
"type": "string"
68+
},
69+
"type": "object"
70+
},
71+
"module": {
72+
"type": "string"
73+
},
74+
"out": {
75+
"type": "string"
76+
},
77+
"gorpc": {
78+
"items": {
79+
"type": "string"
80+
},
81+
"type": "array"
82+
},
83+
"tsrpc": {
84+
"items": {
85+
"type": "string"
86+
},
87+
"type": "array"
88+
},
89+
"skipTSRPCClient": {
90+
"type": "boolean"
91+
}
92+
},
93+
"additionalProperties": false,
94+
"type": "object"
95+
},
96+
"TypeScriptMappings": {
97+
"additionalProperties": {
98+
"$ref": "#/$defs/Mapping"
99+
},
100+
"type": "object"
101+
}
102+
}
103+
}

schema_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package gotsrpc_test
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"os"
7+
"path"
8+
"testing"
9+
10+
testingx "github.com/foomo/go/testing"
11+
tagx "github.com/foomo/go/testing/tag"
12+
"github.com/foomo/gotsrpc/v2/config"
13+
"github.com/invopop/jsonschema"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
func TestSchema(t *testing.T) {
19+
t.Parallel()
20+
testingx.Tags(t, tagx.Short)
21+
22+
cwd, err := os.Getwd()
23+
require.NoError(t, err)
24+
25+
reflector := new(jsonschema.Reflector)
26+
reflector.RequiredFromJSONSchemaTags = true
27+
require.NoError(t, reflector.AddGoComments("github.com/foomo/gotsrpc/v2/config", "./"))
28+
schema := reflector.Reflect(&config.Config{})
29+
actual, err := json.MarshalIndent(schema, "", " ")
30+
require.NoError(t, err)
31+
32+
filename := path.Join(cwd, "gotsrpc.schema.json")
33+
expected, err := os.ReadFile(filename)
34+
if !errors.Is(err, os.ErrNotExist) {
35+
require.NoError(t, err)
36+
}
37+
38+
if !assert.Equal(t, string(expected), string(actual)) {
39+
require.NoError(t, os.WriteFile(filename, actual, 0600))
40+
}
41+
}

0 commit comments

Comments
 (0)