summaryrefslogtreecommitdiffstats
path: root/pkg/jsonschema
diff options
context:
space:
mode:
authorKarim Khaleel <code.karim@gmail.com>2023-09-30 02:47:06 +0300
committerStefan Haller <stefan@haller-berlin.de>2023-12-02 10:46:24 +0100
commit1a035db4c817ac07cd59f0f2aa4e1efd1a9b75b6 (patch)
tree42ada62060c643a6d09fb904a8775121defb4167 /pkg/jsonschema
parentdf5b3693d6821d6d946afffb6002ea1dd4647929 (diff)
Add UserConfig jsonschema generation script
Diffstat (limited to 'pkg/jsonschema')
-rw-r--r--pkg/jsonschema/generate.go106
-rw-r--r--pkg/jsonschema/generator.go14
2 files changed, 120 insertions, 0 deletions
diff --git a/pkg/jsonschema/generate.go b/pkg/jsonschema/generate.go
new file mode 100644
index 000000000..38b582844
--- /dev/null
+++ b/pkg/jsonschema/generate.go
@@ -0,0 +1,106 @@
+//go:generate go run generator.go
+
+package jsonschema
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+ "reflect"
+
+ "github.com/jesseduffield/lazycore/pkg/utils"
+ "github.com/jesseduffield/lazygit/pkg/config"
+ "github.com/karimkhaleel/jsonschema"
+)
+
+func GetSchemaDir() string {
+ return utils.GetLazyRootDirectory() + "/schema"
+}
+
+func GenerateSchema() {
+ schema := customReflect(&config.UserConfig{})
+ obj, _ := json.MarshalIndent(schema, "", " ")
+
+ if err := os.WriteFile(GetSchemaDir()+"/config.json", obj, 0o644); err != nil {
+ fmt.Println("Error writing to file:", err)
+ return
+ }
+}
+
+func customReflect(v *config.UserConfig) *jsonschema.Schema {
+ defaultConfig := config.GetDefaultConfig()
+ r := &jsonschema.Reflector{FieldNameTag: "yaml", RequiredFromJSONSchemaTags: true, DoNotReference: true}
+ if err := r.AddGoComments("github.com/jesseduffield/lazygit/pkg/config", "../config"); err != nil {
+ panic(err)
+ }
+ schema := r.Reflect(v)
+
+ setDefaultVals(defaultConfig, schema)
+
+ return schema
+}
+
+func setDefaultVals(defaults any, schema *jsonschema.Schema) {
+ t := reflect.TypeOf(defaults)
+ v := reflect.ValueOf(defaults)
+
+ if t.Kind() == reflect.Ptr || t.Kind() == reflect.Interface {
+ t = t.Elem()
+ v = v.Elem()
+ }
+
+ for i := 0; i < t.NumField(); i++ {
+ value := v.Field(i).Interface()
+ parentKey := t.Field(i).Name
+
+ key, ok := schema.OriginalPropertiesMapping[parentKey]
+ if !ok {
+ continue
+ }
+
+ subSchema, ok := schema.Properties.Get(key)
+ if !ok {
+ continue
+ }
+
+ if isStruct(value) {
+ setDefaultVals(value, subSchema)
+ } else if !isZeroValue(value) {
+ subSchema.Default = value
+ }
+ }
+}
+
+func isZeroValue(v any) bool {
+ switch v := v.(type) {
+ case int, int32, int64, float32, float64:
+ return v == 0
+ case string:
+ return v == ""
+ case bool:
+ return !v
+ case nil:
+ return true
+ }
+
+ rv := reflect.ValueOf(v)
+ switch rv.Kind() {
+ case reflect.Slice, reflect.Map:
+ return rv.Len() == 0
+ case reflect.Ptr, reflect.Interface:
+ return rv.IsNil()
+ case reflect.Struct:
+ for i := 0; i < rv.NumField(); i++ {
+ if !isZeroValue(rv.Field(i).Interface()) {
+ return false
+ }
+ }
+ return true
+ default:
+ return false
+ }
+}
+
+func isStruct(v any) bool {
+ return reflect.TypeOf(v).Kind() == reflect.Struct
+}
diff --git a/pkg/jsonschema/generator.go b/pkg/jsonschema/generator.go
new file mode 100644
index 000000000..263f4784b
--- /dev/null
+++ b/pkg/jsonschema/generator.go
@@ -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()
+}