From 56f88f759aee5d49b58c1eccb02660076fd5bc6c Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 17 Jan 2022 17:10:44 +0800 Subject: [PATCH] add buildin function yamli for package gview --- encoding/gjson/gjson_api_encoding.go | 8 ++++++-- encoding/gyaml/gyaml.go | 23 +++++++++++++++++++++++ encoding/gyaml/gyaml_z_unit_test.go | 12 ++++++++++++ os/gview/gview.go | 1 + os/gview/gview_buildin.go | 7 +++++++ os/gview/gview_z_unit_test.go | 13 +++++++++++++ 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/encoding/gjson/gjson_api_encoding.go b/encoding/gjson/gjson_api_encoding.go index f3efa1af3..8d95cb946 100644 --- a/encoding/gjson/gjson_api_encoding.go +++ b/encoding/gjson/gjson_api_encoding.go @@ -120,6 +120,12 @@ func (j *Json) ToYaml() ([]byte, error) { return gyaml.Encode(*(j.p)) } +func (j *Json) ToYamlIndent(indent string) ([]byte, error) { + j.mu.RLock() + defer j.mu.RUnlock() + return gyaml.EncodeIndent(*(j.p), indent) +} + func (j *Json) ToYamlString() (string, error) { b, e := j.ToYaml() return string(b), e @@ -170,8 +176,6 @@ func (j *Json) MustToTomlString() string { // ToIni json to ini func (j *Json) ToIni() ([]byte, error) { - j.mu.RLock() - defer j.mu.RUnlock() return gini.Encode(j.Map()) } diff --git a/encoding/gyaml/gyaml.go b/encoding/gyaml/gyaml.go index 1fbdda593..b646b270d 100644 --- a/encoding/gyaml/gyaml.go +++ b/encoding/gyaml/gyaml.go @@ -8,6 +8,9 @@ package gyaml import ( + "bytes" + "strings" + "gopkg.in/yaml.v3" "github.com/gogf/gf/v2/errors/gerror" @@ -22,6 +25,26 @@ func Encode(value interface{}) (out []byte, err error) { return } +func EncodeIndent(value interface{}, indent string) (out []byte, err error) { + out, err = Encode(value) + if err != nil { + return + } + if indent != "" { + var ( + buffer = bytes.NewBuffer(nil) + array = strings.Split(strings.TrimSpace(string(out)), "\n") + ) + for _, v := range array { + buffer.WriteString(indent) + buffer.WriteString(v) + buffer.WriteString("\n") + } + out = buffer.Bytes() + } + return +} + func Decode(value []byte) (interface{}, error) { var ( result map[string]interface{} diff --git a/encoding/gyaml/gyaml_z_unit_test.go b/encoding/gyaml/gyaml_z_unit_test.go index e7b99e9c7..c840f4ef0 100644 --- a/encoding/gyaml/gyaml_z_unit_test.go +++ b/encoding/gyaml/gyaml_z_unit_test.go @@ -59,6 +59,18 @@ func Test_Encode(t *testing.T) { }) } +func Test_EncodeIndent(t *testing.T) { + // Array. + gtest.C(t, func(t *gtest.T) { + b, err := gyaml.EncodeIndent([]string{"a", "b", "c"}, "####") + t.AssertNil(err) + t.Assert(string(b), `####- a +####- b +####- c +`) + }) +} + func Test_Decode(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := gyaml.Decode([]byte(yamlStr)) diff --git a/os/gview/gview.go b/os/gview/gview.go index 95f9d37e1..086532e9d 100644 --- a/os/gview/gview.go +++ b/os/gview/gview.go @@ -149,6 +149,7 @@ func New(path ...string) *View { "xml": view.buildInFuncXml, "ini": view.buildInFuncIni, "yaml": view.buildInFuncYaml, + "yamli": view.buildInFuncYamlIndent, "toml": view.buildInFuncToml, "plus": view.buildInFuncPlus, "minus": view.buildInFuncMinus, diff --git a/os/gview/gview_buildin.go b/os/gview/gview_buildin.go index 076023f7d..92ef1fc58 100644 --- a/os/gview/gview_buildin.go +++ b/os/gview/gview_buildin.go @@ -249,6 +249,13 @@ func (view *View) buildInFuncYaml(value interface{}) (string, error) { return string(b), err } +// buildInFuncYamlIndent implements build-in template function: yamli , +// which encodes and returns `value` as YAML string with custom indent string. +func (view *View) buildInFuncYamlIndent(value, indent interface{}) (string, error) { + b, err := gjson.New(value).ToYamlIndent(gconv.String(indent)) + return string(b), err +} + // buildInFuncToml implements build-in template function: toml , // which encodes and returns `value` as TOML string. func (view *View) buildInFuncToml(value interface{}) (string, error) { diff --git a/os/gview/gview_z_unit_test.go b/os/gview/gview_z_unit_test.go index 3e558e346..771094afa 100644 --- a/os/gview/gview_z_unit_test.go +++ b/os/gview/gview_z_unit_test.go @@ -467,6 +467,19 @@ func Test_BuildInFuncYaml(t *testing.T) { }) } +func Test_BuildInFuncYamlIndent(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + v := gview.New() + v.Assign("v", g.Map{ + "name": "john", + }) + r, err := v.ParseContent(context.TODO(), `{{yamli .v "####"}}`) + t.AssertNil(err) + t.Assert(r, `####name: john +`) + }) +} + func Test_BuildInFuncToml(t *testing.T) { gtest.C(t, func(t *gtest.T) { v := gview.New()