Skip to content

Commit 20f3854

Browse files
authored
feat: add dislpaying enum values as a list (#139)
* feat: add dislpaying enum values as a list * fixed the failing tests
1 parent 9d1a41f commit 20f3854

9 files changed

+134
-8
lines changed

pkg/create_html_output.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"slices"
1212
"sort"
13+
"strings"
1314

1415
"github.com/Skarlso/crd-to-sample-yaml/v1beta1"
1516
)
@@ -196,6 +197,7 @@ type Property struct {
196197
Default string
197198
Required bool
198199
Properties []*Property
200+
Enums string
199201
}
200202

201203
// parseCRD takes the properties and constructs a linked list out of the embedded properties that the recursive
@@ -237,6 +239,12 @@ func parseCRD(properties map[string]v1beta1.JSONSchemaProps, version string, min
237239
description = kind
238240
}
239241
}
242+
243+
var enums []string
244+
for _, e := range v.Enum {
245+
enums = append(enums, string(e.Raw))
246+
}
247+
240248
p := &Property{
241249
Name: k,
242250
Type: v.Type,
@@ -246,6 +254,7 @@ func parseCRD(properties map[string]v1beta1.JSONSchemaProps, version string, min
246254
Nullable: v.Nullable,
247255
Version: version,
248256
Required: required,
257+
Enums: strings.Join(enums, ", "),
249258
}
250259
if v.Default != nil {
251260
p.Default = string(v.Default.Raw)
@@ -255,7 +264,7 @@ func parseCRD(properties map[string]v1beta1.JSONSchemaProps, version string, min
255264
case len(properties[k].Properties) > 0 && properties[k].AdditionalProperties == nil:
256265
requiredList = v.Required
257266
depth++
258-
out, err := parseCRD(properties[k].Properties, version, minimal, "", "", requiredList, depth)
267+
out, err := parseCRD(properties[k].Properties, version, minimal, group, kind, requiredList, depth)
259268
if err != nil {
260269
return nil, err
261270
}
@@ -264,7 +273,7 @@ func parseCRD(properties map[string]v1beta1.JSONSchemaProps, version string, min
264273
case properties[k].Type == array && properties[k].Items.Schema != nil && len(properties[k].Items.Schema.Properties) > 0:
265274
depth++
266275
requiredList = v.Required
267-
out, err := parseCRD(properties[k].Items.Schema.Properties, version, minimal, "", "", requiredList, depth)
276+
out, err := parseCRD(properties[k].Items.Schema.Properties, version, minimal, group, kind, requiredList, depth)
268277
if err != nil {
269278
return nil, err
270279
}
@@ -273,7 +282,7 @@ func parseCRD(properties map[string]v1beta1.JSONSchemaProps, version string, min
273282
case properties[k].AdditionalProperties != nil:
274283
depth++
275284
requiredList = v.Required
276-
out, err := parseCRD(properties[k].AdditionalProperties.Schema.Properties, version, minimal, "", "", requiredList, depth)
285+
out, err := parseCRD(properties[k].AdditionalProperties.Schema.Properties, version, minimal, group, kind, requiredList, depth)
277286
if err != nil {
278287
return nil, err
279288
}

pkg/generate.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,12 @@ func outputValueType(v v1beta1.JSONSchemaProps, skipRandom bool) string {
227227
}
228228

229229
if v.Enum != nil {
230-
return string(v.Enum[0].Raw)
230+
var value []string
231+
for _, ev := range v.Enum {
232+
value = append(value, string(ev.Raw))
233+
}
234+
235+
return string(v.Enum[0].Raw) + " # " + strings.Join(value, ", ")
231236
}
232237

233238
st := "string"

pkg/templates/view_with_groups.html

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ <h1>
9898
{{if $v.Required}}
9999
<span class="badge badge-primary">required</span>
100100
{{end}}
101+
{{if $v.Enums}}
102+
<kbd class="text-muted">{{$v.Enums}}</kbd>
103+
{{end}}
101104
</summary>
102105
<div id="{{$v.Name}}" class="collapse-content">
103106
<div class="property-description">
@@ -140,6 +143,9 @@ <h1>
140143
{{if .Required}}
141144
<span class="badge badge-primary">required</span>
142145
{{end}}
146+
{{if .Enums}}
147+
<kbd class="text-muted">{{.Enums}}</kbd>
148+
{{end}}
143149
</summary>
144150
<div id="{{.Name}}" class="collapse-content">
145151
<div class="property-description">

pkg/testdata/sample_crd.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ spec:
5757
items:
5858
type: string
5959
type: array
60+
suspendStrategy:
61+
description: SuspendStrategy can be used to modify the behaviour that
62+
is used when setting suspend to true.
63+
enum:
64+
- ScaleDown
65+
- ScaleDownAndDeleteDisk
66+
type: string
6067
readInputFromSecret:
6168
description: ReadInputFromSecret if defined, the command will take
6269
a list of key/value pairs in a secret and apply them as arguments

pkg/testdata/sample_crd_different_crd_type_golden.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ kind: xXtStorageAccount
33
spec:
44
parameters:
55
accessTier: "Hot"
6-
environment: "production"
6+
environment: "production" # "production", "staging", "preproduction", "quality_assurance", "test", "development", "proof_of_concept", "disaster_recovery", "sandbox", "global"
77
hnsEnabled: false
88
kind: "StorageV2"
99
largeFileShareEnabled: false
10-
location: "westeurope"
10+
location: "westeurope" # "westeurope", "northeurope", "eastus2", "centralus", "australiaeast", "australiacentral", "global"
1111
projectName: string
1212
replicationType: "ZRS"
1313
resourceGroupName: string

pkg/testdata/sample_crd_golden.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ spec:
1111
name: string
1212
namespace: string
1313
schedule: string
14+
suspendStrategy: "ScaleDown" # "ScaleDown", "ScaleDownAndDeleteDisk"
1415
status: {}

pkg/testdata/sample_crd_with_comments_golden.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ spec:
2121
namespace: string
2222
# Schedule of the command. example: 0 * * * * // follows cron job syntax.
2323
schedule: string
24+
# SuspendStrategy can be used to modify the behaviour that is used when setting suspend to true.
25+
suspendStrategy: "ScaleDown" # "ScaleDown", "ScaleDownAndDeleteDisk"
2426
# KrokCommandStatus defines the observed state of KrokCommand
2527
status: {}

pkg/testdata/sample_crd_with_list_and_multiple_versions_golden.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ spec:
2020
scheme: "internet-facing"
2121
subnets: [] # minItems 0 of type string
2222
identityRef:
23-
kind: "AWSClusterControllerIdentity"
23+
kind: "AWSClusterControllerIdentity" # "AWSClusterControllerIdentity", "AWSClusterRoleIdentity", "AWSClusterStaticIdentity"
2424
name: string
2525
imageLookupBaseOS: string
2626
imageLookupFormat: string
@@ -158,7 +158,7 @@ spec:
158158
scheme: "internet-facing"
159159
subnets: [] # minItems 0 of type string
160160
identityRef:
161-
kind: "AWSClusterControllerIdentity"
161+
kind: "AWSClusterControllerIdentity" # "AWSClusterControllerIdentity", "AWSClusterRoleIdentity", "AWSClusterStaticIdentity"
162162
name: string
163163
imageLookupBaseOS: string
164164
imageLookupFormat: string

sample-crd/crd_with_enum.yaml

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.9.2
7+
creationTimestamp: null
8+
name: krokcommands.delivery.krok.app
9+
spec:
10+
group: delivery.krok.app
11+
names:
12+
kind: KrokCommand
13+
listKind: KrokCommandList
14+
plural: krokcommands
15+
singular: krokcommand
16+
scope: Namespaced
17+
versions:
18+
- name: v1alpha1
19+
schema:
20+
openAPIV3Schema:
21+
description: KrokCommand is the Schema for the krokcommands API
22+
properties:
23+
apiVersion:
24+
description: 'APIVersion defines the versioned schema of this representation
25+
of an object. Servers should convert recognized schemas to the latest
26+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
27+
type: string
28+
kind:
29+
description: 'Kind is a string value representing the REST resource this
30+
object represents. Servers may infer this from the endpoint the client
31+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
32+
type: string
33+
metadata:
34+
type: object
35+
spec:
36+
description: KrokCommandSpec defines the desired state of KrokCommand
37+
properties:
38+
commandHasOutputToWrite:
39+
description: CommandHasOutputToWrite if defined, it signals the underlying
40+
Job, to put its output into a generated and created secret.
41+
type: boolean
42+
dependencies:
43+
description: Dependencies defines a list of command names that this
44+
command depends on.
45+
items:
46+
type: string
47+
type: array
48+
enabled:
49+
description: Enabled defines if this command can be executed or not.
50+
type: boolean
51+
suspendStrategy:
52+
default: ScaleDown
53+
description: SuspendStrategy can be used to modify the behaviour that
54+
is used when setting suspend to true.
55+
enum:
56+
- ScaleDown
57+
- ScaleDownAndDeleteDisk
58+
type: string
59+
image:
60+
description: 'Image defines the image name and tag of the command
61+
example: krok-hook/slack-notification:v0.0.1'
62+
type: string
63+
platforms:
64+
description: Platforms holds all the platforms which this command
65+
supports.
66+
items:
67+
type: string
68+
type: array
69+
readInputFromSecret:
70+
description: ReadInputFromSecret if defined, the command will take
71+
a list of key/value pairs in a secret and apply them as arguments
72+
to the command.
73+
properties:
74+
name:
75+
type: string
76+
namespace:
77+
type: string
78+
required:
79+
- name
80+
- namespace
81+
type: object
82+
schedule:
83+
description: 'Schedule of the command. example: 0 * * * * // follows
84+
cron job syntax.'
85+
type: string
86+
required:
87+
- image
88+
type: object
89+
status:
90+
description: KrokCommandStatus defines the observed state of KrokCommand
91+
type: object
92+
type: object
93+
served: true
94+
storage: true
95+
subresources:
96+
status: {}

0 commit comments

Comments
 (0)