Repository: kcl-lang/kcl-go (go)

Issue:
support Go API for GetSchemaTypeMapping
## Feature Request

**Is your feature request related to a problem? Please describe:**
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->

We have rest API `rpc GetSchemaTypeMapping(GetSchemaTypeMapping_Args) returns(GetSchemaTypeMapping_Result);` on kcl-py but the corresponding Go API is missing.

The `GetSchemaTypeMapping((file: string, code:string)` is required to export HIR of KCL schema, which could be used to integrate other tools, like kcl-doc, vscode-kcl low code create from schema, etc.

**Describe the feature you'd like:**
<!-- A clear and concise description of what you want to happen. -->
add `GetSchemaTypeMapping((file: string, code:string)` Go API

**Describe alternatives you've considered:**
<!-- A clear and concise description of any alternative solutions or features you've considered. -->

**Teachability, Documentation, Adoption, Migration Strategy:**
<!-- If you can, explain some scenarios how users might use this, situations it would be helpful in. Any API designs, mockups, or diagrams are also helpful. -->


Pull request description:
fix: fix escape error in document
<!-- Thank you for contributing to KCL!

Note: 

1. With pull requests:

    - Open your pull request against "main"
    - Your pull request should have no more than two commits, if not you should squash them.
    - It should pass all tests in the available continuous integration systems such as GitHub Actions.
    - You should add/modify tests to cover your proposed code changes.
    - If your pull request contains a new feature, please document it on the README.

2. Please create an issue first to describe the problem.

    We recommend that link the issue with the PR in the following question.
    For more info, check https://kcl-lang.io/docs/community/contribute/
-->

#### 1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

- [ ] N
- [x] Y

fix #https://github.com/kcl-lang/kcl/issues/1797 

<!-- You can add issue references here. 
    e.g. 
    fix #123, re #123, 
    fix https://github.com/XXX/issues/44
-->

#### 2. What is the scope of this PR (e.g. component or file name):

<!-- You can add the scope of this change here. 
    e.g. 
    /src/server/core.rs,
    kcl-lang/kcl/src/parser 
-->

#### 3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):

<!-- You can choose a brief description here -->
- [x] Affects user behaviors
- [ ] Contains syntax changes
- [ ] Contains variable changes
- [ ] Contains experimental features
- [ ] Performance regression: Consumes more CPU
- [ ] Performance regression: Consumes more Memory
- [ ] Other

<!-- You can add more details here.
    e.g. 
    Call method "XXXX" to ..... in order to ....,
    More details: https://XXXX.com/doc......
-->

#### 4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):

- [x] N
- [ ] Y 

<!-- You can add more details here.
    e.g. 
    Calling method "XXXX" will cause the "XXXX", "XXXX" modules to be affected.
    More details: https://XXXX.com/doc......
-->

#### 5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links:

<!-- You can choose a brief description here -->
- [x] Unit test
- [ ] Integration test
- [ ] Benchmark (add benchmark stats below)
- [ ] Manual test (add detailed scripts or steps below)
- [ ] Other

<!-- You can add more details here.
e.g. 
The test case in XXXX is used to .....
test cases in /src/tests/XXXXX
test cases https://github.com/XXX/pull/44
benchmark stats: time XXX ms
-->


Patch:
diff --git a/go.mod b/go.mod
index 209a87c..192158e 100644
--- a/go.mod
+++ b/go.mod
@@ -22,7 +22,7 @@ require (
 	google.golang.org/grpc v1.70.0
 	google.golang.org/protobuf v1.36.4
 	gopkg.in/yaml.v3 v3.0.1
-	kcl-lang.io/lib v0.11.1
+	kcl-lang.io/lib v0.11.0
 )
 
 require (
diff --git a/go.sum b/go.sum
index c431d73..bf5fab3 100644
--- a/go.sum
+++ b/go.sum
@@ -113,5 +113,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-kcl-lang.io/lib v0.11.1 h1:fdInWNMxvh4Ig0Jti1B/t+7BxZw3SKdpjjUG8QWREHw=
-kcl-lang.io/lib v0.11.1/go.mod h1:0Dw/MQwRMjLDksxl4JerGBn/ueaxRyCCKBCCwQwJ1MI=
+kcl-lang.io/lib v0.11.0 h1:Oxf5OrhTfoMVmEHH8cNsfxlpChvS3pXuNdEaNdZB4T8=
+kcl-lang.io/lib v0.11.0/go.mod h1:0Dw/MQwRMjLDksxl4JerGBn/ueaxRyCCKBCCwQwJ1MI=
diff --git a/pkg/tools/gen/genkcl_value.go b/pkg/tools/gen/genkcl_value.go
index 12f8984..5ecd34e 100644
--- a/pkg/tools/gen/genkcl_value.go
+++ b/pkg/tools/gen/genkcl_value.go
@@ -52,6 +52,7 @@ type printer struct {
 	writer       io.Writer
 	listInline   bool
 	configInline bool
+	escape       bool
 }
 
 func (p *printer) writeIndent() {
@@ -243,7 +244,7 @@ func (p *printer) walkValue(v any) error {
 		p.writeConfigEnd()
 	case reflect.String:
 		value := val.String()
-		if isStringEscaped(value) {
+		if isStringEscaped(value) && p.escape {
 			if value[len(value)-1] == '"' {
 				// if the string ends with '"' then we need to add a space after the closing triple quote
 				p.write(fmt.Sprintf(`r"""%s """`, value))
diff --git a/pkg/tools/gen/template.go b/pkg/tools/gen/template.go
index 4352a13..48c2555 100644
--- a/pkg/tools/gen/template.go
+++ b/pkg/tools/gen/template.go
@@ -29,10 +29,11 @@ var (
 )
 
 var funcs = template.FuncMap{
-	"formatType":  formatType,
-	"formatValue": formatValue,
-	"formatName":  formatName,
-	"indentLines": indentLines,
+	"formatType":            formatType,
+	"formatValue":           formatValue,
+	"formatValueWithEscape": formatValueWithEscape,
+	"formatName":            formatName,
+	"indentLines":           indentLines,
 	"isKclData": func(v interface{}) bool {
 		_, ok := v.([]data)
 		return ok
@@ -92,12 +93,13 @@ func formatType(t typeInterface) string {
 	return typAny
 }
 
-func formatValue(v interface{}) string {
+func formatValueWithEscape(v interface{}, escape bool) string {
 	var buf bytes.Buffer
 	p := &printer{
 		listInline:   true,
 		configInline: true,
 		writer:       &buf,
+		escape:       escape,
 	}
 	err := p.walkValue(v)
 	if err != nil {
@@ -107,6 +109,10 @@ func formatValue(v interface{}) string {
 	}
 }
 
+func formatValue(v interface{}) string {
+	return formatValueWithEscape(v, true)
+}
+
 var kclKeywords = map[string]struct{}{
 	"True":      {},
 	"False":     {},
diff --git a/pkg/tools/gen/templates/kcl/document.gotmpl b/pkg/tools/gen/templates/kcl/document.gotmpl
index f9b44b9..a217cf2 100644
--- a/pkg/tools/gen/templates/kcl/document.gotmpl
+++ b/pkg/tools/gen/templates/kcl/document.gotmpl
@@ -10,7 +10,7 @@
     ----------
   {{- range .Properties }}
     {{ formatName .Name }} : {{ formatType .Type }}, {{ if .Required }}required{{ else }}optional{{ end }}
-    {{- if .HasDefault }}, default is {{ formatValue .DefaultValue }}{{ end }}
+    {{- if .HasDefault }}, default is {{ formatValueWithEscape .DefaultValue false}}{{ end }}
     {{- if .Description }}{{ "\n" }}{{ indentLines .Description "        " }}{{ end }}
   {{- end -}}
 
diff --git a/scripts/kclvm.go b/scripts/kclvm.go
index cedf198..e21a326 100644
--- a/scripts/kclvm.go
+++ b/scripts/kclvm.go
@@ -17,10 +17,9 @@ const (
 )
 
 const (
-	KclvmAbiVersion         KclvmVersionType = KclvmVersionType_v0_11_1
-	KclvmVersionType_latest                  = KclvmVersionType_v0_11_1
+	KclvmAbiVersion         KclvmVersionType = KclvmVersionType_v0_11_0
+	KclvmVersionType_latest                  = KclvmVersionType_v0_11_0
 
-	KclvmVersionType_v0_11_1        KclvmVersionType = "v0.11.1"
 	KclvmVersionType_v0_11_0        KclvmVersionType = "v0.11.0"
 	KclvmVersionType_v0_11_0_alpha1 KclvmVersionType = "v0.11.0-alpha.1"
 	KclvmVersionType_v0_10_0        KclvmVersionType = "v0.10.0"


Test patch:
diff --git a/pkg/tools/gen/genkcl_test.go b/pkg/tools/gen/genkcl_test.go
index 5bd9773..c83695e 100644
--- a/pkg/tools/gen/genkcl_test.go
+++ b/pkg/tools/gen/genkcl_test.go
@@ -2,6 +2,7 @@ package gen
 
 import (
 	"bytes"
+	"fmt"
 	"log"
 	"os"
 	"path/filepath"
@@ -142,11 +143,14 @@ func TestGenKclFromJsonSchema(t *testing.T) {
 	for _, testcase := range cases {
 		t.Run(testcase.name, func(t *testing.T) {
 			var buf bytes.Buffer
-			err := GenKcl(&buf, testcase.input, nil, &GenKclOptions{})
+			err := GenKcl(&buf, testcase.input, nil, &GenKclOptions{
+				Mode: ModeJsonSchema,
+			})
 			if err != nil {
 				t.Fatal(err)
 			}
 			result := buf.Bytes()
+			fmt.Printf("result: %v\n", string(result))
 			assert2.Equal(t, testcase.expect, string(bytes.ReplaceAll(result, []byte("\r\n"), []byte("\n"))))
 		})
 	}
diff --git a/pkg/tools/gen/testdata/jsonschema/document_quota/expect.k b/pkg/tools/gen/testdata/jsonschema/document_quota/expect.k
new file mode 100644
index 0000000..3ac6184
--- /dev/null
+++ b/pkg/tools/gen/testdata/jsonschema/document_quota/expect.k
@@ -0,0 +1,16 @@
+"""
+This file was generated by the KCL auto-gen tool. DO NOT EDIT.
+Editing this file might prove futile when you re-run the KCL auto-gen generate command.
+"""
+
+schema Input:
+    r"""
+    Input
+
+    Attributes
+    ----------
+    foo : str, required, default is "more \"quotes\""
+    """
+
+    foo: str = r"""more "quotes" """
+
diff --git a/pkg/tools/gen/testdata/jsonschema/document_quota/input.json b/pkg/tools/gen/testdata/jsonschema/document_quota/input.json
new file mode 100644
index 0000000..e6a3581
--- /dev/null
+++ b/pkg/tools/gen/testdata/jsonschema/document_quota/input.json
@@ -0,0 +1,12 @@
+{
+    "type": "object",
+    "properties": {
+        "foo": {
+            "type": "string",
+            "default": "more \"quotes\""
+        }
+    },
+    "required": [
+        "foo"
+    ]
+}
\ No newline at end of file
diff --git a/pkg/tools/gen/testdata/jsonschema/document_quota_0/expect.k b/pkg/tools/gen/testdata/jsonschema/document_quota_0/expect.k
new file mode 100644
index 0000000..986ecc7
--- /dev/null
+++ b/pkg/tools/gen/testdata/jsonschema/document_quota_0/expect.k
@@ -0,0 +1,24 @@
+"""
+This file was generated by the KCL auto-gen tool. DO NOT EDIT.
+Editing this file might prove futile when you re-run the KCL auto-gen generate command.
+"""
+
+schema Input:
+    r"""
+    Input
+
+    Attributes
+    ----------
+    foo : str, required, default is "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pellentesque ligula consequat, aliquam elit vitae, dapibus velit. Donec posuere venenatis diam, et viverra mi facilisis nec. Mauris eget velit eu risus ornare accumsan. Suspendisse rutrum vehicula tellus. Morbi lobortis lorem eget odio consequat dapibus. Etiam metus arcu, tincidunt id enim eget, mollis tempor purus. Sed finibus ante id odio dignissim mollis. Fusce auctor sit amet risus nec dictum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec ornare orci nec fermentum accumsan. Vivamus fermentum dolor non ligula consequat, vitae pellentesque quam accumsan. Ut lobortis facilisis pharetra. Nullam tristique orci in nisi tempor, ut consequat metus molestie.\n\nSuspendisse justo quam, fermentum id ultrices sed, fringilla non eros. Ut leo risus, efficitur id posuere sit amet, sagittis vitae risus. Nullam mattis arcu quis ligula tempus ullamcorper. Donec vel nibh vel sem fermentum efficitur id a velit. Maecenas congue iaculis arcu et consectetur. Maecenas gravida nisl vitae eros dignissim, rutrum malesuada felis pulvinar. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae;\n\nCurabitur vitae augue et sapien pulvinar scelerisque. Proin eleifend varius feugiat. Nullam sagittis ante at ligula pharetra, vitae molestie turpis aliquet. Donec vitae odio nec tortor accumsan condimentum. Phasellus suscipit semper tortor, ac sodales orci varius non. Praesent iaculis ultrices dui in fringilla. Sed tristique libero sed sapien sagittis eleifend.\n\nCurabitur id fermentum ex, ac aliquam ligula. Duis tellus neque, luctus eu tristique eget, viverra vitae magna. Maecenas facilisis mauris quam, eu sodales elit vulputate quis. Suspendisse nulla ipsum, auctor ut efficitur sed, rhoncus ut nisi. Donec viverra libero a rhoncus bibendum. Nullam sit amet metus nisi. Nunc venenatis eu ante quis egestas.\n\nUt sollicitudin pellentesque sem in consectetur. Nunc imperdiet lacus in venenatis consectetur. Vestibulum sed turpis tempor, mattis velit eu, hendrerit quam. Morbi ac pretium orci, ut mattis felis. Phasellus id faucibus orci, lobortis bibendum eros. Nunc nec libero consectetur, elementum tortor vitae, cursus orci. Curabitur nisl velit, auctor eu rutrum id, suscipit cursus est."
+    """
+
+    foo: str = r"""Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pellentesque ligula consequat, aliquam elit vitae, dapibus velit. Donec posuere venenatis diam, et viverra mi facilisis nec. Mauris eget velit eu risus ornare accumsan. Suspendisse rutrum vehicula tellus. Morbi lobortis lorem eget odio consequat dapibus. Etiam metus arcu, tincidunt id enim eget, mollis tempor purus. Sed finibus ante id odio dignissim mollis. Fusce auctor sit amet risus nec dictum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec ornare orci nec fermentum accumsan. Vivamus fermentum dolor non ligula consequat, vitae pellentesque quam accumsan. Ut lobortis facilisis pharetra. Nullam tristique orci in nisi tempor, ut consequat metus molestie.
+
+Suspendisse justo quam, fermentum id ultrices sed, fringilla non eros. Ut leo risus, efficitur id posuere sit amet, sagittis vitae risus. Nullam mattis arcu quis ligula tempus ullamcorper. Donec vel nibh vel sem fermentum efficitur id a velit. Maecenas congue iaculis arcu et consectetur. Maecenas gravida nisl vitae eros dignissim, rutrum malesuada felis pulvinar. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae;
+
+Curabitur vitae augue et sapien pulvinar scelerisque. Proin eleifend varius feugiat. Nullam sagittis ante at ligula pharetra, vitae molestie turpis aliquet. Donec vitae odio nec tortor accumsan condimentum. Phasellus suscipit semper tortor, ac sodales orci varius non. Praesent iaculis ultrices dui in fringilla. Sed tristique libero sed sapien sagittis eleifend.
+
+Curabitur id fermentum ex, ac aliquam ligula. Duis tellus neque, luctus eu tristique eget, viverra vitae magna. Maecenas facilisis mauris quam, eu sodales elit vulputate quis. Suspendisse nulla ipsum, auctor ut efficitur sed, rhoncus ut nisi. Donec viverra libero a rhoncus bibendum. Nullam sit amet metus nisi. Nunc venenatis eu ante quis egestas.
+
+Ut sollicitudin pellentesque sem in consectetur. Nunc imperdiet lacus in venenatis consectetur. Vestibulum sed turpis tempor, mattis velit eu, hendrerit quam. Morbi ac pretium orci, ut mattis felis. Phasellus id faucibus orci, lobortis bibendum eros. Nunc nec libero consectetur, elementum tortor vitae, cursus orci. Curabitur nisl velit, auctor eu rutrum id, suscipit cursus est."""
+
diff --git a/pkg/tools/gen/testdata/jsonschema/document_quota_0/input.json b/pkg/tools/gen/testdata/jsonschema/document_quota_0/input.json
new file mode 100644
index 0000000..02cf26c
--- /dev/null
+++ b/pkg/tools/gen/testdata/jsonschema/document_quota_0/input.json
@@ -0,0 +1,12 @@
+{
+    "type": "object",
+    "properties": {
+        "foo": {
+            "type": "string",
+            "default": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pellentesque ligula consequat, aliquam elit vitae, dapibus velit. Donec posuere venenatis diam, et viverra mi facilisis nec. Mauris eget velit eu risus ornare accumsan. Suspendisse rutrum vehicula tellus. Morbi lobortis lorem eget odio consequat dapibus. Etiam metus arcu, tincidunt id enim eget, mollis tempor purus. Sed finibus ante id odio dignissim mollis. Fusce auctor sit amet risus nec dictum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec ornare orci nec fermentum accumsan. Vivamus fermentum dolor non ligula consequat, vitae pellentesque quam accumsan. Ut lobortis facilisis pharetra. Nullam tristique orci in nisi tempor, ut consequat metus molestie.\n\nSuspendisse justo quam, fermentum id ultrices sed, fringilla non eros. Ut leo risus, efficitur id posuere sit amet, sagittis vitae risus. Nullam mattis arcu quis ligula tempus ullamcorper. Donec vel nibh vel sem fermentum efficitur id a velit. Maecenas congue iaculis arcu et consectetur. Maecenas gravida nisl vitae eros dignissim, rutrum malesuada felis pulvinar. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae;\n\nCurabitur vitae augue et sapien pulvinar scelerisque. Proin eleifend varius feugiat. Nullam sagittis ante at ligula pharetra, vitae molestie turpis aliquet. Donec vitae odio nec tortor accumsan condimentum. Phasellus suscipit semper tortor, ac sodales orci varius non. Praesent iaculis ultrices dui in fringilla. Sed tristique libero sed sapien sagittis eleifend.\n\nCurabitur id fermentum ex, ac aliquam ligula. Duis tellus neque, luctus eu tristique eget, viverra vitae magna. Maecenas facilisis mauris quam, eu sodales elit vulputate quis. Suspendisse nulla ipsum, auctor ut efficitur sed, rhoncus ut nisi. Donec viverra libero a rhoncus bibendum. Nullam sit amet metus nisi. Nunc venenatis eu ante quis egestas.\n\nUt sollicitudin pellentesque sem in consectetur. Nunc imperdiet lacus in venenatis consectetur. Vestibulum sed turpis tempor, mattis velit eu, hendrerit quam. Morbi ac pretium orci, ut mattis felis. Phasellus id faucibus orci, lobortis bibendum eros. Nunc nec libero consectetur, elementum tortor vitae, cursus orci. Curabitur nisl velit, auctor eu rutrum id, suscipit cursus est."
+        }
+    },
+    "required": [
+        "foo"
+    ]
+}
\ No newline at end of file