mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
feat: add schema
This commit is contained in:
parent
b605ee6f47
commit
b41cebcea3
@ -11,13 +11,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Target struct {
|
type Target struct {
|
||||||
Package string `yaml:"package"`
|
// Go package name
|
||||||
Services map[string]string `yaml:"services"`
|
Package string `json:"package" yaml:"package"`
|
||||||
TypeScriptModule string `yaml:"module"`
|
// Map of default routes to service names
|
||||||
Out string `yaml:"out"`
|
Services map[string]string `json:"services" yaml:"services"`
|
||||||
GoRPC []string `yaml:"gorpc"`
|
// TypeScript module name
|
||||||
TSRPC []string `yaml:"tsrpc"`
|
TypeScriptModule string `json:"module" yaml:"module"`
|
||||||
SkipTSRPCClient bool `yaml:"skipTSRPCClient"`
|
// TypeScript output filename
|
||||||
|
Out string `json:"out" yaml:"out"`
|
||||||
|
// List of go rpc services to generate
|
||||||
|
GoRPC []string `json:"gorpc" yaml:"gorpc"`
|
||||||
|
// List of ts rpc services to generate
|
||||||
|
TSRPC []string `json:"tsrpc" yaml:"tsrpc"`
|
||||||
|
// Skip generating go rpc client
|
||||||
|
SkipTSRPCClient bool `json:"skipTSRPCClient" yaml:"skipTSRPCClient"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Target) IsGoRPC(service string) bool {
|
func (t *Target) IsGoRPC(service string) bool {
|
||||||
@ -42,25 +49,36 @@ func (t *Target) IsTSRPC(service string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Mapping struct {
|
type Mapping struct {
|
||||||
GoPackage string `yaml:"-"`
|
// Internal go package name
|
||||||
Out string `yaml:"out"`
|
GoPackage string `json:"-" yaml:"-"`
|
||||||
Structs []string `yaml:"structs"`
|
// TypeScript output filename
|
||||||
Scalars []string `yaml:"scalars"`
|
Out string `json:"out" yaml:"out"`
|
||||||
TypeScriptModule string `yaml:"module"`
|
// List of go types to generate
|
||||||
|
Structs []string `json:"structs" yaml:"structs"`
|
||||||
|
// List of go types to generate
|
||||||
|
Scalars []string `json:"scalars" yaml:"scalars"`
|
||||||
|
// Optional TypeScript module name
|
||||||
|
TypeScriptModule string `json:"module" yaml:"module"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TypeScriptMappings map[string]*Mapping
|
type TypeScriptMappings map[string]*Mapping
|
||||||
|
|
||||||
type Namespace struct {
|
type Namespace struct {
|
||||||
Name string `yaml:"name"`
|
// Go module name
|
||||||
Path string `yaml:"path"`
|
Name string `json:"name" yaml:"name"`
|
||||||
ModFile *modfile.File `yaml:"-"`
|
// Go module path
|
||||||
|
Path string `json:"path" yaml:"path"`
|
||||||
|
// Internally loaded mod file
|
||||||
|
ModFile *modfile.File `json:"-" yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Module Namespace
|
// Go module settings
|
||||||
Targets map[string]*Target
|
Module Namespace `json:"module" yaml:"module"`
|
||||||
Mappings TypeScriptMappings
|
// Map of target names to target settings
|
||||||
|
Targets map[string]*Target `json:"targets" yaml:"targets"`
|
||||||
|
// Map of go module names to TypeScript mapping settings
|
||||||
|
Mappings TypeScriptMappings `json:"mappings" yaml:"mappings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfigFile(file string) (conf *Config, err error) {
|
func LoadConfigFile(file string) (conf *Config, err error) {
|
||||||
|
|||||||
19
gotsrpc.example.yaml
Normal file
19
gotsrpc.example.yaml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# yaml-language-server: $schema=gotsrpc.schema.json
|
||||||
|
module:
|
||||||
|
name: github.com/foomo/gotsrpc/v2
|
||||||
|
path: ./
|
||||||
|
|
||||||
|
targets:
|
||||||
|
basic:
|
||||||
|
services:
|
||||||
|
/service: Service
|
||||||
|
package: github.com/foomo/gotsrpc/v2/example/basic/service
|
||||||
|
out: ./client/src/service-client.ts
|
||||||
|
gorpc:
|
||||||
|
- Service
|
||||||
|
tsrpc:
|
||||||
|
- Service
|
||||||
|
|
||||||
|
mappings:
|
||||||
|
github.com/foomo/gotsrpc/v2/example/basic/service:
|
||||||
|
out: ./client/src/service-vo.ts
|
||||||
103
gotsrpc.schema.json
Normal file
103
gotsrpc.schema.json
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
|
"$id": "https://github.com/foomo/gotsrpc/v2/config/config",
|
||||||
|
"$ref": "#/$defs/Config",
|
||||||
|
"$defs": {
|
||||||
|
"Config": {
|
||||||
|
"properties": {
|
||||||
|
"module": {
|
||||||
|
"$ref": "#/$defs/Namespace"
|
||||||
|
},
|
||||||
|
"targets": {
|
||||||
|
"additionalProperties": {
|
||||||
|
"$ref": "#/$defs/Target"
|
||||||
|
},
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"mappings": {
|
||||||
|
"$ref": "#/$defs/TypeScriptMappings"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"Mapping": {
|
||||||
|
"properties": {
|
||||||
|
"out": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"structs": {
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"scalars": {
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"module": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"Namespace": {
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"path": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"Target": {
|
||||||
|
"properties": {
|
||||||
|
"package": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"services": {
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"module": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"out": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"gorpc": {
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"tsrpc": {
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"skipTSRPCClient": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"TypeScriptMappings": {
|
||||||
|
"additionalProperties": {
|
||||||
|
"$ref": "#/$defs/Mapping"
|
||||||
|
},
|
||||||
|
"type": "object"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
schema_test.go
Normal file
41
schema_test.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package gotsrpc_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
testingx "github.com/foomo/go/testing"
|
||||||
|
tagx "github.com/foomo/go/testing/tag"
|
||||||
|
"github.com/foomo/gotsrpc/v2/config"
|
||||||
|
"github.com/invopop/jsonschema"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSchema(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
testingx.Tags(t, tagx.Short)
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
reflector := new(jsonschema.Reflector)
|
||||||
|
reflector.RequiredFromJSONSchemaTags = true
|
||||||
|
require.NoError(t, reflector.AddGoComments("github.com/foomo/gotsrpc/v2/config", "./"))
|
||||||
|
schema := reflector.Reflect(&config.Config{})
|
||||||
|
actual, err := json.MarshalIndent(schema, "", " ")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
filename := path.Join(cwd, "gotsrpc.schema.json")
|
||||||
|
expected, err := os.ReadFile(filename)
|
||||||
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.Equal(t, string(expected), string(actual)) {
|
||||||
|
require.NoError(t, os.WriteFile(filename, actual, 0600))
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user