From 1aa2cbbd29fa836376ae775e5f03d20c1b14bc9e Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Mon, 15 Jan 2024 11:12:50 +0000 Subject: [PATCH 1/2] fix: better mode detection for import tool Signed-off-by: jakezhu9 --- pkg/tools/gen/genkcl.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/tools/gen/genkcl.go b/pkg/tools/gen/genkcl.go index 01d0cc4f..f87eb837 100644 --- a/pkg/tools/gen/genkcl.go +++ b/pkg/tools/gen/genkcl.go @@ -57,8 +57,6 @@ func (k *kclGenerator) GenSchema(w io.Writer, filename string, src interface{}) codeStr := string(code) var i interface{} switch { - case strings.Contains(codeStr, "package "): - k.opts.Mode = ModeGoStruct case json.Unmarshal(code, &i) == nil: switch { case strings.Contains(codeStr, "$schema"): @@ -70,6 +68,8 @@ func (k *kclGenerator) GenSchema(w io.Writer, filename string, src interface{}) } case yaml.Unmarshal(code, &i) == nil: k.opts.Mode = ModeYaml + case strings.Contains(codeStr, "package "): + k.opts.Mode = ModeGoStruct default: return errors.New("failed to detect mode") } From 74a58f79b16b86646d7deb46885f55bb5566b4ea Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Mon, 15 Jan 2024 12:09:16 +0000 Subject: [PATCH 2/2] fix: use raw string in generated schema docs Signed-off-by: jakezhu9 --- pkg/tools/gen/templates/kcl/schema.gotmpl | 2 +- pkg/tools/gen/testdata/jsonschema/additional/expect.k | 4 ++-- pkg/tools/gen/testdata/jsonschema/basic/expect.k | 2 +- pkg/tools/gen/testdata/jsonschema/const/expect.k | 2 +- pkg/tools/gen/testdata/jsonschema/document/expect.k | 2 +- pkg/tools/gen/testdata/jsonschema/items/expect.k | 2 +- pkg/tools/gen/testdata/jsonschema/multipleof/expect.k | 2 +- .../gen/testdata/jsonschema/nested-items/expect.k | 10 +++++----- pkg/tools/gen/testdata/jsonschema/nested/expect.k | 4 ++-- pkg/tools/gen/testdata/jsonschema/pattern/expect.k | 2 +- pkg/tools/gen/testdata/jsonschema/ref/expect.k | 6 +++--- pkg/tools/gen/testdata/jsonschema/unsupport/expect.k | 6 +++--- pkg/tools/gen/testdata/jsonschema/validation/expect.k | 2 +- pkg/tools/gen/testdata/terraform/expect.k | 6 +++--- 14 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pkg/tools/gen/templates/kcl/schema.gotmpl b/pkg/tools/gen/templates/kcl/schema.gotmpl index fe826ec2..e726bb83 100644 --- a/pkg/tools/gen/templates/kcl/schema.gotmpl +++ b/pkg/tools/gen/templates/kcl/schema.gotmpl @@ -1,5 +1,5 @@ schema {{ formatName .Name }}: - """{{- "\n" }} + r"""{{- "\n" }} {{- template "document" . }} """{{- "\n" }} diff --git a/pkg/tools/gen/testdata/jsonschema/additional/expect.k b/pkg/tools/gen/testdata/jsonschema/additional/expect.k index 22939902..c307f702 100644 --- a/pkg/tools/gen/testdata/jsonschema/additional/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/additional/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Ethernet: - """ + r""" Ethernet Attributes @@ -17,7 +17,7 @@ schema Ethernet: socket?: EthernetSocket = {"HTTP": 80, "HTTPS": 443} schema EthernetSocket: - """ + r""" EthernetSocket """ diff --git a/pkg/tools/gen/testdata/jsonschema/basic/expect.k b/pkg/tools/gen/testdata/jsonschema/basic/expect.k index 2f380358..ee2519a1 100644 --- a/pkg/tools/gen/testdata/jsonschema/basic/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/basic/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Book: - """ + r""" Book Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/const/expect.k b/pkg/tools/gen/testdata/jsonschema/const/expect.k index c5341481..dfa578fa 100644 --- a/pkg/tools/gen/testdata/jsonschema/const/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/const/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Book: - """ + r""" Book Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/document/expect.k b/pkg/tools/gen/testdata/jsonschema/document/expect.k index b085951b..437de093 100644 --- a/pkg/tools/gen/testdata/jsonschema/document/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/document/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema MyOrder: - """ + r""" Schema for representing an order information. It contains the order number, list of items and the current status. diff --git a/pkg/tools/gen/testdata/jsonschema/items/expect.k b/pkg/tools/gen/testdata/jsonschema/items/expect.k index eb8b1579..a6bc5bbc 100644 --- a/pkg/tools/gen/testdata/jsonschema/items/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/items/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Book: - """ + r""" Book Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/multipleof/expect.k b/pkg/tools/gen/testdata/jsonschema/multipleof/expect.k index 1bcc4979..b4adeb09 100644 --- a/pkg/tools/gen/testdata/jsonschema/multipleof/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/multipleof/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Book: - """ + r""" Book Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/nested-items/expect.k b/pkg/tools/gen/testdata/jsonschema/nested-items/expect.k index b1a4a2ff..6952a306 100644 --- a/pkg/tools/gen/testdata/jsonschema/nested-items/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/nested-items/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema MonacoManifestSchema: - """ + r""" MonacoManifestSchema Attributes @@ -22,7 +22,7 @@ schema MonacoManifestSchema: environment_groups: [MonacoManifestSchemaEnvironmentGroupsItems0] schema MonacoManifestSchemaEnvironmentGroupsItems0: - """ + r""" MonacoManifestSchemaEnvironmentGroupsItems0 Attributes @@ -37,7 +37,7 @@ schema MonacoManifestSchemaEnvironmentGroupsItems0: environments?: [MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0] schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0: - """ + r""" MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0 Attributes @@ -52,7 +52,7 @@ schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0: url?: MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0Url schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0Url: - """ + r""" The URL of this environment Attributes @@ -67,7 +67,7 @@ schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0Url: value?: str schema MonacoManifestSchemaProjectsItems0: - """ + r""" MonacoManifestSchemaProjectsItems0 Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/nested/expect.k b/pkg/tools/gen/testdata/jsonschema/nested/expect.k index b64ad53d..5a1300ae 100644 --- a/pkg/tools/gen/testdata/jsonschema/nested/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/nested/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Book: - """ + r""" Book Attributes @@ -17,7 +17,7 @@ schema Book: author?: BookAuthor schema BookAuthor: - """ + r""" BookAuthor Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/pattern/expect.k b/pkg/tools/gen/testdata/jsonschema/pattern/expect.k index 6656416a..360aff12 100644 --- a/pkg/tools/gen/testdata/jsonschema/pattern/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/pattern/expect.k @@ -5,7 +5,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c import regex schema Cronjob: - """ + r""" Cronjob Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/ref/expect.k b/pkg/tools/gen/testdata/jsonschema/ref/expect.k index 23618790..206d9870 100644 --- a/pkg/tools/gen/testdata/jsonschema/ref/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/ref/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Customer: - """ + r""" Customer Attributes @@ -17,7 +17,7 @@ schema Customer: address?: Address schema Address: - """ + r""" Address Attributes @@ -30,7 +30,7 @@ schema Address: state?: State schema State: - """ + r""" State Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/unsupport/expect.k b/pkg/tools/gen/testdata/jsonschema/unsupport/expect.k index c3c03ee0..d1463b9a 100644 --- a/pkg/tools/gen/testdata/jsonschema/unsupport/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/unsupport/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Shop: - """ + r""" Schema for representing a shop information. In this test case, we use some logic keywords like "oneOf" that can't be directly converted at the moment. To make it still work, we'll convert it into "any" type. @@ -16,14 +16,14 @@ schema Shop: products?: any schema Clothing: - """ + r""" Clothing """ [...str]: any schema Product: - """ + r""" Product Attributes diff --git a/pkg/tools/gen/testdata/jsonschema/validation/expect.k b/pkg/tools/gen/testdata/jsonschema/validation/expect.k index c55ff4ac..d2219e7f 100644 --- a/pkg/tools/gen/testdata/jsonschema/validation/expect.k +++ b/pkg/tools/gen/testdata/jsonschema/validation/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema Book: - """ + r""" Book Attributes diff --git a/pkg/tools/gen/testdata/terraform/expect.k b/pkg/tools/gen/testdata/terraform/expect.k index fd5a2bfb..19b866b9 100644 --- a/pkg/tools/gen/testdata/terraform/expect.k +++ b/pkg/tools/gen/testdata/terraform/expect.k @@ -4,7 +4,7 @@ Editing this file might prove futile when you re-run the KCL auto-gen generate c """ schema AlicloudConfigRule: - """ + r""" AlicloudConfigRule Attributes @@ -17,7 +17,7 @@ schema AlicloudConfigRule: resource_types_scope?: [str] schema AlicloudDbInstance: - """ + r""" AlicloudDbInstance Attributes @@ -40,7 +40,7 @@ schema AlicloudDbInstance: isunique(security_ips) schema ComplianceItem: - """ + r""" ComplianceItem Attributes