|
@@ -10,6 +10,8 @@ import (
|
|
"reflect"
|
|
"reflect"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+type LoadMapCallback func(objectMap map[string]any)
|
|
|
|
+
|
|
// LoadYamlFile 加载YAML文件,一个文件中可以有多个YAML对象
|
|
// LoadYamlFile 加载YAML文件,一个文件中可以有多个YAML对象
|
|
// 参数:
|
|
// 参数:
|
|
// yamlFilePath: YAML文件路径
|
|
// yamlFilePath: YAML文件路径
|
|
@@ -42,6 +44,38 @@ func LoadYamlBytes(yamlBytes []byte, retObjects *[]any) error {
|
|
return loadYaml(bytes.NewReader(yamlBytes), retObjects)
|
|
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 {
|
|
func loadYaml(r io.Reader, retObjects *[]any) error {
|
|
decoder := yaml.NewDecoder(r)
|
|
decoder := yaml.NewDecoder(r)
|
|
|
|
|
|
@@ -66,3 +100,24 @@ func loadYaml(r io.Reader, retObjects *[]any) error {
|
|
|
|
|
|
return nil
|
|
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
|
|
|
|
+}
|