Skip to content

Commit

Permalink
Merge pull request #53 from losisin/feature/add-description
Browse files Browse the repository at this point in the history
add description from comments
  • Loading branch information
losisin committed Apr 11, 2024
2 parents ab9b2e7 + bc9cbbf commit cf57ddf
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
10 changes: 7 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The following annotations are supported:
* [maxProperties](#maxproperties)
* [required](#required)
* [Meta-Data Annotations](#meta-data-annotations)
* [title](#title)
* [title and description](#title-and-description)
* [default](#default)
* [readOnly](#readonly)

Expand Down Expand Up @@ -371,17 +371,21 @@ image:

## Meta-Data Annotations

### title
### title and description

String. [section 9.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-9.1)

```yaml
fullnameOverride: bar # @schema title: My title
fullnameOverride: bar # @schema title: My title ; description: My description
```

```json
```

```json
"fullnameOverride": {
"title": "My title",
"description": "My description",
"type": "string"
},
```
Expand Down
6 changes: 6 additions & 0 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func mergeSchemas(dest, src *Schema) *Schema {
if src.Title != "" {
dest.Title = src.Title
}
if src.Description != "" {
dest.Description = src.Description
}
if src.ReadOnly {
dest.ReadOnly = src.ReadOnly
}
Expand Down Expand Up @@ -149,6 +152,9 @@ func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool) (map[string
if schema.Title != "" {
schemaMap["title"] = schema.Title
}
if schema.Description != "" {
schemaMap["description"] = schema.Description
}
if schema.ReadOnly {
schemaMap["readOnly"] = schema.ReadOnly
}
Expand Down
42 changes: 22 additions & 20 deletions pkg/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func schemasEqual(a, b *Schema) bool {
return a == b
}
// Compare simple fields
if a.Type != b.Type || a.Pattern != b.Pattern || a.UniqueItems != b.UniqueItems || a.Title != b.Title || a.ReadOnly != b.ReadOnly {
if a.Type != b.Type || a.Pattern != b.Pattern || a.UniqueItems != b.UniqueItems || a.Title != b.Title || a.Description != b.Description || a.ReadOnly != b.ReadOnly {
return false
}
// Compare pointer fields
Expand Down Expand Up @@ -156,9 +156,9 @@ func TestMergeSchemas(t *testing.T) {
},
{
name: "meta-data properties",
dest: &Schema{Type: "object", Title: "My Title", ReadOnly: true, Default: "default value"},
src: &Schema{Type: "object", Title: "My Title", ReadOnly: true, Default: "default value"},
want: &Schema{Type: "object", Title: "My Title", ReadOnly: true, Default: "default value"},
dest: &Schema{Type: "object", Title: "My Title", Description: "My description", ReadOnly: true, Default: "default value"},
src: &Schema{Type: "object", Title: "My Title", Description: "My description", ReadOnly: true, Default: "default value"},
want: &Schema{Type: "object", Title: "My Title", Description: "My description", ReadOnly: true, Default: "default value"},
},
}

Expand Down Expand Up @@ -235,24 +235,26 @@ func TestConvertSchemaToMap(t *testing.T) {
{
name: "with all scalar types",
schema: &Schema{
Type: "integer",
MultipleOf: float64Ptr(3),
Maximum: float64Ptr(10),
Minimum: float64Ptr(1),
Pattern: "^abc",
Title: "My Title",
Enum: []interface{}{1, 2, 3},
Default: "default",
Type: "integer",
MultipleOf: float64Ptr(3),
Maximum: float64Ptr(10),
Minimum: float64Ptr(1),
Pattern: "^abc",
Title: "My Title",
Description: "some description",
Enum: []interface{}{1, 2, 3},
Default: "default",
},
want: map[string]interface{}{
"type": "integer",
"multipleOf": 3.0,
"maximum": 10.0,
"minimum": 1.0,
"pattern": "^abc",
"title": "My Title",
"enum": []interface{}{1, 2, 3},
"default": "default",
"type": "integer",
"multipleOf": 3.0,
"maximum": 10.0,
"minimum": 1.0,
"pattern": "^abc",
"title": "My Title",
"description": "some description",
"enum": []interface{}{1, 2, 3},
"default": "default",
},
},
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Schema struct {
Items *Schema `json:"items,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Default interface{} `json:"default,omitempty"`
}
Expand Down Expand Up @@ -163,6 +164,8 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
schema.Type = processList(value, true)
case "title":
schema.Title = value
case "description":
schema.Description = value
case "readOnly":
if v, err := strconv.ParseBool(value); err == nil {
schema.ReadOnly = v
Expand Down
4 changes: 2 additions & 2 deletions pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func TestProcessComment(t *testing.T) {
{
name: "Set meta-data",
schema: &Schema{},
comment: "# @schema title:My Title;readOnly:false;default:\"foo\"",
expectedSchema: &Schema{Title: "My Title", ReadOnly: false, Default: "foo"},
comment: "# @schema title:My Title;description: some description;readOnly:false;default:\"foo\"",
expectedSchema: &Schema{Title: "My Title", Description: "some description", ReadOnly: false, Default: "foo"},
expectedRequired: false,
},
}
Expand Down
1 change: 1 addition & 0 deletions testdata/full.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
]
},
"fullnameOverride": {
"description": "My description",
"pattern": "^[a-z]$",
"title": "My title",
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion testdata/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ empty: # @schema type: [string, null]
replicas: 2 # @schema minimum: 1 ; maximum: 10 ; multipleOf: 2

# Strings
fullnameOverride: bar # @schema pattern: ^[a-z]$ ; title: My title
fullnameOverride: bar # @schema pattern: ^[a-z]$ ; title: My title ; description: My description

# Arrays
imagePullSecrets: [] # @schema item: object
Expand Down

0 comments on commit cf57ddf

Please sign in to comment.