Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
misc: generate jsonschema (#33)
Browse files Browse the repository at this point in the history
* generate jsonschema
  • Loading branch information
hgiasac authored Aug 10, 2024
1 parent bddef14 commit 78789cd
Show file tree
Hide file tree
Showing 20 changed files with 1,196 additions and 23 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ clean:
build:
go build -o _output/ndc-rest-schema .

.PHONY: build-jsonschema
build-jsonschema:
cd jsonschema && go run .

# build the ndc-rest-schema for all given platform/arch
.PHONY: ci-build
ci-build: export CGO_ENABLED=0
Expand Down
20 changes: 10 additions & 10 deletions command/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ func CommandConvertToNDCSchema(args *ConvertCommandArguments, logger *slog.Logge
// ConvertConfig represents the content of convert config file
type ConvertConfig struct {
File string `json:"file" yaml:"file"`
Spec schema.SchemaSpecType `json:"spec" yaml:"spec"`
MethodAlias map[string]string `json:"methodAlias" yaml:"methodAlias"`
TrimPrefix string `json:"trimPrefix" yaml:"trimPrefix"`
EnvPrefix string `json:"envPrefix" yaml:"envPrefix"`
Pure bool `json:"pure" yaml:"pure"`
Strict bool `json:"strict" yaml:"strict"`
PatchBefore []utils.PatchConfig `json:"patchBefore" yaml:"patchBefore"`
PatchAfter []utils.PatchConfig `json:"patchAfter" yaml:"patchAfter"`
AllowedContentTypes []string `json:"allowedContentTypes" yaml:"allowedContentTypes"`
Output string `json:"output" yaml:"output"`
Spec schema.SchemaSpecType `json:"spec,omitempty" yaml:"spec"`
MethodAlias map[string]string `json:"methodAlias,omitempty" yaml:"methodAlias"`
TrimPrefix string `json:"trimPrefix,omitempty" yaml:"trimPrefix"`
EnvPrefix string `json:"envPrefix,omitempty" yaml:"envPrefix"`
Pure bool `json:"pure,omitempty" yaml:"pure"`
Strict bool `json:"strict,omitempty" yaml:"strict"`
PatchBefore []utils.PatchConfig `json:"patchBefore,omitempty" yaml:"patchBefore"`
PatchAfter []utils.PatchConfig `json:"patchAfter,omitempty" yaml:"patchAfter"`
AllowedContentTypes []string `json:"allowedContentTypes,omitempty" yaml:"allowedContentTypes"`
Output string `json:"output,omitempty" yaml:"output"`
}

// ConvertToNDCSchema converts to NDC REST schema from config
Expand Down
1 change: 1 addition & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/hasura/ndc-rest-schema/main/jsonschema/convert-config.jsonschema
# -- File path needs to be converted.
file: ""

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ require (
github.com/alecthomas/kong v0.9.0
github.com/evanphx/json-patch v0.5.2
github.com/hasura/ndc-sdk-go v1.2.5
github.com/invopop/jsonschema v0.12.0
github.com/lmittmann/tint v1.0.5
github.com/pb33f/libopenapi v0.16.14
github.com/wk8/go-ordered-map/v2 v2.1.8
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -19,6 +21,5 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/net v0.28.0 // indirect
)
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand All @@ -69,8 +71,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.16.13 h1:uR/W3Rit/yxRWG5DWal26PdEnEq4mu/3cYjbkK6LHm0=
github.com/pb33f/libopenapi v0.16.13/go.mod h1:8/lZGTZmxybpTPOggS6LefdrYvsQ5kbirD364TceyQo=
github.com/pb33f/libopenapi v0.16.14 h1:NyyYWAhNuuzVO/PM690tKUaljfL5nZY77w1Kx4kiKIg=
github.com/pb33f/libopenapi v0.16.14/go.mod h1:8/lZGTZmxybpTPOggS6LefdrYvsQ5kbirD364TceyQo=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -101,8 +101,6 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -122,16 +120,16 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
Expand Down
91 changes: 91 additions & 0 deletions jsonschema/convert-config.jsonschema
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/hasura/ndc-rest-schema/command/convert-config",
"$ref": "#/$defs/ConvertConfig",
"$defs": {
"ConvertConfig": {
"properties": {
"file": {
"type": "string"
},
"spec": {
"$ref": "#/$defs/SchemaSpecType"
},
"methodAlias": {
"additionalProperties": {
"type": "string"
},
"type": "object"
},
"trimPrefix": {
"type": "string"
},
"envPrefix": {
"type": "string"
},
"pure": {
"type": "boolean"
},
"strict": {
"type": "boolean"
},
"patchBefore": {
"items": {
"$ref": "#/$defs/PatchConfig"
},
"type": "array"
},
"patchAfter": {
"items": {
"$ref": "#/$defs/PatchConfig"
},
"type": "array"
},
"allowedContentTypes": {
"items": {
"type": "string"
},
"type": "array"
},
"output": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"file"
]
},
"PatchConfig": {
"properties": {
"path": {
"type": "string"
},
"strategy": {
"type": "string",
"enum": [
"merge",
"json6902"
]
}
},
"additionalProperties": false,
"type": "object",
"required": [
"path",
"strategy"
]
},
"SchemaSpecType": {
"type": "string",
"enum": [
"oas3",
"oas2",
"openapi3",
"openapi2",
"ndc"
]
}
}
}
40 changes: 40 additions & 0 deletions jsonschema/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

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

"github.com/hasura/ndc-rest-schema/command"
"github.com/hasura/ndc-rest-schema/schema"
"github.com/invopop/jsonschema"
)

func main() {
if err := jsonSchemaConvertConfig(); err != nil {
panic(fmt.Errorf("failed to write jsonschema for ConvertConfig: %s", err))
}
if err := jsonSchemaNdcRESTSchema(); err != nil {
panic(fmt.Errorf("failed to write jsonschema for NDCRestSchema: %s", err))
}
}

func jsonSchemaConvertConfig() error {
reflectSchema := jsonschema.Reflect(&command.ConvertConfig{})
schemaBytes, err := json.MarshalIndent(reflectSchema, "", " ")
if err != nil {
return err
}

return os.WriteFile("convert-config.jsonschema", schemaBytes, 0644)
}

func jsonSchemaNdcRESTSchema() error {
reflectSchema := jsonschema.Reflect(&schema.NDCRestSchema{})
schemaBytes, err := json.MarshalIndent(reflectSchema, "", " ")
if err != nil {
return err
}

return os.WriteFile("ndc-rest-schema.jsonschema", schemaBytes, 0644)
}
Loading

0 comments on commit 78789cd

Please sign in to comment.