From bfc0aad0bd784ed9d528fe034d9f2357c804febe Mon Sep 17 00:00:00 2001 From: Aleksandar Stojanov Date: Thu, 11 Apr 2024 14:45:12 +0200 Subject: [PATCH 1/2] add description from comments Signed-off-by: Aleksandar Stojanov --- docs/README.md | 10 +++++++--- pkg/parser.go | 6 ++++++ pkg/parser_test.go | 34 ++++++++++++++++++---------------- pkg/schema.go | 3 +++ pkg/schema_test.go | 4 ++-- testdata/full.schema.json | 1 + testdata/full.yaml | 2 +- 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2ecd97b..0e7dfd8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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) @@ -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" }, ``` diff --git a/pkg/parser.go b/pkg/parser.go index 61f211b..ced7e49 100644 --- a/pkg/parser.go +++ b/pkg/parser.go @@ -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 } @@ -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 } diff --git a/pkg/parser_test.go b/pkg/parser_test.go index 92c637d..418c04e 100644 --- a/pkg/parser_test.go +++ b/pkg/parser_test.go @@ -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", }, }, } diff --git a/pkg/schema.go b/pkg/schema.go index bc589a9..fa8abee 100644 --- a/pkg/schema.go +++ b/pkg/schema.go @@ -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"` } @@ -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 diff --git a/pkg/schema_test.go b/pkg/schema_test.go index 52faa08..b2108f2 100644 --- a/pkg/schema_test.go +++ b/pkg/schema_test.go @@ -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, }, } diff --git a/testdata/full.schema.json b/testdata/full.schema.json index a46c5e5..157870e 100644 --- a/testdata/full.schema.json +++ b/testdata/full.schema.json @@ -89,6 +89,7 @@ ] }, "fullnameOverride": { + "description": "My description", "pattern": "^[a-z]$", "title": "My title", "type": "string" diff --git a/testdata/full.yaml b/testdata/full.yaml index 0f09868..c407166 100644 --- a/testdata/full.yaml +++ b/testdata/full.yaml @@ -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 From bc9cbbf83d65c993af3cd1210a5c4704dbfd5b9a Mon Sep 17 00:00:00 2001 From: Aleksandar Stojanov Date: Thu, 11 Apr 2024 14:50:40 +0200 Subject: [PATCH 2/2] add some more tests Signed-off-by: Aleksandar Stojanov --- pkg/parser_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/parser_test.go b/pkg/parser_test.go index 418c04e..f59864e 100644 --- a/pkg/parser_test.go +++ b/pkg/parser_test.go @@ -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 @@ -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"}, }, }