Skip to content

Commit

Permalink
feat: Add command to generate JSON schema of config (dc) (#240)
Browse files Browse the repository at this point in the history
* Add command to generate JSON schema of config

* minor niggles

---------

Co-authored-by: kaiiorg <k349@live.com>
  • Loading branch information
danielchalef and kaiiorg committed Oct 20, 2023
1 parent 04d4698 commit 0b5e020
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/zep/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,25 @@ var loadFixturesCmd = &cobra.Command{
},
}

var dumpJsonSchemaCmd = &cobra.Command{
Use: "json-schema",
Short: "Generates JSON Schema for Zep's configuration file",
Example: "zep json-schema > zep_config_schema.json",
RunE: func(cmd *cobra.Command, args []string) error {
schema, err := config.JSONSchema()
if err != nil {
return err
}
fmt.Println(string(schema))
return nil
},
}

func init() {
testCmd.AddCommand(createFixturesCmd)
testCmd.AddCommand(loadFixturesCmd)
cmd.AddCommand(testCmd)
cmd.AddCommand(dumpJsonSchemaCmd)

cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default config.yaml)")
cmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "print version number")
Expand Down
21 changes: 21 additions & 0 deletions config/json_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

import (
"errors"

"github.com/invopop/jsonschema"
)

var (
ErrGeneratedSchemaIsNil = errors.New("generated JSON Schema is nil")
)

func JSONSchema() ([]byte, error) {
schema := jsonschema.Reflect(&Config{})

if schema == nil {
return nil, ErrGeneratedSchemaIsNil
}

return schema.MarshalJSON()
}
18 changes: 18 additions & 0 deletions config/json_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package config

import (
"testing"

"github.com/invopop/jsonschema"
"github.com/stretchr/testify/assert"
)

func TestJSONSchema(t *testing.T) {
schemaJSON, err := JSONSchema()
assert.NoError(t, err)
assert.NotNil(t, schemaJSON)

unmarshalledSchema := &jsonschema.Schema{}
err = unmarshalledSchema.UnmarshalJSON(schemaJSON)
assert.NoError(t, err)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/getzep/sprig/v3 v3.0.0-20230930153539-1d7fce7d845e
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/invopop/jsonschema v0.12.0
github.com/tmc/langchaingo v0.0.0-20230929160525-e16b77704b8d
github.com/uptrace/bun/dbfixture v1.1.16
github.com/uptrace/bun/extra/bundebug v1.1.16
Expand All @@ -46,6 +47,8 @@ require (
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/chewxy/math32 v1.10.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand Down Expand Up @@ -96,6 +99,7 @@ require (
github.com/viterin/partial v1.1.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ github.com/alitto/pond v1.8.3 h1:ydIqygCLVPqIX/USe5EaV/aSRXTRXDEI9JwuDdu+/xs=
github.com/alitto/pond v1.8.3/go.mod h1:CmvIIGd5jKLasGI3D87qDkQxjzChdKMmnXMg3fG6M6Q=
github.com/avast/retry-go/v4 v4.5.0 h1:QoRAZZ90cj5oni2Lsgl2GW8mNTnUCnmpx/iKpwVisHg=
github.com/avast/retry-go/v4 v4.5.0/go.mod h1:7hLEXp0oku2Nir2xBAsg0PTphp9z71bN5Aq1fboC3+I=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/brianvoe/gofakeit/v6 v6.23.2 h1:lVde18uhad5wII/f5RMVFLtdQNE0HaGFuBUXmYKk8i8=
github.com/brianvoe/gofakeit/v6 v6.23.2/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chewxy/math32 v1.10.1 h1:LFpeY0SLJXeaiej/eIp2L40VYfscTvKh/FSEZ68uMkU=
github.com/chewxy/math32 v1.10.1/go.mod h1:dOB2rcuFrCn6UHrze36WSLVPKtzPMRAQvBvUwkSsLqs=
Expand Down Expand Up @@ -212,6 +216,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down Expand Up @@ -362,6 +368,8 @@ github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vb
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down

0 comments on commit 0b5e020

Please sign in to comment.