Skip to content

Commit

Permalink
readmegen: show if parameter is required or optional (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
hariso authored Feb 4, 2025
1 parent 371a2b9 commit 6e80a87
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 168 deletions.
43 changes: 39 additions & 4 deletions conn-sdk-cli/readmegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,45 @@ func Generate(opts GenerateOptions) error {
}

var funcMap = template.FuncMap{
"formatCommentYAML": formatCommentYAML,
"formatValueYAML": formatValueYAML,
"zeroValueForType": zeroValueForType,
"args": args,
"formatCommentYAML": formatCommentYAML,
"formatValueYAML": formatValueYAML,
"zeroValueForType": zeroValueForType,
"args": args,
"isParameterRequired": isParameterRequired,
}

func isParameterRequired(param any) (bool, error) {
paramMap, ok := param.(map[string]any)
if !ok {
return false, fmt.Errorf("invalid parameter type: %T", param)
}

validations, ok := paramMap["validations"]
if !ok {
return false, nil
}

validationsSlice, ok := validations.([]interface{})
if !ok {
return false, fmt.Errorf("validations not a slice, got: %T", validations)
}

for _, validation := range validationsSlice {
// Check if the current item is a map
validationMap, ok := validation.(map[string]interface{})
if !ok {
return false, fmt.Errorf("validation not a map, got: %T", validation)
}

// Check if the map has a "type" key with "required" value
if typeVal, exists := validationMap["type"]; exists {
if strTypeVal, ok := typeVal.(string); ok && strTypeVal == "required" {
return true, nil
}
}
}

return false, nil
}

func args(kvs ...any) (map[string]any, error) {
Expand Down
167 changes: 3 additions & 164 deletions conn-sdk-cli/readmegen/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,169 +44,8 @@ func TestGenerate(t *testing.T) {
err = Generate(opts)
is.NoErr(err)

want := `# Test
Name: <!-- readmegen:name -->Test-Connector<!-- /readmegen:name -->
Summary: <!-- readmegen:summary -->test summary<!-- /readmegen:summary -->
Description: <!-- readmegen:description -->
Test description
should be able to handle new lines as well!
<!-- /readmegen:description -->
Version: <!-- readmegen:version -->v0.1.0<!-- /readmegen:version -->
Author: <!-- readmegen:author -->test author<!-- /readmegen:author -->
## Source Parameters
<!-- readmegen:source.parameters.yaml -->
` + "```yaml" + `
version: 2.2
pipelines:
- id: example
status: running
connectors:
- id: example
plugin: "test-connector"
settings:
# string param1 description
# Type: string
param1: "foo"
# int param2 description
# Type: int
param2: "0"
` + "```" + `
<!-- /readmegen:source.parameters.yaml -->
<!-- readmegen:source.parameters.table -->
<table class="no-margin-table">
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td>
` + "`param1`" + `
</td>
<td>
string
</td>
<td>
` + "`foo`" + `
</td>
<td>
string param1 description
</td>
</tr>
<tr>
<td>
` + "`param2`" + `
</td>
<td>
int
</td>
<td>
` + "`0`" + `
</td>
<td>
int param2 description
</td>
</tr>
</table>
<!-- /readmegen:source.parameters.table -->
## Destination Parameters
<!-- readmegen:destination.parameters.yaml -->
` + "```yaml" + `
version: 2.2
pipelines:
- id: example
status: running
connectors:
- id: example
plugin: "test-connector"
settings:
# boolean param1 description
# Type: boolean
param1: "true"
# float param2 description
# Type: float
param2: "0.0"
` + "```" + `
<!-- /readmegen:destination.parameters.yaml -->
<!-- readmegen:destination.parameters.table -->
<table class="no-margin-table">
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td>
` + "`param1`" + `
</td>
<td>
boolean
</td>
<td>
` + "`true`" + `
</td>
<td>
boolean param1 description
</td>
</tr>
<tr>
<td>
` + "`param2`" + `
</td>
<td>
float
</td>
<td>
` + "`0.0`" + `
</td>
<td>
float param2 description
</td>
</tr>
</table>
<!-- /readmegen:destination.parameters.table -->
`
wantBytes, err := os.ReadFile("./testdata/test1want.md")
is.NoErr(err)
want := string(wantBytes)
is.Equal("", cmp.Diff(want, buf.String()))
}
6 changes: 6 additions & 0 deletions conn-sdk-cli/readmegen/templates/parameters.table.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<tr>
<th>Name</th>
<th>Type</th>
<th>Required</th>
<th>Default</th>
<th>Description</th>
</tr>
Expand All @@ -20,6 +21,11 @@
</td>
<td>

{{ if isParameterRequired $param }}✅{{ end }}

</td>
<td>

`{{ if $param.default }}{{ $param.default }}{{ else }}{{ zeroValueForType $param.type }}{{ end }}`

</td>
Expand Down
1 change: 1 addition & 0 deletions conn-sdk-cli/readmegen/templates/parameters.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pipelines:
{{- range $param := .parameters }}
{{ formatCommentYAML $param.description 10 }}
# Type: {{ $param.type }}
# Required: {{ if isParameterRequired $param }}yes{{ else }}no{{ end }}
{{ $param.name }}: {{ if $param.default }}"{{ $param.default }}"{{ else }}"{{ zeroValueForType $param.type }}"{{ end }}
{{- end }}
```
Expand Down
3 changes: 3 additions & 0 deletions conn-sdk-cli/readmegen/testdata/connector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ specification:
type: string
description: string param1 description
default: "foo"
validations:
- type: required
value: ""
- name: param2
type: int
description: int param2 description
Expand Down
Loading

0 comments on commit 6e80a87

Please sign in to comment.