Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 790e240

Browse files
authored
feat: Package JSON Schema with plugins (#1494)
Add the JSON schema if exists during plugin `package` command ---
1 parent 80eac6d commit 790e240

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

plugin/plugin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ func (p *Plugin) Version() string {
126126
return p.version
127127
}
128128

129+
func (p *Plugin) JSONSchema() string {
130+
return p.schema
131+
}
132+
129133
func (p *Plugin) Meta() Meta {
130134
return Meta{
131135
Team: p.team,

serve/package.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ func (s *PluginServe) writePackageJSON(dir, version, message string, targets []T
234234
return os.WriteFile(outputPath, buffer.Bytes(), 0644)
235235
}
236236

237+
func (s *PluginServe) writeSpecJSONSchema(dir string) error {
238+
if s.plugin.JSONSchema() == "" {
239+
return nil
240+
}
241+
242+
return os.WriteFile(filepath.Join(dir, "spec_json_schema.json"), []byte(s.plugin.JSONSchema()), 0644)
243+
}
244+
237245
func (*PluginServe) copyDocs(distPath, docsPath string) error {
238246
err := os.MkdirAll(filepath.Join(distPath, "docs"), 0755)
239247
if err != nil {
@@ -427,6 +435,9 @@ func (s *PluginServe) newCmdPluginPackage() *cobra.Command {
427435
if err := s.copyDocs(distPath, docsPath); err != nil {
428436
return fmt.Errorf("failed to copy docs: %w", err)
429437
}
438+
if err := s.writeSpecJSONSchema(distPath); err != nil {
439+
return fmt.Errorf("failed to write spec json schema: %w", err)
440+
}
430441
return nil
431442
},
432443
}

serve/package_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import (
2121
//go:embed testdata/memdbtables.json
2222
var memDBPackageJSON string
2323

24+
//go:embed testdata/source_spec_schema.json
25+
var sourceSpecSchema string
26+
27+
//go:embed testdata/destination_spec_schema.json
28+
var destinationSpecSchema string
29+
2430
func TestPluginPackage_Source(t *testing.T) {
2531
_, filename, _, ok := runtime.Caller(0)
2632
if !ok {
@@ -39,6 +45,7 @@ func TestPluginPackage_Source(t *testing.T) {
3945
}),
4046
plugin.WithKind("source"),
4147
plugin.WithTeam("test-team"),
48+
plugin.WithJSONSchema(sourceSpecSchema),
4249
)
4350
msg := `Test message
4451
with multiple lines and **markdown**`
@@ -78,6 +85,7 @@ with multiple lines and **markdown**`
7885
"package.json",
7986
"plugin-test-plugin-v1.2.3-linux-amd64.zip",
8087
"plugin-test-plugin-v1.2.3-windows-amd64.zip",
88+
"spec_json_schema.json",
8189
"tables.json",
8290
}
8391
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
@@ -112,6 +120,7 @@ with multiple lines and **markdown**`
112120
}
113121
checkDocs(t, filepath.Join(distDir, "docs"), expectDocs)
114122
checkTables(t, distDir)
123+
checkFileContent(t, filepath.Join(distDir, "spec_json_schema.json"), sourceSpecSchema)
115124
})
116125
}
117126
}
@@ -134,6 +143,7 @@ func TestPluginPackage_Destination(t *testing.T) {
134143
}),
135144
plugin.WithKind("destination"),
136145
plugin.WithTeam("test-team"),
146+
plugin.WithJSONSchema(destinationSpecSchema),
137147
)
138148
msg := `Test message
139149
with multiple lines and **markdown**`
@@ -173,6 +183,7 @@ with multiple lines and **markdown**`
173183
"package.json",
174184
"plugin-test-plugin-v1.2.3-darwin-amd64.zip",
175185
"plugin-test-plugin-v1.2.3-windows-amd64.zip",
186+
"spec_json_schema.json",
176187
}
177188
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
178189
t.Fatalf("unexpected files in dist directory (-want +got):\n%s", diff)
@@ -205,6 +216,7 @@ with multiple lines and **markdown**`
205216
"overview.md",
206217
}
207218
checkDocs(t, filepath.Join(distDir, "docs"), expectDocs)
219+
checkFileContent(t, filepath.Join(distDir, "spec_json_schema.json"), destinationSpecSchema)
208220
})
209221
}
210222
}
@@ -327,6 +339,21 @@ func checkPackageJSONContents(t *testing.T, filename string, expect PackageJSON)
327339
}
328340
}
329341

342+
func checkFileContent(t *testing.T, filename string, expect string) {
343+
f, err := os.Open(filename)
344+
if err != nil {
345+
t.Fatalf("failed to open %s: %v", filename, err)
346+
}
347+
defer f.Close()
348+
b, err := io.ReadAll(f)
349+
if err != nil {
350+
t.Fatalf("failed to read %s: %v", filename, err)
351+
}
352+
if diff := cmp.Diff(expect, string(b)); diff != "" {
353+
t.Fatalf("%s contents mismatch (-want +got):\n%s", filename, diff)
354+
}
355+
}
356+
330357
func fileNames(files []os.DirEntry) []string {
331358
names := make([]string, 0, len(files))
332359
for _, file := range files {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$ref": "#/$defs/Spec",
4+
"$defs": {
5+
"Spec": {
6+
"properties": {
7+
"connection_string": {
8+
"type": "string"
9+
}
10+
},
11+
"additionalProperties": false,
12+
"type": "object"
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$ref": "#/$defs/Spec",
4+
"$defs": {
5+
"Spec": {
6+
"properties": {
7+
"concurrency": {
8+
"type": "integer"
9+
}
10+
},
11+
"additionalProperties": false,
12+
"type": "object"
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)