Skip to content

Commit 503e5c8

Browse files
authored
feat: get arg type information from schema (#57)
* feat: get arg type information from schema * fix(render): get type itself from schema In JSON schema, `type` can be an array if multiple types are allowed. * fix: join if type is array regardless
1 parent fd8de90 commit 503e5c8

3 files changed

Lines changed: 199 additions & 48 deletions

File tree

doc-util/README.md

Lines changed: 120 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ local d = import "github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet"
2525
* [`fn render(obj)`](#fn-render)
2626
* [`fn val(type, help, default)`](#fn-val)
2727
* [`obj argument`](#obj-argument)
28+
* [`fn fromSchema(name, schema)`](#fn-argumentfromschema)
2829
* [`fn new(name, type, default, enums)`](#fn-argumentnew)
2930
* [`obj func`](#obj-func)
3031
* [`fn new(help, args)`](#fn-funcnew)
@@ -48,38 +49,65 @@ local d = import "github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet"
4849
arg(name, type, default, enums)
4950
```
5051

51-
`arg` is a shorthand for `argument.new`
52+
PARAMETERS:
53+
54+
* **name** (`string`)
55+
* **type** (`string`)
56+
* **default** (`any`)
57+
* **enums** (`array`)
5258

59+
`arg` is a shorthand for `argument.new`
5360
### fn fn
5461

5562
```jsonnet
5663
fn(help, args)
5764
```
5865

59-
`fn` is a shorthand for `func.new`
66+
PARAMETERS:
6067

68+
* **help** (`string`)
69+
* **args** (`array`)
70+
71+
`fn` is a shorthand for `func.new`
6172
### fn obj
6273

6374
```jsonnet
6475
obj(help, fields)
6576
```
6677

67-
`obj` is a shorthand for `object.new`
78+
PARAMETERS:
79+
80+
* **help** (`string`)
81+
* **fields** (`object`)
6882

83+
`obj` is a shorthand for `object.new`
6984
### fn pkg
7085

7186
```jsonnet
7287
pkg(name, url, help, filename="", version="master")
7388
```
7489

75-
`new` is a shorthand for `package.new`
90+
PARAMETERS:
7691

92+
* **name** (`string`)
93+
* **url** (`string`)
94+
* **help** (`string`)
95+
* **filename** (`string`)
96+
- default value: `""`
97+
* **version** (`string`)
98+
- default value: `"master"`
99+
100+
`new` is a shorthand for `package.new`
77101
### fn render
78102

79103
```jsonnet
80104
render(obj)
81105
```
82106

107+
PARAMETERS:
108+
109+
* **obj** (`object`)
110+
83111
`render` converts the docstrings to human readable Markdown files.
84112

85113
Usage:
@@ -91,25 +119,59 @@ d.render(import 'main.libsonnet')
91119

92120
Call with: `jsonnet -S -c -m docs/ docs.jsonnet`
93121

94-
95122
### fn val
96123

97124
```jsonnet
98125
val(type, help, default)
99126
```
100127

101-
`val` is a shorthand for `value.new`
128+
PARAMETERS:
129+
130+
* **type** (`string`)
131+
* **help** (`string`)
132+
* **default** (`any`)
102133

134+
`val` is a shorthand for `value.new`
103135
### obj argument
104136

105137
Utilities for creating function arguments
106138

139+
#### fn argument.fromSchema
140+
141+
```jsonnet
142+
argument.fromSchema(name, schema)
143+
```
144+
145+
PARAMETERS:
146+
147+
* **name** (`string`)
148+
* **schema** (`object`)
149+
150+
`fromSchema` creates a new function argument, taking a JSON `schema` to describe the type information for this argument.
151+
152+
Examples:
153+
154+
```jsonnet
155+
[
156+
d.argument.fromSchema('foo', { type: 'string' }),
157+
d.argument.fromSchema('bar', { type: 'string', default='loo' }),
158+
d.argument.fromSchema('baz', { type: 'number', enum=[1,2,3] }),
159+
]
160+
```
161+
107162
#### fn argument.new
108163

109164
```jsonnet
110-
new(name, type, default, enums)
165+
argument.new(name, type, default, enums)
111166
```
112167

168+
PARAMETERS:
169+
170+
* **name** (`string`)
171+
* **type** (`string`)
172+
* **default** (`any`)
173+
* **enums** (`array`)
174+
113175
`new` creates a new function argument, taking the `name`, the `type`. Optionally it
114176
can take a `default` value and `enum`-erate potential values.
115177

@@ -123,67 +185,88 @@ Examples:
123185
]
124186
```
125187

126-
127188
### obj func
128189

129190
Utilities for documenting Jsonnet methods (functions of objects)
130191

131192
#### fn func.new
132193

133194
```jsonnet
134-
new(help, args)
195+
func.new(help, args)
135196
```
136197

137-
new creates a new function, optionally with description and arguments
198+
PARAMETERS:
138199

200+
* **help** (`string`)
201+
* **args** (`array`)
202+
203+
new creates a new function, optionally with description and arguments
139204
#### fn func.withArgs
140205

141206
```jsonnet
142-
withArgs(args)
207+
func.withArgs(args)
143208
```
144209

145-
The `withArgs` modifier overrides the arguments of that function
210+
PARAMETERS:
211+
212+
* **args** (`array`)
146213

214+
The `withArgs` modifier overrides the arguments of that function
147215
#### fn func.withHelp
148216

149217
```jsonnet
150-
withHelp(help)
218+
func.withHelp(help)
151219
```
152220

153-
The `withHelp` modifier overrides the help text of that function
221+
PARAMETERS:
154222

223+
* **help** (`string`)
224+
225+
The `withHelp` modifier overrides the help text of that function
155226
### obj object
156227

157228
Utilities for documenting Jsonnet objects (`{ }`).
158229

159230
#### fn object.new
160231

161232
```jsonnet
162-
new(help, fields)
233+
object.new(help, fields)
163234
```
164235

165-
new creates a new object, optionally with description and fields
236+
PARAMETERS:
166237

238+
* **help** (`string`)
239+
* **fields** (`object`)
240+
241+
new creates a new object, optionally with description and fields
167242
#### fn object.withFields
168243

169244
```jsonnet
170-
withFields(fields)
245+
object.withFields(fields)
171246
```
172247

173-
The `withFields` modifier overrides the fields property of an already created object
248+
PARAMETERS:
249+
250+
* **fields** (`object`)
174251

252+
The `withFields` modifier overrides the fields property of an already created object
175253
### obj value
176254

177255
Utilities for documenting plain Jsonnet values (primitives)
178256

179257
#### fn value.new
180258

181259
```jsonnet
182-
new(type, help, default)
260+
value.new(type, help, default)
183261
```
184262

185-
new creates a new object of given type, optionally with description and default value
263+
PARAMETERS:
264+
265+
* **type** (`string`)
266+
* **help** (`string`)
267+
* **default** (`any`)
186268

269+
new creates a new object of given type, optionally with description and default value
187270
### obj T
188271

189272
* `T.any` (`string`): `"any"` - argument of type "any"
@@ -201,9 +284,19 @@ new creates a new object of given type, optionally with description and default
201284
#### fn package.new
202285

203286
```jsonnet
204-
new(name, url, help, filename="", version="master")
287+
package.new(name, url, help, filename="", version="master")
205288
```
206289

290+
PARAMETERS:
291+
292+
* **name** (`string`)
293+
* **url** (`string`)
294+
* **help** (`string`)
295+
* **filename** (`string`)
296+
- default value: `""`
297+
* **version** (`string`)
298+
- default value: `"master"`
299+
207300
`new` creates a new package
208301

209302
Arguments:
@@ -214,17 +307,20 @@ Arguments:
214307
* `filename` for the import, defaults to blank for backward compatibility
215308
* `version` for jsonnet-bundler install, defaults to `master` just like jsonnet-bundler
216309

217-
218310
#### fn package.newSub
219311

220312
```jsonnet
221-
newSub(name, help)
313+
package.newSub(name, help)
222314
```
223315

316+
PARAMETERS:
317+
318+
* **name** (`string`)
319+
* **help** (`string`)
320+
224321
`newSub` creates a package without the preconfigured install/usage templates.
225322

226323
Arguments:
227324

228325
* given `name`
229326
* `help` text
230-

doc-util/main.libsonnet

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@
173173
default: default,
174174
enums: enums,
175175
},
176+
'#fromSchema': d.fn(|||
177+
`fromSchema` creates a new function argument, taking a JSON `schema` to describe the type information for this argument.
178+
179+
Examples:
180+
181+
```jsonnet
182+
[
183+
d.argument.fromSchema('foo', { type: 'string' }),
184+
d.argument.fromSchema('bar', { type: 'string', default='loo' }),
185+
d.argument.fromSchema('baz', { type: 'number', enum=[1,2,3] }),
186+
]
187+
```
188+
|||, [
189+
d.arg('name', d.T.string),
190+
d.arg('schema', d.T.object),
191+
]),
192+
fromSchema(name, schema): {
193+
name: name,
194+
schema: schema,
195+
},
176196
},
177197
'#arg': self.argument['#new'] + self.func.withHelp('`arg` is a shorthand for `argument.new`'),
178198
arg:: self.argument.new,

0 commit comments

Comments
 (0)