@@ -2,14 +2,13 @@ package jsonschema
2
2
3
3
import (
4
4
"bytes"
5
- "encoding/json"
6
5
"errors"
7
6
"fmt"
8
7
"os"
9
8
"strings"
10
9
11
- "github.com/iancoleman/orderedmap"
12
10
"github.com/jesseduffield/lazycore/pkg/utils"
11
+ "github.com/karimkhaleel/jsonschema"
13
12
"github.com/samber/lo"
14
13
15
14
"gopkg.in/yaml.v3"
@@ -106,16 +105,7 @@ func (n *Node) MarshalYAML() (interface{}, error) {
106
105
setComment (& keyNode , n .Description )
107
106
}
108
107
109
- if n .Default != nil {
110
- valueNode := yaml.Node {
111
- Kind : yaml .ScalarNode ,
112
- }
113
- err := valueNode .Encode (n .Default )
114
- if err != nil {
115
- return nil , err
116
- }
117
- node .Content = append (node .Content , & keyNode , & valueNode )
118
- } else if len (n .Children ) > 0 {
108
+ if len (n .Children ) > 0 {
119
109
childrenNode := yaml.Node {
120
110
Kind : yaml .MappingNode ,
121
111
}
@@ -136,60 +126,18 @@ func (n *Node) MarshalYAML() (interface{}, error) {
136
126
childrenNode .Content = append (childrenNode .Content , childYaml .(* yaml.Node ).Content ... )
137
127
}
138
128
node .Content = append (node .Content , & keyNode , & childrenNode )
139
- }
140
-
141
- return & node , nil
142
- }
143
-
144
- func getDescription (v * orderedmap.OrderedMap ) string {
145
- description , ok := v .Get ("description" )
146
- if ! ok {
147
- description = ""
148
- }
149
- return description .(string )
150
- }
151
-
152
- func getDefault (v * orderedmap.OrderedMap ) (error , any ) {
153
- defaultValue , ok := v .Get ("default" )
154
- if ok {
155
- return nil , defaultValue
156
- }
157
-
158
- dataType , ok := v .Get ("type" )
159
- if ok {
160
- dataTypeString := dataType .(string )
161
- if dataTypeString == "string" {
162
- return nil , ""
129
+ } else {
130
+ valueNode := yaml.Node {
131
+ Kind : yaml .ScalarNode ,
163
132
}
133
+ err := valueNode .Encode (n .Default )
134
+ if err != nil {
135
+ return nil , err
136
+ }
137
+ node .Content = append (node .Content , & keyNode , & valueNode )
164
138
}
165
139
166
- return errors .New ("Failed to get default value" ), nil
167
- }
168
-
169
- func parseNode (parent * Node , name string , value * orderedmap.OrderedMap ) {
170
- description := getDescription (value )
171
- err , defaultValue := getDefault (value )
172
- if err == nil {
173
- leaf := & Node {Name : name , Description : description , Default : defaultValue }
174
- parent .Children = append (parent .Children , leaf )
175
- }
176
-
177
- properties , ok := value .Get ("properties" )
178
- if ! ok {
179
- return
180
- }
181
-
182
- orderedProperties := properties .(orderedmap.OrderedMap )
183
-
184
- node := & Node {Name : name , Description : description }
185
- parent .Children = append (parent .Children , node )
186
-
187
- keys := orderedProperties .Keys ()
188
- for _ , name := range keys {
189
- value , _ := orderedProperties .Get (name )
190
- typedValue := value .(orderedmap.OrderedMap )
191
- parseNode (node , name , & typedValue )
192
- }
140
+ return & node , nil
193
141
}
194
142
195
143
func writeToConfigDocs (config []byte ) error {
@@ -222,30 +170,37 @@ func writeToConfigDocs(config []byte) error {
222
170
return nil
223
171
}
224
172
225
- func GenerateConfigDocs () {
226
- content , err := os .ReadFile (GetSchemaDir () + "/config.json" )
227
- if err != nil {
228
- panic ("Error reading config.json" )
173
+ func GenerateConfigDocs (schema * jsonschema.Schema ) {
174
+ rootNode := & Node {
175
+ Children : make ([]* Node , 0 ),
229
176
}
230
177
231
- schema := orderedmap .New ()
178
+ userConfigSchema := schema .Definitions ["UserConfig" ]
179
+ for pair := userConfigSchema .Properties .Oldest (); pair != nil ; pair = pair .Next () {
180
+ yamlName := pair .Key
181
+ subSchema := getSubSchema (schema , userConfigSchema , yamlName )
232
182
233
- err = json .Unmarshal (content , & schema )
234
- if err != nil {
235
- panic ("Failed to unmarshal config.json" )
236
- }
183
+ if subSchema .Type == "array" {
184
+ _ = subSchema
185
+ }
237
186
238
- root , ok := schema .Get ("properties" )
239
- if ! ok {
240
- panic ("properties key not found in schema" )
241
- }
242
- orderedRoot := root .(orderedmap.OrderedMap )
187
+ // Skip empty objects
188
+ if subSchema .Type == "object" && subSchema .Properties == nil {
189
+ continue
190
+ }
191
+
192
+ // Skip empty arrays
193
+ if isZeroValue (subSchema .Default ) && subSchema .Type == "array" {
194
+ continue
195
+ }
243
196
244
- rootNode := Node {}
245
- for _ , name := range orderedRoot .Keys () {
246
- value , _ := orderedRoot .Get (name )
247
- typedValue := value .(orderedmap.OrderedMap )
248
- parseNode (& rootNode , name , & typedValue )
197
+ node := Node {
198
+ Name : pair .Key ,
199
+ Description : subSchema .Description ,
200
+ Default : getZeroValue (subSchema .Default , subSchema .Type ),
201
+ }
202
+ recurseOverSchema (schema , subSchema , & node )
203
+ rootNode .Children = append (rootNode .Children , & node )
249
204
}
250
205
251
206
var buffer bytes.Buffer
@@ -262,8 +217,54 @@ func GenerateConfigDocs() {
262
217
263
218
config := prepareMarshalledConfig (buffer )
264
219
265
- err = writeToConfigDocs (config )
220
+ err : = writeToConfigDocs (config )
266
221
if err != nil {
267
222
panic (err )
268
223
}
269
224
}
225
+
226
+ func recurseOverSchema (rootSchema , schema * jsonschema.Schema , parent * Node ) {
227
+ if schema == nil || schema .Properties == nil || schema .Properties .Len () == 0 {
228
+ return
229
+ }
230
+
231
+ for pair := schema .Properties .Oldest (); pair != nil ; pair = pair .Next () {
232
+ if pair .Key == "confirmOnQuit" {
233
+ _ = pair
234
+ }
235
+ subSchema := getSubSchema (rootSchema , schema , pair .Key )
236
+
237
+ // Skip empty objects
238
+ if subSchema .Type == "object" && subSchema .Properties == nil {
239
+ continue
240
+ }
241
+
242
+ // Skip empty arrays
243
+ if isZeroValue (subSchema .Default ) && subSchema .Type == "array" {
244
+ continue
245
+ }
246
+
247
+ node := Node {
248
+ Name : pair .Key ,
249
+ Description : subSchema .Description ,
250
+ Default : getZeroValue (subSchema .Default , subSchema .Type ),
251
+ }
252
+ parent .Children = append (parent .Children , & node )
253
+ recurseOverSchema (rootSchema , subSchema , & node )
254
+ }
255
+ }
256
+
257
+ func getZeroValue (val any , t string ) any {
258
+ if ! isZeroValue (val ) {
259
+ return val
260
+ }
261
+
262
+ switch t {
263
+ case "string" :
264
+ return ""
265
+ case "boolean" :
266
+ return false
267
+ default :
268
+ return nil
269
+ }
270
+ }
0 commit comments