Skip to content

Commit

Permalink
fix: 修复kv导出时格式不对问题 (#2980)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambition9186 authored Feb 26, 2024
1 parent ccc49ef commit 201a0f6
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion bcs-services/bcs-bscp/cmd/api-server/service/released_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"net/http"
"reflect"
"strconv"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -63,7 +64,15 @@ type XMLExporter struct {

// Export method implements the Exporter interface, exporting data as a byte slice in YAML format.
func (ye *YAMLExporter) Export() ([]byte, error) {
return yaml.Marshal(ye.OutData)
buffer := &bytes.Buffer{}
encoder := yaml.NewEncoder(buffer)
// 设置缩进
encoder.SetIndent(2)
defer func() {
_ = encoder.Close()
}()
err := encoder.Encode(ye.OutData)
return buffer.Bytes(), err
}

// Export method implements the Exporter interface, exporting data as a byte slice in JSON format.
Expand Down Expand Up @@ -198,6 +207,10 @@ func rkvsToOutData(details []*pbrkv.ReleasedKv) map[string]interface{} {
value = i
case string(table.KvJson):
_ = json.Unmarshal([]byte(rkv.Spec.Value), &value)
if reflect.TypeOf(value).Kind() != reflect.String {
jm, _ := json.Marshal(value)
value = string(jm)
}
}
d[rkv.Spec.Key] = map[string]interface{}{
"kv_type": rkv.Spec.KvType,
Expand All @@ -219,6 +232,10 @@ func kvsToOutData(details []*pbkv.Kv) map[string]interface{} {
value = i
case string(table.KvJson):
_ = json.Unmarshal([]byte(rkv.Spec.Value), &value)
if reflect.TypeOf(value).Kind() != reflect.String {
jm, _ := json.Marshal(value)
value = string(jm)
}
}
d[rkv.Spec.Key] = map[string]interface{}{
"kv_type": rkv.Spec.KvType,
Expand Down

0 comments on commit 201a0f6

Please sign in to comment.