Browse Source

yaml loader添加函数

yjp 7 tháng trước cách đây
mục cha
commit
c9109b1a44
2 tập tin đã thay đổi với 97 bổ sung1 xóa
  1. 55 0
      yaml/yaml_loader/yaml_loader.go
  2. 42 1
      yaml/yaml_loader/yaml_loader_test.go

+ 55 - 0
yaml/yaml_loader/yaml_loader.go

@@ -10,6 +10,8 @@ import (
 	"reflect"
 )
 
+type LoadMapCallback func(objectMap map[string]any)
+
 // LoadYamlFile 加载YAML文件,一个文件中可以有多个YAML对象
 // 参数:
 // yamlFilePath: YAML文件路径
@@ -42,6 +44,38 @@ func LoadYamlBytes(yamlBytes []byte, retObjects *[]any) error {
 	return loadYaml(bytes.NewReader(yamlBytes), retObjects)
 }
 
+// LoadYamlMapFile 加载YAML文件,一个文件中可以有多个YAML对象,顶级不能是YAML数组
+// 参数:
+// yamlFilePath: YAML文件路径
+// callback: 回调函数
+// 返回值:
+// 错误
+func LoadYamlMapFile(yamlFilePath string, callback LoadMapCallback) error {
+	yamlFile, err := os.Open(yamlFilePath)
+	if err != nil {
+		return err
+	}
+
+	defer func(yamlFile *os.File) {
+		err := yamlFile.Close()
+		if err != nil {
+			fmt.Println(err)
+		}
+	}(yamlFile)
+
+	return loadYamlMap(yamlFile, callback)
+}
+
+// LoadYamlMapBytes 加载YAML文件字节,一个文件中可以有多个YAML对象,顶级不能是YAML数组
+// 参数:
+// yamlBytes: YAML文件的字节
+// callback: 回调函数
+// 返回值:
+// 错误
+func LoadYamlMapBytes(yamlBytes []byte, callback LoadMapCallback) error {
+	return loadYamlMap(bytes.NewReader(yamlBytes), callback)
+}
+
 func loadYaml(r io.Reader, retObjects *[]any) error {
 	decoder := yaml.NewDecoder(r)
 
@@ -66,3 +100,24 @@ func loadYaml(r io.Reader, retObjects *[]any) error {
 
 	return nil
 }
+
+func loadYamlMap(r io.Reader, callback LoadMapCallback) error {
+	decoder := yaml.NewDecoder(r)
+
+	for {
+		object := make(map[string]any)
+
+		err := decoder.Decode(object)
+		if err != nil {
+			if err == io.EOF {
+				break
+			}
+
+			return err
+		}
+
+		callback(object)
+	}
+
+	return nil
+}

+ 42 - 1
yaml/yaml_loader/yaml_loader_test.go

@@ -17,7 +17,7 @@ type Test struct {
 	Name string `yaml:"name"`
 }
 
-func TestLoadYAMLFile(t *testing.T) {
+func TestLoadYAML(t *testing.T) {
 	testModel1 := new(TestYamlModel1)
 	testModel2 := new(TestYamlModel2)
 	retObjects := []any{testModel1, testModel2}
@@ -72,3 +72,44 @@ func TestLoadYAMLFile(t *testing.T) {
 		t.Fatal("model2名称错误")
 	}
 }
+
+func TestLoadYAMLMap(t *testing.T) {
+	retObjectMaps := make([]map[string]any, 0)
+
+	err := LoadYamlMapFile("test.yaml", func(objectMap map[string]any) {
+		retObjectMaps = append(retObjectMaps, objectMap)
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if retObjectMaps[0]["test1"].(map[string]any)["name"] != "foo1" {
+		t.Fatal("model1名称错误")
+	}
+
+	if retObjectMaps[1]["test2"].(map[string]any)["name"] != "foo2" {
+		t.Fatal("model2名称错误")
+	}
+
+	retObjectMaps = make([]map[string]any, 0)
+
+	yamlFileBytes, err := os.ReadFile("test.yaml")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	err = LoadYamlMapBytes(yamlFileBytes, func(objectMap map[string]any) {
+		retObjectMaps = append(retObjectMaps, objectMap)
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if retObjectMaps[0]["test1"].(map[string]any)["name"] != "foo1" {
+		t.Fatal("model1名称错误")
+	}
+
+	if retObjectMaps[1]["test2"].(map[string]any)["name"] != "foo2" {
+		t.Fatal("model2名称错误")
+	}
+}