Skip to content

Commit

Permalink
feat: Include schema in model (#55)
Browse files Browse the repository at this point in the history
- Removed enum from description string
- Added  Format, Enum, Default,
 Minimum and Maximum in the built-in
 templates.

Signed-off-by: roee88 <roee88@gmail.com>
  • Loading branch information
roee88 authored Jun 30, 2021
1 parent 23094f2 commit 3296bd6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
8 changes: 7 additions & 1 deletion example/crds/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ spec:
additionalProperties:
properties:
code:
description: Some code
type: integer
format: int32
default: 7
minimum: 0
maximum: 10
text:
description: Just text
type: string
area:
type: string
description: Area...
description: Some area
enum:
- East
- West
Expand Down
23 changes: 19 additions & 4 deletions example/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ Resource Types:
</tr><tr>
<td><b><a href="#examplemessagesindexkey">messages</a></b></td>
<td>[]map[string]object</td>
<td></td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>
Expand All @@ -77,17 +79,30 @@ Resource Types:
<tbody><tr>
<td><b>area</b></td>
<td>enum</td>
<td>Area... [East West]</td>
<td>
Some area<br/>
<br/>
<i>Enum</i>: East, West<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>code</b></td>
<td>integer</td>
<td></td>
<td>
Some code<br/>
<br/>
<i>Format</i>: int32<br/>
<i>Default</i>: 7<br/>
<i>Minimum</i>: 0<br/>
<i>Maximum</i>: 10<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>text</b></td>
<td>string</td>
<td></td>
<td>
Just text<br/>
</td>
<td>false</td>
</tr></tbody>
</table>
4 changes: 1 addition & 3 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ func (b *ModelBuilder) addTypeModels(groupModel *GroupModel, kindModel *KindMode
}

fieldDescription := property.Description
if fieldTypename == "enum" {
fieldDescription = fmt.Sprintf("%s %s", fieldDescription, property.Enum)
}

// Create field model
fieldModel := &FieldModel{
Expand All @@ -172,6 +169,7 @@ func (b *ModelBuilder) addTypeModels(groupModel *GroupModel, kindModel *KindMode
TypeKey: fieldTypeKey,
Description: fieldDescription,
Required: containsString(fieldName, schema.Required),
Schema: property,
}
typeModel.Fields = append(typeModel.Fields, fieldModel)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/builder/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package builder

import "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"

type Model struct {
Metadata PageMetadata `yaml:"metadata"`
Groups []*GroupModel `yaml:"groups"`
Expand Down Expand Up @@ -43,6 +45,7 @@ type FieldModel struct {
TypeKey *string
Description string
Required bool
Schema apiextensions.JSONSchemaProps
}

func (m *Model) findGroupModel(group, version string) *GroupModel {
Expand Down
22 changes: 21 additions & 1 deletion templates/frontmatter.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,27 @@ Resource Types:
<tr>
<td><b>{{if .TypeKey}}<a href="{{.TypeKey}}">{{.Name}}</a>{{else}}{{.Name}}{{end}}</b></td>
<td>{{.Type}}</td>
<td>{{.Description}}</td>
<td>
{{.Description}}<br/>
{{- if or .Schema.Format .Schema.Enum .Schema.Default .Schema.Minimum .Schema.Maximum }}
<br/>
{{- end}}
{{- if .Schema.Format }}
<i>Format</i>: {{ .Schema.Format }}<br/>
{{- end }}
{{- if .Schema.Enum }}
<i>Enum</i>: {{ .Schema.Enum | toStrings | join ", " }}<br/>
{{- end }}
{{- if .Schema.Default }}
<i>Default</i>: {{ .Schema.Default }}<br/>
{{- end }}
{{- if .Schema.Minimum }}
<i>Minimum</i>: {{ .Schema.Minimum }}<br/>
{{- end }}
{{- if .Schema.Maximum }}
<i>Maximum</i>: {{ .Schema.Maximum }}<br/>
{{- end }}
</td>
<td>{{.Required}}</td>
</tr>
{{- end -}}
Expand Down
22 changes: 21 additions & 1 deletion templates/markdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,27 @@ Resource Types:
<tr>
<td><b>{{if .TypeKey}}<a href="#{{.TypeKey}}">{{.Name}}</a>{{else}}{{.Name}}{{end}}</b></td>
<td>{{.Type}}</td>
<td>{{.Description}}</td>
<td>
{{.Description}}<br/>
{{- if or .Schema.Format .Schema.Enum .Schema.Default .Schema.Minimum .Schema.Maximum }}
<br/>
{{- end}}
{{- if .Schema.Format }}
<i>Format</i>: {{ .Schema.Format }}<br/>
{{- end }}
{{- if .Schema.Enum }}
<i>Enum</i>: {{ .Schema.Enum | toStrings | join ", " }}<br/>
{{- end }}
{{- if .Schema.Default }}
<i>Default</i>: {{ .Schema.Default }}<br/>
{{- end }}
{{- if .Schema.Minimum }}
<i>Minimum</i>: {{ .Schema.Minimum }}<br/>
{{- end }}
{{- if .Schema.Maximum }}
<i>Maximum</i>: {{ .Schema.Maximum }}<br/>
{{- end }}
</td>
<td>{{.Required}}</td>
</tr>
{{- end -}}
Expand Down

0 comments on commit 3296bd6

Please sign in to comment.