From 2e53d443d2e2cfdf6cd2a05760094815f54544ee Mon Sep 17 00:00:00 2001 From: ambition <918632536@qq.com> Date: Mon, 29 Jan 2024 15:54:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AF=BC=E5=87=BAkv?= =?UTF-8?q?=E6=97=B6=E5=92=8C=E5=AF=BC=E5=85=A5=E7=9A=84=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmd/api-server/service/released_kv.go | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/bcs-services/bcs-bscp/cmd/api-server/service/released_kv.go b/bcs-services/bcs-bscp/cmd/api-server/service/released_kv.go index cc87032148..5af3e38dff 100644 --- a/bcs-services/bcs-bscp/cmd/api-server/service/released_kv.go +++ b/bcs-services/bcs-bscp/cmd/api-server/service/released_kv.go @@ -13,6 +13,7 @@ package service import ( + "bytes" "encoding/json" "encoding/xml" "errors" @@ -25,6 +26,7 @@ import ( "github.com/xuri/excelize/v2" "gopkg.in/yaml.v3" + "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/table" "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/kit" "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs" pbcs "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/config-server" @@ -64,7 +66,14 @@ func (ye *YAMLExporter) Export() ([]byte, error) { // Export method implements the Exporter interface, exporting data as a byte slice in JSON format. func (je *JSONExporter) Export() ([]byte, error) { - return json.Marshal(je.OutData) + buffer := &bytes.Buffer{} + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + err := encoder.Encode(je.OutData) + if err != nil { + return nil, err + } + return buffer.Bytes(), nil } // Export method implements the Exporter interface, exporting data as a byte slice in XLSX format. @@ -147,7 +156,6 @@ func (m *kvService) Export(w http.ResponseWriter, r *http.Request) { logs.Errorf("export kv fail, err: %v", err) _ = render.Render(w, r, rest.BadRequest(err)) } - _, err = w.Write(content) if err != nil { logs.Errorf("Error writing response:", err) @@ -166,9 +174,18 @@ type RkvOutData struct { func rkvsToOutData(rkvs []*pbrkv.ReleasedKv) map[string]interface{} { d := map[string]interface{}{} for _, rkv := range rkvs { + var value interface{} + value = rkv.Spec.Value + switch rkv.Spec.KvType { + case string(table.KvNumber): + i, _ := strconv.Atoi(rkv.Spec.Value) + value = i + case string(table.KvJson): + _ = json.Unmarshal([]byte(rkv.Spec.Value), &value) + } d[rkv.Spec.Key] = map[string]interface{}{ "kv_type": rkv.Spec.KvType, - "value": rkv.Spec.Value, + "value": value, } }