Skip to content

Commit

Permalink
✨ yaml模板、value初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
Rehtt committed May 9, 2023
1 parent 58c00be commit e56493a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
41 changes: 41 additions & 0 deletions value/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package value

import (
"reflect"
)

func InitValue(v interface{}) {
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
if val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
val = val.Elem()
}

if val.Kind() == reflect.Struct {
for i := 0; i < val.NumField(); i++ {
fieldValue := val.Field(i)
var ty = fieldValue.Type()

// 防止死循环
if ty.Kind() == reflect.Ptr && ty.Elem().Name() == val.Type().Name() {
continue
}
InitValue(fieldValue.Addr().Interface())
}
}

//todo test3{test3: []test3 } 死循环
if val.Kind() == reflect.Slice {
if val.Len() == 0 {
l := 1
val.Set(reflect.MakeSlice(val.Type(), l, l))
}
for i := 0; i < val.Len(); i++ {
fieldValue := val.Index(i)
InitValue(fieldValue.Addr().Interface())
}
}

}
12 changes: 9 additions & 3 deletions yaml/marshal.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package yaml

import (
"errors"
"bytes"
"gopkg.in/yaml.v3"
"reflect"
)

// MarshalWithComment 带注释的序列化
func MarshalWithComment(v interface{}) ([]byte, error) {
var tmp bytes.Buffer
var marshal = yaml.NewEncoder(&tmp)
marshal.SetIndent(2)

var node yaml.Node
node.Encode(v)
vv := reflect.TypeOf(v)
for vv.Kind() == reflect.Ptr {
vv = vv.Elem()
}
if vv.Kind() != reflect.Struct {
return nil, errors.New("必须要Struct")
err := marshal.Encode(v)
return tmp.Bytes(), err
}
rangeStruct(vv, &node)
return yaml.Marshal(node)
err := marshal.Encode(node)
return tmp.Bytes(), err
}
func rangeStruct(v reflect.Type, node *yaml.Node) {
if v.Kind() != reflect.Struct {
Expand Down
20 changes: 20 additions & 0 deletions yaml/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ func TestMarshalWithComment(t *testing.T) {
data, _ := MarshalWithComment(tmp)
fmt.Println(string(data))
}

func TestName(t *testing.T) {
type Test2 struct {
C float64 `yaml:"c"`
}
type Test3 struct {
B []string `yaml:"b" comment:"b"`
Q string `yaml:"q"`
W int `yaml:"w" comment:"WW"`
T2 [][]*Test2 `yaml:"t2"`
}
type Test struct {
A string `yaml:"a" comment:"A"`
T2 Test2 `yaml:"t2" comment:"t2"`
T3 *Test3 `yaml:"t3" comment:"t3"`
}
var tmp Test
data, _ := GenYamlTemplate(tmp)
fmt.Println(string(data))
}
16 changes: 16 additions & 0 deletions yaml/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package yaml

import (
"github.com/Rehtt/Kit/value"
"reflect"
)

func GenYamlTemplate(v interface{}) ([]byte, error) {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
val = reflect.New(val.Type())
val.Elem().Set(reflect.ValueOf(v))
}
value.InitValue(val.Interface())
return MarshalWithComment(val.Interface())
}

0 comments on commit e56493a

Please sign in to comment.