Skip to content

Commit

Permalink
Refactor schema generation script
Browse files Browse the repository at this point in the history
  • Loading branch information
karimkhaleel committed Sep 30, 2023
1 parent fabf7c0 commit 3e4a5fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
32 changes: 24 additions & 8 deletions scripts/jsonschema/main.go → pkg/jsonschema/generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package main
//go:generate go run generator.go

package jsonschema

import (
"encoding/json"
Expand All @@ -7,22 +9,27 @@ import (
"reflect"

"github.com/invopop/jsonschema"
"github.com/jesseduffield/lazycore/pkg/utils"
"github.com/jesseduffield/lazygit/pkg/config"
)

func main() {
schema := CustomReflect(&config.UserConfig{})
func GetSchemaDir() string {
return utils.GetLazyRootDirectory() + "/schema"
}

func GenerateSchema() {
schema := customReflect(&config.UserConfig{})
obj, _ := json.MarshalIndent(schema, "", " ")

if err := os.WriteFile("schema.json", obj, 0o644); err != nil {
if err := os.WriteFile(GetSchemaDir()+"/schema.json", obj, 0o644); err != nil {
fmt.Println("Error writing to file:", err)
return
}
}

func CustomReflect(v *config.UserConfig) *jsonschema.Schema {
func customReflect(v *config.UserConfig) *jsonschema.Schema {
defaultConfig := config.GetDefaultConfig()
r := new(jsonschema.Reflector)
r := &jsonschema.Reflector{KeyNamer: func(name string) string { return name }, FieldNameTag: "yaml"}
if err := r.AddGoComments("github.com/jesseduffield/lazygit", "./"); err != nil {
panic(err)
}
Expand Down Expand Up @@ -67,8 +74,17 @@ func setDefaultVals(defaults interface{}, schema *jsonschema.Schema) {
continue
}

property, ok := definition.Properties.Get(key)
if ok && property == nil {
propertyName, ok := definition.OriginalPropertiesMapping[key]
if !ok {
continue
}

property, ok := definition.Properties.Get(propertyName)
if !ok {
continue
}

if property.Default != nil {
continue
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/jsonschema/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build ignore

package main

import (
"fmt"

"github.com/jesseduffield/lazygit/pkg/jsonschema"
)

func main() {
fmt.Printf("Generating jsonschema in %s...\n", jsonschema.GetSchemaDir())
jsonschema.GenerateSchema()
}

0 comments on commit 3e4a5fc

Please sign in to comment.