Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better mode detection and use raw string in docs for import tool #215

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/tools/gen/genkcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/templates/kcl/schema.gotmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
schema {{ formatName .Name }}:
"""{{- "\n" }}
r"""{{- "\n" }}
{{- template "document" . }}
"""{{- "\n" }}

Expand Down
4 changes: 2 additions & 2 deletions pkg/tools/gen/testdata/jsonschema/additional/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@ schema Ethernet:
socket?: EthernetSocket = {"HTTP": 80, "HTTPS": 443}

schema EthernetSocket:
"""
r"""
EthernetSocket
"""

Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/testdata/jsonschema/basic/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/testdata/jsonschema/const/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/testdata/jsonschema/document/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/testdata/jsonschema/items/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/testdata/jsonschema/multipleof/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/tools/gen/testdata/jsonschema/nested-items/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +22,7 @@ schema MonacoManifestSchema:
environment_groups: [MonacoManifestSchemaEnvironmentGroupsItems0]

schema MonacoManifestSchemaEnvironmentGroupsItems0:
"""
r"""
MonacoManifestSchemaEnvironmentGroupsItems0

Attributes
Expand All @@ -37,7 +37,7 @@ schema MonacoManifestSchemaEnvironmentGroupsItems0:
environments?: [MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0]

schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0:
"""
r"""
MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0

Attributes
Expand All @@ -52,7 +52,7 @@ schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0:
url?: MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0Url

schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0Url:
"""
r"""
The URL of this environment

Attributes
Expand All @@ -67,7 +67,7 @@ schema MonacoManifestSchemaEnvironmentGroupsItems0EnvironmentsItems0Url:
value?: str

schema MonacoManifestSchemaProjectsItems0:
"""
r"""
MonacoManifestSchemaProjectsItems0

Attributes
Expand Down
4 changes: 2 additions & 2 deletions pkg/tools/gen/testdata/jsonschema/nested/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@ schema Book:
author?: BookAuthor

schema BookAuthor:
"""
r"""
BookAuthor

Attributes
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/testdata/jsonschema/pattern/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/tools/gen/testdata/jsonschema/ref/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@ schema Customer:
address?: Address

schema Address:
"""
r"""
Address

Attributes
Expand All @@ -30,7 +30,7 @@ schema Address:
state?: State

schema State:
"""
r"""
State

Attributes
Expand Down
6 changes: 3 additions & 3 deletions pkg/tools/gen/testdata/jsonschema/unsupport/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -16,14 +16,14 @@ schema Shop:
products?: any

schema Clothing:
"""
r"""
Clothing
"""

[...str]: any

schema Product:
"""
r"""
Product

Attributes
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/gen/testdata/jsonschema/validation/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/tools/gen/testdata/terraform/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@ schema AlicloudConfigRule:
resource_types_scope?: [str]

schema AlicloudDbInstance:
"""
r"""
AlicloudDbInstance

Attributes
Expand All @@ -40,7 +40,7 @@ schema AlicloudDbInstance:
isunique(security_ips)

schema ComplianceItem:
"""
r"""
ComplianceItem

Attributes
Expand Down
Loading