Skip to content

Commit

Permalink
Generate json schema for flowkit (#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Jul 3, 2023
1 parent 3e63214 commit daa1550
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
make ci
make check-tidy
make check-headers
make check-schema
- name: Upload coverage report
uses: codecov/codecov-action@v1
with:
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,19 @@ fix-lint:
check-headers:
@./check-headers.sh

.PHONY: check-schema
check-schema:
cd flowkit && go run ./cmd/flow-schema/flow-schema.go --verify=true ./schema.json

.PHONY: check-tidy
check-tidy:
go mod tidy
cd flowkit; go mod tidy

.PHONY: generate-schema
generate-schema:
cd flowkit && go run ./cmd/flow-schema/flow-schema.go ./schema.json

.PHONY: generate
generate: install-tools
cd flowkit; \
Expand Down
64 changes: 64 additions & 0 deletions flowkit/cmd/flow-schema/flow-schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Flow CLI
*
* Copyright 2019 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"encoding/json"
"flag"
"fmt"
"os"

configJson "github.com/onflow/flow-cli/flowkit/config/json"
)

func main() {
var verify bool
flag.BoolVar(&verify, "verify", false, "Verify the schema")

flag.Parse()
path := flag.Arg(0)

if path == "" {
fmt.Println("Path is required")
os.Exit(1)
}

schema := configJson.GenerateSchema()
json, err := json.MarshalIndent(schema, "", " ")

if err != nil {
fmt.Println(err)
os.Exit(1)
}

if verify {
fileContents, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if string(fileContents) != string(json) {
fmt.Println("Schema is out of date - have you run `make generate-schema`?")
os.Exit(1)
}
} else {
os.WriteFile(path, json, 0644)
}
}
26 changes: 26 additions & 0 deletions flowkit/config/json/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"regexp"
"strings"

"github.com/invopop/jsonschema"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -456,3 +457,28 @@ func (j account) MarshalJSON() ([]byte, error) {

return json.Marshal(j.Advanced)
}

func (a account) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Ref: "#/$defs/simpleAccount",
},
{
Ref: "#/$defs/advancedAccount",
},
{
Ref: "#/$defs/simpleAccountPre022",
},
{
Ref: "#/$defs/advanceAccountPre022",
},
},
Definitions: map[string]*jsonschema.Schema{
"simpleAccount": jsonschema.Reflect(simpleAccount{}),
"advancedAccount": jsonschema.Reflect(advancedAccount{}),
"simpleAccountPre022": jsonschema.Reflect(simpleAccountPre022{}),
"advanceAccountPre022": jsonschema.Reflect(advanceAccountPre022{}),
},
}
}
17 changes: 17 additions & 0 deletions flowkit/config/json/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"

"github.com/invopop/jsonschema"
"github.com/onflow/flow-go-sdk"

"github.com/onflow/flow-cli/flowkit/config"
Expand Down Expand Up @@ -131,3 +132,19 @@ func (j jsonContract) MarshalJSON() ([]byte, error) {
return json.Marshal(j.Advanced)
}
}

func (j jsonContract) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Type: "string",
},
{
Ref: "#/$defs/jsonContractAdvanced",
},
},
Definitions: map[string]*jsonschema.Schema{
"jsonContractAdvanced": jsonschema.Reflect(jsonContractAdvanced{}),
},
}
}
17 changes: 17 additions & 0 deletions flowkit/config/json/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"

"github.com/invopop/jsonschema"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"

Expand Down Expand Up @@ -179,3 +180,19 @@ func (d deployment) MarshalJSON() ([]byte, error) {
return json.Marshal(d.advanced)
}
}

func (j deployment) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Type: "string",
},
{
Ref: "#/$defs/contractDeployment",
},
},
Definitions: map[string]*jsonschema.Schema{
"contractDeployment": jsonschema.Reflect(contractDeployment{}),
},
}
}
20 changes: 20 additions & 0 deletions flowkit/config/json/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"strings"

"github.com/invopop/jsonschema"
"github.com/onflow/flow-go-sdk/crypto"

"github.com/onflow/flow-cli/flowkit/config"
Expand Down Expand Up @@ -147,3 +148,22 @@ func validateECDSAP256Pub(key string) error {

return nil
}

func (j jsonNetwork) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Ref: "#/$defs/simpleNetwork",
},
{
Ref: "#/$defs/advancedNetwork",
},
},
Definitions: map[string]*jsonschema.Schema{
"simpleNetwork": {
Type: "string",
},
"advancedNetwork": jsonschema.Reflect(advancedNetwork{}),
},
}
}
44 changes: 44 additions & 0 deletions flowkit/config/json/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package json

/*
* Flow CLI
*
* Copyright 2019 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import (
"github.com/invopop/jsonschema"
)

func GenerateSchema() *jsonschema.Schema {
schema := jsonschema.Reflect(jsonConfig{})

// Recursively move all definitions to the root of the schema
// This is necessary because the jsonschema library does not support
// definitions in nested schemas and is a workaround
var moveDefinitions func(*jsonschema.Schema)
moveDefinitions = func (s *jsonschema.Schema) {
for k, v := range s.Definitions {
schema.Definitions[k] = v
moveDefinitions(v)
}
if (s != schema) {
s.Definitions = nil
}
}
moveDefinitions(schema)

return schema
}
2 changes: 2 additions & 0 deletions flowkit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/ethereum/go-ethereum v1.10.22
github.com/gosuri/uilive v0.0.4
github.com/invopop/jsonschema v0.7.0
github.com/lmars/go-slip10 v0.0.0-20190606092855-400ba44fee12
github.com/onflow/cadence v0.39.12
github.com/onflow/flow-emulator v0.51.1
Expand Down Expand Up @@ -68,6 +69,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-block-format v0.0.3 // indirect
Expand Down
5 changes: 5 additions & 0 deletions flowkit/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,16 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY=
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down Expand Up @@ -690,6 +694,7 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v0.0.0-20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
Loading

0 comments on commit daa1550

Please sign in to comment.