forked from redpanda-data/benthos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/redpanda-data/benthos/v4/internal/bundle" | ||
"github.com/redpanda-data/benthos/v4/internal/docs" | ||
) | ||
|
||
func compSpecsToDefinition(specs []docs.ComponentSpec, typeFields map[string]docs.FieldSpec) map[string]any { | ||
generalFields := map[string]any{} | ||
for k, v := range typeFields { | ||
generalFields[k] = v.JSONSchema() | ||
} | ||
|
||
var componentDefs []any | ||
for _, s := range specs { | ||
componentDefs = append(componentDefs, map[string]any{ | ||
"type": "object", | ||
"properties": map[string]any{ | ||
s.Name: s.Config.JSONSchema(), | ||
}, | ||
}) | ||
} | ||
|
||
return map[string]any{ | ||
"allOf": []any{ | ||
map[string]any{ | ||
"anyOf": componentDefs, // TODO: Convert this to oneOf once issues are resolved. | ||
}, | ||
map[string]any{ | ||
"type": "object", | ||
"properties": generalFields, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Marshal attempts to marshal a JSON Schema definition containing the entire | ||
// config and plugin ecosystem such that other applications can potentially | ||
// execute their own linting and generation tools with it. | ||
func Marshal(fields docs.FieldSpecs, env *bundle.Environment) ([]byte, error) { | ||
defs := map[string]any{ | ||
"input": compSpecsToDefinition(env.InputDocs(), docs.ReservedFieldsByType(docs.TypeInput)), | ||
"buffer": compSpecsToDefinition(env.BufferDocs(), docs.ReservedFieldsByType(docs.TypeBuffer)), | ||
"cache": compSpecsToDefinition(env.CacheDocs(), docs.ReservedFieldsByType(docs.TypeCache)), | ||
"processor": compSpecsToDefinition(env.ProcessorDocs(), docs.ReservedFieldsByType(docs.TypeProcessor)), | ||
"rate_limit": compSpecsToDefinition(env.RateLimitDocs(), docs.ReservedFieldsByType(docs.TypeRateLimit)), | ||
"output": compSpecsToDefinition(env.OutputDocs(), docs.ReservedFieldsByType(docs.TypeOutput)), | ||
"metrics": compSpecsToDefinition(env.MetricsDocs(), docs.ReservedFieldsByType(docs.TypeMetrics)), | ||
"tracer": compSpecsToDefinition(env.TracersDocs(), docs.ReservedFieldsByType(docs.TypeTracer)), | ||
"scanner": compSpecsToDefinition(env.ScannerDocs(), docs.ReservedFieldsByType(docs.TypeScanner)), | ||
} | ||
|
||
schemaObj := map[string]any{ | ||
"properties": fields.JSONSchema(), | ||
"definitions": defs, | ||
} | ||
|
||
return json.Marshal(schemaObj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package jsonschema_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
jsonschema "github.com/xeipuuv/gojsonschema" | ||
|
||
"github.com/redpanda-data/benthos/v4/internal/bundle" | ||
"github.com/redpanda-data/benthos/v4/internal/component/input" | ||
"github.com/redpanda-data/benthos/v4/internal/component/processor" | ||
"github.com/redpanda-data/benthos/v4/internal/config" | ||
"github.com/redpanda-data/benthos/v4/internal/docs" | ||
ijschema "github.com/redpanda-data/benthos/v4/internal/jsonschema" | ||
|
||
_ "github.com/redpanda-data/benthos/v4/public/components/pure" | ||
) | ||
|
||
func testEnvWithPlugins(t testing.TB) *bundle.Environment { | ||
t.Helper() | ||
|
||
env := bundle.GlobalEnvironment.Clone() | ||
|
||
require.NoError(t, env.InputAdd(func(c input.Config, nm bundle.NewManagement) (input.Streamed, error) { | ||
return nil, errors.New("nope") | ||
}, docs.ComponentSpec{ | ||
Name: "testinput", | ||
Type: docs.TypeInput, | ||
Config: docs.FieldComponent().WithChildren( | ||
docs.FieldString("woof", "", "WOOF"), | ||
), | ||
})) | ||
|
||
require.NoError(t, env.ProcessorAdd(func(conf processor.Config, mgr bundle.NewManagement) (processor.V1, error) { | ||
return nil, errors.New("nope") | ||
}, docs.ComponentSpec{ | ||
Name: "testprocessor", | ||
Type: docs.TypeProcessor, | ||
Config: docs.FieldComponent().WithChildren( | ||
docs.FieldBloblang("mapfield", ""), | ||
), | ||
})) | ||
|
||
return env | ||
} | ||
|
||
func TestJSONSchema(t *testing.T) { | ||
env := testEnvWithPlugins(t) | ||
|
||
testSchema, err := ijschema.Marshal(config.Spec(), env) | ||
require.NoError(t, err) | ||
|
||
schema, err := jsonschema.NewSchema(jsonschema.NewStringLoader(string(testSchema))) | ||
require.NoError(t, err) | ||
|
||
res, err := schema.Validate(jsonschema.NewGoLoader(map[string]any{ | ||
"input": map[string]any{ | ||
"testinput": map[string]any{ | ||
"woof": "uhhhhh, woof!", | ||
}, | ||
"processors": []any{ | ||
map[string]any{ | ||
"testprocessor": map[string]any{ | ||
"mapfield": "hello world", | ||
}, | ||
}, | ||
}, | ||
}, | ||
})) | ||
require.NoError(t, err) | ||
require.Empty(t, res.Errors()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters