Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JSON schema of Flogo app descriptor #20

Merged
merged 2 commits into from
Feb 28, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _testmain.go
.vscode/
tags
.vscode/symbols.json
.idea
.build-cache
submodules/flogo-cicd/.build-cache
./Dockerfile
Expand Down
10 changes: 5 additions & 5 deletions action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import "encoding/json"
// Config is the configuration for the Action
type Config struct {
//inline action
Ref string `json:"ref"`
Type string `json:"type"` //an alias to the ref, can be used if imported
Settings map[string]interface{} `json:"settings"`
Ref string `json:"ref,omitempty"`
Type string `json:"type,omitempty"` //an alias to the ref, can be used if imported
Settings map[string]interface{} `json:"settings,omitempty"`

//referenced action
Id string `json:"id"`
Id string `json:"id,omitempty"`

//DEPRECATED
Data json.RawMessage `json:"data"`
Data json.RawMessage `json:"data,omitempty"`
}
13 changes: 7 additions & 6 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ type Config struct {
Type string `json:"type"`
Version string `json:"version"`
Description string `json:"description"`
AppModel string `json:"appModel"`

Imports []string `json:"imports"`
Properties []*data.Attribute `json:"properties"`
Channels []string `json:"channels"`
Triggers []*trigger.Config `json:"triggers"`
Resources []*resource.Config `json:"resources"`
Actions []*action.Config `json:"actions"`
Imports []string `json:"imports,omitempty"`
Properties []*data.Attribute `json:"properties,omitempty"`
Channels []string `json:"channels,omitempty"`
Triggers []*trigger.Config `json:"triggers,omitempty"`
Resources []*resource.Config `json:"resources,omitempty"`
Actions []*action.Config `json:"actions,omitempty"`
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/project-flogo/core

require (
github.com/square-it/jsonschema v1.9.1 // indirect
github.com/stretchr/testify v1.2.2
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/project-flogo/core v0.9.0-alpha.3/go.mod h1:BHeB55AxPhvlNGd+it50rE977ag6xE3bD2RluSDeKBA=
github.com/square-it/jsonschema v1.9.1 h1:0pYdNW+bvukTIBqcfal5XXHikgp/AbADeqcP7I0uV4M=
github.com/square-it/jsonschema v1.9.1/go.mod h1:80WJHSuy3YnokzfFopfx+MAt5lVVnVpS6w2Avv+svHk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
235 changes: 235 additions & 0 deletions internal/schema/assets.go

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

28 changes: 28 additions & 0 deletions internal/schema/generate/schema_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build ignore

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/square-it/jsonschema"
"github.com/project-flogo/core/app"
)

func main() {
reflector := &jsonschema.Reflector{ExpandedStruct: true}
schema := reflector.Reflect(&app.Config{})
schemaJSON, err := json.MarshalIndent(schema, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
err = ioutil.WriteFile("schema.json", schemaJSON, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
47 changes: 47 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:generate go run generate/schema_generator.go
//go:generate go-bindata -pkg schema -o assets.go schema.json

package schema

import (
"bytes"
"errors"
"fmt"

"github.com/xeipuuv/gojsonschema"
)

var schema *gojsonschema.Schema

func init() {
jsonSchema, err := Asset("schema.json")
if err != nil {
panic(err)
}
schemaLoader := gojsonschema.NewStringLoader(string(jsonSchema))
schema, err = gojsonschema.NewSchema(schemaLoader)
if err != nil {
panic(err)
}
}

// Validate validates the provided JSON against the v2 JSON schema.
func Validate(JSON []byte) error {
JSONLoader := gojsonschema.NewStringLoader(string(JSON))
result, err := schema.Validate(JSONLoader)

if err != nil {
return err
}

if result.Valid() {
return err
}
var msg bytes.Buffer

msg.WriteString("The JSON is not valid. See errors:\n")
for _, desc := range result.Errors() {
msg.WriteString(fmt.Sprintf("- %s\n", desc))
}
return errors.New(msg.String())
}
Loading